Skip to content

Commit 5af7f29

Browse files
committed
Started working on patchValue for both fields and form
1 parent 66b0c58 commit 5af7f29

File tree

5 files changed

+41
-22
lines changed

5 files changed

+41
-22
lines changed

example/lib/sources/complete_form.dart

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ class CompleteFormState extends State<CompleteForm> {
9292
decoration: const InputDecoration(labelText: 'Chips'),
9393
name: 'chips_test',
9494
onChanged: _onChanged,
95-
initialValue: [
96-
Contact('Andrew', '[email protected]',
97-
'https://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX4057996.jpg'),
98-
],
9995
maxChips: 5,
10096
findSuggestions: (String query) {
10197
if (query.isNotEmpty) {
@@ -189,9 +185,7 @@ class CompleteFormState extends State<CompleteForm> {
189185
divisions: 20,
190186
activeColor: Colors.red,
191187
inactiveColor: Colors.pink[100],
192-
decoration: const InputDecoration(
193-
labelText: 'Price Range',
194-
),
188+
decoration: const InputDecoration(labelText: 'Price Range'),
195189
),
196190
FormBuilderCheckbox(
197191
name: 'accept_terms',
@@ -326,7 +320,8 @@ class CompleteFormState extends State<CompleteForm> {
326320
),
327321
FormBuilderSegmentedControl(
328322
decoration: const InputDecoration(
329-
labelText: 'Movie Rating (Archer)'),
323+
labelText: 'Movie Rating (Archer)',
324+
),
330325
name: 'movie_rating',
331326
// initialValue: 1,
332327
// textStyle: TextStyle(fontWeight: FontWeight.bold),
@@ -403,7 +398,9 @@ class CompleteFormState extends State<CompleteForm> {
403398
initialValue: 'Germany',
404399
name: 'country',
405400
decoration: const InputDecoration(
406-
border: OutlineInputBorder(), labelText: 'Country'),
401+
border: OutlineInputBorder(),
402+
labelText: 'Country',
403+
),
407404
validator: FormBuilderValidators.compose([
408405
FormBuilderValidators.required(context,
409406
errorText: 'This field required.'),
@@ -433,6 +430,7 @@ class CompleteFormState extends State<CompleteForm> {
433430
FormBuilderSearchableDropdown<Contact>(
434431
decoration: InputDecoration(
435432
labelText: 'Phone Number',
433+
border: OutlineInputBorder(),
436434
),
437435
name: 'searchable',
438436
items: contacts
@@ -459,12 +457,26 @@ class CompleteFormState extends State<CompleteForm> {
459457
style: TextStyle(color: Colors.white),
460458
),
461459
onPressed: () {
462-
if (_fbKey.currentState.saveAndValidate()) {
460+
_fbKey.currentState.fields['color_picker']
461+
.patchValue(Colors.black);
462+
_fbKey.currentState.patchValue({
463+
'age': '50',
464+
'slider': 6.7,
465+
'filter_chip': ['Test 1'],
466+
'choice_chip': 'Test 2',
467+
'rate': 4,
468+
'chips_test': [
469+
Contact('Andrew', '[email protected]',
470+
'https://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX4057996.jpg'),
471+
],
472+
});
473+
474+
/*if (_fbKey.currentState.saveAndValidate()) {
463475
print(_fbKey.currentState.value);
464476
} else {
465477
print(_fbKey.currentState.value);
466478
print('validation failed');
467-
}
479+
}*/
468480
},
469481
),
470482
),
@@ -473,10 +485,7 @@ class CompleteFormState extends State<CompleteForm> {
473485
child: OutlineButton(
474486
focusNode: FocusNode(),
475487
color: Theme.of(context).accentColor,
476-
child: Text(
477-
'Reset',
478-
style: TextStyle(color: Colors.white),
479-
),
488+
child: Text('Reset', style: TextStyle(color: Colors.white)),
480489
onPressed: () {
481490
_fbKey.currentState.reset();
482491
},

lib/src/fields/form_builder_signature_pad.dart

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ class FormBuilderSignaturePad extends FormBuilderField {
6060
child: Column(
6161
children: <Widget>[
6262
Container(
63-
decoration: BoxDecoration(
64-
border: border,
65-
),
63+
decoration: BoxDecoration(border: border),
6664
child: GestureDetector(
6765
onVerticalDragUpdate: (_) {},
6866
child: Signature(
@@ -85,10 +83,7 @@ class FormBuilderSignaturePad extends FormBuilderField {
8583
clearButtonText ?? localizations.cancelButtonLabel,
8684
style: TextStyle(color: theme.errorColor),
8785
),
88-
icon: Icon(
89-
Icons.clear,
90-
color: theme.errorColor,
91-
),
86+
icon: Icon(Icons.clear, color: theme.errorColor),
9287
),
9388
],
9489
),

lib/src/fields/form_builder_text_field.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,9 @@ class _FormBuilderTextFieldState extends FormBuilderFieldState {
230230
_effectiveController.text = initialValue ?? '';
231231
});
232232
}
233+
234+
@override
235+
void patchValue(dynamic val){
236+
_effectiveController.text = val;
237+
}
233238
}

lib/src/form_builder.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ class FormBuilderState extends State<FormBuilder> {
185185
_formKey.currentState.reset();
186186
}
187187

188+
void patchValue(Map<String, dynamic> val){
189+
val.forEach((key, value) {
190+
_fields[key]?.patchValue(value);
191+
});
192+
}
193+
188194
@override
189195
Widget build(BuildContext context) {
190196
return Form(

lib/src/form_builder_field.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,8 @@ class FormBuilderFieldState<T> extends FormFieldState<T> {
156156
_focusNode?.dispose();
157157
super.dispose();
158158
}
159+
160+
void patchValue(T value){
161+
didChange(value);
162+
}
159163
}

0 commit comments

Comments
 (0)