Skip to content

Commit 2d6fe1e

Browse files
committed
fomat length
1 parent 58297e8 commit 2d6fe1e

1 file changed

Lines changed: 54 additions & 15 deletions

File tree

lib/src/form_builder.dart

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,16 @@ class FormBuilder extends StatefulWidget {
110110
this.canPop,
111111
});
112112

113-
static FormBuilderState? of(BuildContext context) => context.findAncestorStateOfType<FormBuilderState>();
113+
static FormBuilderState? of(BuildContext context) =>
114+
context.findAncestorStateOfType<FormBuilderState>();
114115

115116
@override
116117
FormBuilderState createState() => FormBuilderState();
117118
}
118119

119120
/// A type alias for a map of form fields.
120-
typedef FormBuilderFields = Map<String, FormBuilderFieldState<FormBuilderField<dynamic>, dynamic>>;
121+
typedef FormBuilderFields =
122+
Map<String, FormBuilderFieldState<FormBuilderField<dynamic>, dynamic>>;
121123

122124
class FormBuilderState extends State<FormBuilder> {
123125
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
@@ -151,7 +153,9 @@ class FormBuilderState extends State<FormBuilder> {
151153

152154
/// Get a map of errors
153155
Map<String, String> get errors => {
154-
for (var element in fields.entries.where((element) => element.value.hasError))
156+
for (var element in fields.entries.where(
157+
(element) => element.value.hasError,
158+
))
155159
element.key.toString(): element.value.errorText ?? '',
156160
};
157161

@@ -163,12 +167,22 @@ class FormBuilderState extends State<FormBuilder> {
163167

164168
/// Get all fields values of form.
165169
Map<String, dynamic> get instantValue => Map<String, dynamic>.unmodifiable(
166-
_instantValue.map((key, value) => MapEntry(key, _transformers[key] == null ? value : _transformers[key]!(value))),
170+
_instantValue.map(
171+
(key, value) => MapEntry(
172+
key,
173+
_transformers[key] == null ? value : _transformers[key]!(value),
174+
),
175+
),
167176
);
168177

169178
/// Returns the saved value only
170179
Map<String, dynamic> get value => Map<String, dynamic>.unmodifiable(
171-
_savedValue.map((key, value) => MapEntry(key, _transformers[key] == null ? value : _transformers[key]!(value))),
180+
_savedValue.map(
181+
(key, value) => MapEntry(
182+
key,
183+
_transformers[key] == null ? value : _transformers[key]!(value),
184+
),
185+
),
172186
);
173187

174188
dynamic transformValue<T>(String name, T? v) {
@@ -181,7 +195,8 @@ class FormBuilderState extends State<FormBuilder> {
181195
}
182196

183197
T? getRawValue<T>(String name, {bool fromSaved = false}) {
184-
return (fromSaved ? _savedValue[name] : _instantValue[name]) ?? initialValue[name];
198+
return (fromSaved ? _savedValue[name] : _instantValue[name]) ??
199+
initialValue[name];
185200
}
186201

187202
/// Get a field value by name
@@ -275,7 +290,10 @@ class FormBuilderState extends State<FormBuilder> {
275290
/// the form will auto scroll to show this invalid field.
276291
/// In this case, the automatic scroll happens because is a behavior inside the framework,
277292
/// not because [autoScrollWhenFocusOnInvalid] is `true`.
278-
bool validate({bool focusOnInvalid = true, bool autoScrollWhenFocusOnInvalid = false}) {
293+
bool validate({
294+
bool focusOnInvalid = true,
295+
bool autoScrollWhenFocusOnInvalid = false,
296+
}) {
279297
_focusOnInvalid = focusOnInvalid;
280298
final hasError = !_formKey.currentState!.validate();
281299
if (hasError) {
@@ -299,9 +317,15 @@ class FormBuilderState extends State<FormBuilder> {
299317
/// the form will auto scroll to show this invalid field.
300318
/// In this case, the automatic scroll happens because is a behavior inside the framework,
301319
/// not because [autoScrollWhenFocusOnInvalid] is `true`.
302-
bool saveAndValidate({bool focusOnInvalid = true, bool autoScrollWhenFocusOnInvalid = false}) {
320+
bool saveAndValidate({
321+
bool focusOnInvalid = true,
322+
bool autoScrollWhenFocusOnInvalid = false,
323+
}) {
303324
save();
304-
return validate(focusOnInvalid: focusOnInvalid, autoScrollWhenFocusOnInvalid: autoScrollWhenFocusOnInvalid);
325+
return validate(
326+
focusOnInvalid: focusOnInvalid,
327+
autoScrollWhenFocusOnInvalid: autoScrollWhenFocusOnInvalid,
328+
);
305329
}
306330

307331
/// Reset form to `initialValue` set on FormBuilder or on each field.
@@ -332,7 +356,11 @@ class FormBuilderState extends State<FormBuilder> {
332356
/// the form will auto scroll to show this invalid field.
333357
/// In this case, the automatic scroll happens because is a behavior inside the framework,
334358
/// not because [autoScrollWhenFocusOnInvalid] is `true`.
335-
void patchError(Map<String, String> errors, {bool focusOnInvalid = true, bool autoScrollWhenFocusOnInvalid = false}) {
359+
void patchError(
360+
Map<String, String> errors, {
361+
bool focusOnInvalid = true,
362+
bool autoScrollWhenFocusOnInvalid = false,
363+
}) {
336364
errors.forEach((key, value) {
337365
_fields[key]?.invalidate(value);
338366
});
@@ -352,8 +380,12 @@ class FormBuilderState extends State<FormBuilder> {
352380
/// the form will auto scroll to show this invalid field.
353381
/// In this case, the automatic scroll happens because is a behavior inside the framework,
354382
/// not because [autoScrollWhenFocusOnInvalid] is `true`.
355-
void scrollToFirstInvalidField({bool focusOnInvalid = true, bool autoScrollWhenFocusOnInvalid = false}) {
356-
final wrongFields = fields.values.where((element) => element.hasError).toList();
383+
void scrollToFirstInvalidField({
384+
bool focusOnInvalid = true,
385+
bool autoScrollWhenFocusOnInvalid = false,
386+
}) {
387+
final wrongFields =
388+
fields.values.where((element) => element.hasError).toList();
357389
if (wrongFields.isNotEmpty) {
358390
if (focusOnInvalid) {
359391
wrongFields.first.focus();
@@ -384,19 +416,26 @@ class FormBuilderState extends State<FormBuilder> {
384416
onPopInvokedWithResult: widget.onPopInvokedWithResult,
385417
canPop: widget.canPop,
386418
// `onChanged` is called during setInternalFieldValue else will be called early
387-
child: _FormBuilderScope(formState: this, child: FocusTraversalGroup(child: widget.child)),
419+
child: _FormBuilderScope(
420+
formState: this,
421+
child: FocusTraversalGroup(child: widget.child),
422+
),
388423
);
389424
}
390425
}
391426

392427
class _FormBuilderScope extends InheritedWidget {
393-
const _FormBuilderScope({required super.child, required FormBuilderState formState}) : _formState = formState;
428+
const _FormBuilderScope({
429+
required super.child,
430+
required FormBuilderState formState,
431+
}) : _formState = formState;
394432

395433
final FormBuilderState _formState;
396434

397435
/// The [Form] associated with this widget.
398436
FormBuilder get form => _formState.widget;
399437

400438
@override
401-
bool updateShouldNotify(_FormBuilderScope oldWidget) => oldWidget._formState != _formState;
439+
bool updateShouldNotify(_FormBuilderScope oldWidget) =>
440+
oldWidget._formState != _formState;
402441
}

0 commit comments

Comments
 (0)