1
- import 'dart:developer' ;
2
-
3
1
import 'package:flutter/material.dart' ;
4
2
import 'package:flutter_form_builder/flutter_form_builder.dart' ;
5
3
@@ -102,16 +100,33 @@ typedef FormBuilderFields
102
100
= Map <String , FormBuilderFieldState <FormBuilderField <dynamic >, dynamic >>;
103
101
104
102
class FormBuilderState extends State <FormBuilder > {
105
- final _formKey = GlobalKey <FormState >();
103
+ final GlobalKey <FormState > _formKey = GlobalKey <FormState >();
104
+ final FormBuilderFields _fields = {};
105
+ final Map <String , dynamic > _instantValue = {};
106
+ final Map <String , dynamic > _savedValue = {};
107
+ // Because dart type system will not accept ValueTransformer<dynamic>
108
+ final Map <String , Function > _transformers = {};
106
109
107
110
bool get enabled => widget.enabled;
108
111
109
- final FormBuilderFields _fields = {};
112
+ /// Verify if all fields on form are valid.
113
+ bool get isValid => fields.values.every ((field) => field.isValid);
114
+
115
+ /// Will be true if some field on form are dirty.
116
+ ///
117
+ /// Dirty: The value of field is changed by user or by logic code.
118
+ bool get isDirty => fields.values.any ((field) => field.isDirty);
119
+
120
+ /// Will be true if some field on form are touched.
121
+ ///
122
+ /// Touched: The field is focused by user or by logic code.
123
+ bool get isTouched => fields.values.any ((field) => field.isTouched);
124
+
125
+ /// Get initialValue.
126
+ Map <String , dynamic > get initialValue => widget.initialValue;
110
127
111
- //because dart type system will not accept ValueTransformer<dynamic>
112
- final _transformers = < String , Function > {};
113
- final _instantValue = < String , dynamic > {};
114
- final _savedValue = < String , dynamic > {};
128
+ /// Get all fields of form.
129
+ FormBuilderFields get fields => _fields;
115
130
116
131
Map <String , dynamic > get instantValue =>
117
132
Map <String , dynamic >.unmodifiable (_instantValue.map ((key, value) =>
@@ -122,11 +137,6 @@ class FormBuilderState extends State<FormBuilder> {
122
137
Map <String , dynamic >.unmodifiable (_savedValue.map ((key, value) =>
123
138
MapEntry (key, _transformers[key]? .call (value) ?? value)));
124
139
125
- /// Returns values after saving
126
- Map <String , dynamic > get initialValue => widget.initialValue;
127
-
128
- FormBuilderFields get fields => _fields;
129
-
130
140
dynamic transformValue <T >(String name, T ? v) {
131
141
final t = _transformers[name];
132
142
return t != null ? t.call (v) : v;
@@ -150,9 +160,6 @@ class FormBuilderState extends State<FormBuilder> {
150
160
widget.onChanged? .call ();
151
161
}
152
162
153
- bool get isValid =>
154
- fields.values.where ((element) => ! element.isValid).isEmpty;
155
-
156
163
void removeInternalFieldValue (
157
164
String name, {
158
165
required bool isSetState,
@@ -282,22 +289,15 @@ class FormBuilderState extends State<FormBuilder> {
282
289
);
283
290
}
284
291
292
+ /// Reset form to `initialValue`
285
293
void reset () {
286
- _formKey.currentState! .reset ();
287
- for (var field in _fields.entries) {
288
- try {
289
- field.value.didChange (getRawValue (field.key));
290
- } catch (e, st) {
291
- log (
292
- 'Error when resetting field: ${field .key }' ,
293
- error: e,
294
- stackTrace: st,
295
- level: 2000 ,
296
- );
297
- }
298
- }
294
+ _formKey.currentState? .reset ();
299
295
}
300
296
297
+ /// Update fields values of form.
298
+ /// Useful when need update all values at once, after init.
299
+ ///
300
+ /// To load all values at once on init, use `initialValue` property
301
301
void patchValue (Map <String , dynamic > val) {
302
302
val.forEach ((key, dynamic value) {
303
303
_fields[key]? .didChange (value);
0 commit comments