Skip to content

Commit 6e55bbf

Browse files
committed
format
1 parent ac77d13 commit 6e55bbf

File tree

2 files changed

+72
-30
lines changed

2 files changed

+72
-30
lines changed

lib/src/form_builder_field.dart

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ class FormBuilderField<T> extends FormField<T> {
6262
});
6363

6464
@override
65-
FormBuilderFieldState<FormBuilderField<T>, T> createState() => FormBuilderFieldState<FormBuilderField<T>, T>();
65+
FormBuilderFieldState<FormBuilderField<T>, T> createState() =>
66+
FormBuilderFieldState<FormBuilderField<T>, T>();
6667
}
6768

68-
class FormBuilderFieldState<F extends FormBuilderField<T>, T> extends FormFieldState<T> {
69+
class FormBuilderFieldState<F extends FormBuilderField<T>, T>
70+
extends FormFieldState<T> {
6971
String? _customErrorText;
7072
FormBuilderState? _formBuilderState;
7173
bool _touched = false;
@@ -81,7 +83,10 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, T> extends FormFieldS
8183
/// Returns the initial value, which may be declared at the field, or by the
8284
/// parent [FormBuilder.initialValue]. When declared at both levels, the field
8385
/// initialValue prevails.
84-
T? get initialValue => widget.initialValue ?? (_formBuilderState?.initialValue ?? const <String, dynamic>{})[widget.name] as T?;
86+
T? get initialValue =>
87+
widget.initialValue ??
88+
(_formBuilderState?.initialValue ??
89+
const <String, dynamic>{})[widget.name] as T?;
8590

8691
dynamic get transformedValue => widget.valueTransformer?.call(value) ?? value;
8792

@@ -99,7 +104,9 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, T> extends FormFieldS
99104

100105
bool get enabled => widget.enabled && (_formBuilderState?.enabled ?? true);
101106
bool get readOnly => !(_formBuilderState?.widget.skipDisabled ?? false);
102-
bool get _isAlwaysValidate => widget.autovalidateMode.isAlways || (_formBuilderState?.widget.autovalidateMode?.isAlways ?? false);
107+
bool get _isAlwaysValidate =>
108+
widget.autovalidateMode.isAlways ||
109+
(_formBuilderState?.widget.autovalidateMode?.isAlways ?? false);
103110

104111
/// Will be true if the field is dirty
105112
///
@@ -235,9 +242,14 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, T> extends FormFieldS
235242
}
236243
final isValid = super.validate() && !hasError;
237244

238-
final fields = _formBuilderState?.fields ?? <String, FormBuilderFieldState<FormBuilderField<dynamic>, dynamic>>{};
245+
final fields = _formBuilderState?.fields ??
246+
<String, FormBuilderFieldState<FormBuilderField<dynamic>, dynamic>>{};
239247

240-
if (!isValid && focusOnInvalid && (formState?.focusOnInvalid ?? true) && enabled && !fields.values.any((e) => e.effectiveFocusNode.hasFocus)) {
248+
if (!isValid &&
249+
focusOnInvalid &&
250+
(formState?.focusOnInvalid ?? true) &&
251+
enabled &&
252+
!fields.values.any((e) => e.effectiveFocusNode.hasFocus)) {
241253
focus();
242254
if (autoScrollWhenFocusOnInvalid) ensureScrollableVisibility();
243255
}

test/src/form_builder_field_test.dart

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import '../form_builder_tester.dart';
77
void main() {
88
group('FormBuilderField -', () {
99
group('custom error -', () {
10-
testWidgets('Should show custom error when invalidate field', (tester) async {
10+
testWidgets('Should show custom error when invalidate field',
11+
(tester) async {
1112
final textFieldKey = GlobalKey<FormBuilderFieldState>();
1213
const textFieldName = 'text2';
1314
const errorTextField = 'error text field';
@@ -41,15 +42,18 @@ void main() {
4142

4243
expect(textFieldKey.currentState?.isValid, isFalse);
4344
});
44-
testWidgets('Should valid when no has error and autovalidateMode is always', (tester) async {
45+
testWidgets(
46+
'Should valid when no has error and autovalidateMode is always',
47+
(tester) async {
4548
final textFieldKey = GlobalKey<FormBuilderFieldState>();
4649
const textFieldName = 'text';
4750
const errorTextField = 'error text field';
4851
final testWidget = FormBuilderTextField(
4952
name: textFieldName,
5053
key: textFieldKey,
5154
autovalidateMode: AutovalidateMode.always,
52-
validator: (value) => value == null || value.isEmpty ? errorTextField : null,
55+
validator: (value) =>
56+
value == null || value.isEmpty ? errorTextField : null,
5357
);
5458
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
5559

@@ -61,15 +65,18 @@ void main() {
6165

6266
expect(textFieldKey.currentState?.isValid, isTrue);
6367
});
64-
testWidgets('Should invalid when has error and autovalidateMode is always', (tester) async {
68+
testWidgets(
69+
'Should invalid when has error and autovalidateMode is always',
70+
(tester) async {
6571
final textFieldKey = GlobalKey<FormBuilderFieldState>();
6672
const textFieldName = 'text';
6773
const errorTextField = 'error text field';
6874
final testWidget = FormBuilderTextField(
6975
name: textFieldName,
7076
key: textFieldKey,
7177
autovalidateMode: AutovalidateMode.always,
72-
validator: (value) => value == null || value.length < 10 ? errorTextField : null,
78+
validator: (value) =>
79+
value == null || value.length < 10 ? errorTextField : null,
7380
);
7481
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
7582

@@ -100,7 +107,8 @@ void main() {
100107

101108
expect(textFieldKey.currentState?.hasError, isTrue);
102109
});
103-
testWidgets('Should no has errors when is empty and no has validators', (tester) async {
110+
testWidgets('Should no has errors when is empty and no has validators',
111+
(tester) async {
104112
final textFieldKey = GlobalKey<FormBuilderFieldState>();
105113
const textFieldName = 'text';
106114
final testWidget = FormBuilderTextField(
@@ -118,7 +126,9 @@ void main() {
118126
});
119127

120128
group('valueIsValid -', () {
121-
testWidgets('Should value is valid when validator passes, despite set custom error', (tester) async {
129+
testWidgets(
130+
'Should value is valid when validator passes, despite set custom error',
131+
(tester) async {
122132
final textFieldKey = GlobalKey<FormBuilderFieldState>();
123133
const textFieldName = 'text';
124134
const errorTextField = 'error text field';
@@ -137,7 +147,8 @@ void main() {
137147
});
138148

139149
group('valueHasError -', () {
140-
testWidgets('Should value is invalid when validator passes', (tester) async {
150+
testWidgets('Should value is invalid when validator passes',
151+
(tester) async {
141152
final textFieldKey = GlobalKey<FormBuilderFieldState>();
142153
const textFieldName = 'text';
143154
const invalidValue = 'invalid';
@@ -155,20 +166,25 @@ void main() {
155166
});
156167

157168
group('autovalidateMode -', () {
158-
testWidgets('Should show error when init form and AutovalidateMode is always', (tester) async {
169+
testWidgets(
170+
'Should show error when init form and AutovalidateMode is always',
171+
(tester) async {
159172
const textFieldName = 'text4';
160173
const errorTextField = 'error text field';
161174
final testWidget = FormBuilderTextField(
162175
name: textFieldName,
163-
validator: (value) => value == null || value.isEmpty ? errorTextField : null,
176+
validator: (value) =>
177+
value == null || value.isEmpty ? errorTextField : null,
164178
autovalidateMode: AutovalidateMode.always,
165179
);
166180
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
167181
await tester.pumpAndSettle();
168182

169183
expect(find.text(errorTextField), findsOneWidget);
170184
});
171-
testWidgets('Should show error when AutovalidateMode is onUserInteraction and change field', (tester) async {
185+
testWidgets(
186+
'Should show error when AutovalidateMode is onUserInteraction and change field',
187+
(tester) async {
172188
const textFieldName = 'text4';
173189
const errorTextField = 'error text field';
174190
final testWidget = FormBuilderTextField(
@@ -190,34 +206,40 @@ void main() {
190206
testWidgets('Should not dirty by default', (tester) async {
191207
const textFieldName = 'text';
192208
final textFieldKey = GlobalKey<FormBuilderFieldState>();
193-
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
209+
final testWidget =
210+
FormBuilderTextField(name: textFieldName, key: textFieldKey);
194211
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
195212

196213
expect(textFieldKey.currentState?.isDirty, false);
197214
});
198-
testWidgets('Should dirty when update field value by user', (tester) async {
215+
testWidgets('Should dirty when update field value by user',
216+
(tester) async {
199217
const textFieldName = 'text';
200218
final textFieldKey = GlobalKey<FormBuilderFieldState>();
201-
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
219+
final testWidget =
220+
FormBuilderTextField(name: textFieldName, key: textFieldKey);
202221
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
203222

204223
final widgetFinder = find.byWidget(testWidget);
205224
await tester.enterText(widgetFinder, 'test');
206225

207226
expect(textFieldKey.currentState?.isDirty, true);
208227
});
209-
testWidgets('Should dirty when update field value by method', (tester) async {
228+
testWidgets('Should dirty when update field value by method',
229+
(tester) async {
210230
const textFieldName = 'text';
211231
final textFieldKey = GlobalKey<FormBuilderFieldState>();
212-
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
232+
final testWidget =
233+
FormBuilderTextField(name: textFieldName, key: textFieldKey);
213234
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
214235

215236
textFieldKey.currentState?.setValue('test');
216237
await tester.pumpAndSettle();
217238

218239
expect(textFieldKey.currentState?.isDirty, true);
219240
});
220-
testWidgets('Should dirty when update field with initial value by user', (tester) async {
241+
testWidgets('Should dirty when update field with initial value by user',
242+
(tester) async {
221243
const textFieldName = 'text';
222244
final textFieldKey = GlobalKey<FormBuilderFieldState>();
223245
final testWidget = FormBuilderTextField(
@@ -232,7 +254,8 @@ void main() {
232254

233255
expect(textFieldKey.currentState?.isDirty, true);
234256
});
235-
testWidgets('Should dirty when update field with initial value by method', (tester) async {
257+
testWidgets('Should dirty when update field with initial value by method',
258+
(tester) async {
236259
const textFieldName = 'text';
237260
final textFieldKey = GlobalKey<FormBuilderFieldState>();
238261
final testWidget = FormBuilderTextField(
@@ -250,7 +273,8 @@ void main() {
250273
testWidgets('Should not dirty when reset field value', (tester) async {
251274
const textFieldName = 'text';
252275
final textFieldKey = GlobalKey<FormBuilderFieldState>();
253-
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
276+
final testWidget =
277+
FormBuilderTextField(name: textFieldName, key: textFieldKey);
254278
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
255279

256280
textFieldKey.currentState?.setValue('test');
@@ -259,7 +283,8 @@ void main() {
259283

260284
expect(textFieldKey.currentState?.isDirty, false);
261285
});
262-
testWidgets('Should not dirty when reset field with initial value', (tester) async {
286+
testWidgets('Should not dirty when reset field with initial value',
287+
(tester) async {
263288
const textFieldName = 'text';
264289
final textFieldKey = GlobalKey<FormBuilderFieldState>();
265290
final testWidget = FormBuilderTextField(
@@ -281,15 +306,17 @@ void main() {
281306
testWidgets('Should not touched by default', (tester) async {
282307
const textFieldName = 'text';
283308
final textFieldKey = GlobalKey<FormBuilderFieldState>();
284-
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
309+
final testWidget =
310+
FormBuilderTextField(name: textFieldName, key: textFieldKey);
285311
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
286312

287313
expect(textFieldKey.currentState?.isTouched, false);
288314
});
289315
testWidgets('Should touched when focus input', (tester) async {
290316
const textFieldName = 'text';
291317
final textFieldKey = GlobalKey<FormBuilderFieldState>();
292-
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
318+
final testWidget =
319+
FormBuilderTextField(name: textFieldName, key: textFieldKey);
293320
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
294321

295322
final widgetFinder = find.byWidget(testWidget);
@@ -303,7 +330,8 @@ void main() {
303330
testWidgets('Should reset to null when call reset', (tester) async {
304331
const textFieldName = 'text';
305332
final textFieldKey = GlobalKey<FormBuilderFieldState>();
306-
final testWidget = FormBuilderTextField(name: textFieldName, key: textFieldKey);
333+
final testWidget =
334+
FormBuilderTextField(name: textFieldName, key: textFieldKey);
307335
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
308336

309337
textFieldKey.currentState?.setValue('test');
@@ -329,7 +357,9 @@ void main() {
329357

330358
expect(textFieldKey.currentState?.value, equals(initialValue));
331359
});
332-
testWidgets('Should reset custom error when invalidate field and then reset', (tester) async {
360+
testWidgets(
361+
'Should reset custom error when invalidate field and then reset',
362+
(tester) async {
333363
final textFieldKey = GlobalKey<FormBuilderFieldState>();
334364
const textFieldName = 'text';
335365
const errorTextField = 'error text field';

0 commit comments

Comments
 (0)