Skip to content

Commit d04bda6

Browse files
committed
Use ObjectKeys to enforce rebuild after reset
1 parent ab1f0f1 commit d04bda6

11 files changed

+181
-167
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## [3.9.0] - 03-May-2020
22
* New field type `FormBuilderImagePicker` courtesy of [Gustavo Vítor](https://github.com/gustavovitor)
3-
* Switched rating package from [sy_flutter_widgets](https://pub.dev/packages/form_builder_map_field) to [sy_flutter_widgets](https://pub.dev/packages/rating_bar) with more configuration options
3+
* Switched rating package from [sy_flutter_widgets](https://pub.dev/packages/form_builder_map_field) to [rating_bar](https://pub.dev/packages/rating_bar) with more configuration options
44
* Added option to `showCheckmark` for FormBuilderFilterChip, along with other options. Closes #260
55
* Added option to `allowEmpty` in `minLength` and `maxLength` validations. Closes #259
66
* Fixed bug where `locale`, `textDirection`, `useRootNavigator` & `builder` not passed down to `showDatePicker()` and `showTimePicker()`

lib/src/fields/form_builder_checkbox_list.dart

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class _FormBuilderCheckboxListState extends State<FormBuilderCheckboxList> {
7676
? null
7777
: (bool value) {
7878
FocusScope.of(context).requestFocus(FocusNode());
79-
var currValue = field.value;
79+
var currValue = [...field.value];
8080
if (value)
8181
currValue.add(widget.options[i].value);
8282
else
@@ -102,65 +102,65 @@ class _FormBuilderCheckboxListState extends State<FormBuilderCheckboxList> {
102102
_readOnly = (_formState?.readOnly == true) ? true : widget.readOnly;
103103

104104
return FormField(
105-
key: _fieldKey,
106-
enabled: !_readOnly,
107-
initialValue: _initialValue ?? [],
108-
validator: (val) {
109-
for (int i = 0; i < widget.validators.length; i++) {
110-
if (widget.validators[i](val) != null)
111-
return widget.validators[i](val);
112-
}
113-
return null;
114-
},
115-
onSaved: (val) {
116-
var transformed;
117-
if (widget.valueTransformer != null) {
118-
transformed = widget.valueTransformer(val);
119-
_formState?.setAttributeValue(widget.attribute, transformed);
120-
} else
121-
_formState?.setAttributeValue(widget.attribute, val);
122-
if (widget.onSaved != null) {
123-
widget.onSaved(transformed ?? val);
124-
}
125-
},
126-
builder: (FormFieldState<dynamic> field) {
127-
List<Widget> checkboxList = [];
128-
for (int i = 0; i < widget.options.length; i++) {
129-
checkboxList.addAll([
130-
ListTile(
131-
dense: true,
132-
isThreeLine: false,
133-
contentPadding: EdgeInsets.all(0.0),
134-
leading: _leading(field, i),
135-
trailing: _trailing(field, i),
136-
title: widget.options[i],
137-
onTap: _readOnly
138-
? null
139-
: () {
140-
var currentValue = field.value;
141-
if (!currentValue.contains(widget.options[i].value))
142-
currentValue.add(widget.options[i].value);
143-
else
144-
currentValue.remove(widget.options[i].value);
145-
field.didChange(currentValue);
146-
if (widget.onChanged != null)
147-
widget.onChanged(currentValue);
148-
},
149-
),
150-
Divider(
151-
height: 0.0,
152-
),
153-
]);
154-
}
155-
return InputDecorator(
156-
decoration: widget.decoration.copyWith(
157-
enabled: !_readOnly,
158-
errorText: field.errorText,
105+
key: _fieldKey,
106+
enabled: !_readOnly,
107+
initialValue: _initialValue ?? [],
108+
validator: (val) {
109+
for (int i = 0; i < widget.validators.length; i++) {
110+
if (widget.validators[i](val) != null)
111+
return widget.validators[i](val);
112+
}
113+
return null;
114+
},
115+
onSaved: (val) {
116+
var transformed;
117+
if (widget.valueTransformer != null) {
118+
transformed = widget.valueTransformer(val);
119+
_formState?.setAttributeValue(widget.attribute, transformed);
120+
} else
121+
_formState?.setAttributeValue(widget.attribute, val);
122+
if (widget.onSaved != null) {
123+
widget.onSaved(transformed ?? val);
124+
}
125+
},
126+
builder: (FormFieldState<dynamic> field) {
127+
List<Widget> checkboxList = [];
128+
for (int i = 0; i < widget.options.length; i++) {
129+
checkboxList.addAll([
130+
ListTile(
131+
dense: true,
132+
isThreeLine: false,
133+
contentPadding: EdgeInsets.all(0.0),
134+
leading: _leading(field, i),
135+
trailing: _trailing(field, i),
136+
title: widget.options[i],
137+
onTap: _readOnly
138+
? null
139+
: () {
140+
var currentValue = [...field.value];
141+
if (!currentValue.contains(widget.options[i].value))
142+
currentValue.add(widget.options[i].value);
143+
else
144+
currentValue.remove(widget.options[i].value);
145+
field.didChange(currentValue);
146+
if (widget.onChanged != null)
147+
widget.onChanged(currentValue);
148+
},
159149
),
160-
child: Column(
161-
children: checkboxList,
162-
),
163-
);
164-
});
150+
Divider(height: 0.0),
151+
]);
152+
}
153+
return InputDecorator(
154+
decoration: widget.decoration.copyWith(
155+
enabled: !_readOnly,
156+
errorText: field.errorText,
157+
),
158+
child: Column(
159+
key: ObjectKey(field.value),
160+
children: checkboxList,
161+
),
162+
);
163+
},
164+
);
165165
}
166166
}

lib/src/fields/form_builder_chips_choice.dart

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class FormBuilderChoiceChip extends StatefulWidget {
1313
final FormFieldSetter onSaved;
1414
final ValueTransformer valueTransformer;
1515
final List<FormBuilderFieldOption> options;
16+
1617
// FilterChip Settings
1718
final double elevation, pressElevation;
1819
final Color selectedColor,
@@ -22,6 +23,7 @@ class FormBuilderChoiceChip extends StatefulWidget {
2223
shadowColor;
2324
final ShapeBorder shape;
2425
final MaterialTapTargetSize materialTapTargetSize;
26+
2527
// Wrap Settings
2628
final Axis direction;
2729
final WrapAlignment alignment;
@@ -116,46 +118,47 @@ class _FormBuilderChoiceChipState extends State<FormBuilderChoiceChip> {
116118
},
117119
builder: (FormFieldState<dynamic> field) {
118120
return InputDecorator(
119-
decoration: widget.decoration.copyWith(
120-
enabled: !_readOnly,
121-
errorText: field.errorText,
122-
),
123-
child: Wrap(
124-
direction: widget.direction,
125-
alignment: widget.alignment,
126-
crossAxisAlignment: widget.crossAxisAlignment,
127-
runAlignment: widget.runAlignment,
128-
runSpacing: widget.runSpacing,
129-
spacing: widget.spacing,
130-
textDirection: widget.textDirection,
131-
verticalDirection: widget.verticalDirection,
132-
children: <Widget>[
133-
for (FormBuilderFieldOption option in widget.options)
134-
ChoiceChip(
135-
selectedColor: widget.selectedColor,
136-
disabledColor: widget.disabledColor,
137-
backgroundColor: widget.backgroundColor,
138-
shadowColor: widget.shadowColor,
139-
selectedShadowColor: widget.selectedShadowColor,
140-
shape: widget.shape,
141-
elevation: widget.elevation,
142-
pressElevation: widget.pressElevation,
143-
materialTapTargetSize: widget.materialTapTargetSize,
144-
label: option.child,
145-
selected: field.value == option.value,
146-
onSelected: _readOnly
147-
? null
148-
: (bool selected) {
149-
setState(() {
150-
FocusScope.of(context)
151-
.requestFocus(FocusNode());
152-
var choice = selected ? option.value : null;
153-
field.didChange(choice);
154-
if (widget.onChanged != null)
155-
widget.onChanged(choice);
156-
});
157-
})
158-
]));
121+
decoration: widget.decoration.copyWith(
122+
enabled: !_readOnly,
123+
errorText: field.errorText,
124+
),
125+
child: Wrap(
126+
direction: widget.direction,
127+
alignment: widget.alignment,
128+
crossAxisAlignment: widget.crossAxisAlignment,
129+
runAlignment: widget.runAlignment,
130+
runSpacing: widget.runSpacing,
131+
spacing: widget.spacing,
132+
textDirection: widget.textDirection,
133+
verticalDirection: widget.verticalDirection,
134+
children: <Widget>[
135+
for (FormBuilderFieldOption option in widget.options)
136+
ChoiceChip(
137+
selectedColor: widget.selectedColor,
138+
disabledColor: widget.disabledColor,
139+
backgroundColor: widget.backgroundColor,
140+
shadowColor: widget.shadowColor,
141+
selectedShadowColor: widget.selectedShadowColor,
142+
shape: widget.shape,
143+
elevation: widget.elevation,
144+
pressElevation: widget.pressElevation,
145+
materialTapTargetSize: widget.materialTapTargetSize,
146+
label: option.child,
147+
selected: field.value == option.value,
148+
onSelected: _readOnly
149+
? null
150+
: (bool selected) {
151+
setState(() {
152+
FocusScope.of(context).requestFocus(FocusNode());
153+
var choice = selected ? option.value : null;
154+
field.didChange(choice);
155+
if (widget.onChanged != null)
156+
widget.onChanged(choice);
157+
});
158+
},
159+
)
160+
]),
161+
);
159162
},
160163
);
161164
}

lib/src/fields/form_builder_chips_filter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class _FormBuilderFilterChipState extends State<FormBuilderFilterChip> {
156156
setState(
157157
() {
158158
FocusScope.of(context).requestFocus(FocusNode());
159-
var currentValue = field.value;
159+
var currentValue = [...field.value];
160160

161161
if (selected)
162162
currentValue.add(option.value);

lib/src/fields/form_builder_chips_input.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class _FormBuilderChipsInputState extends State<FormBuilderChipsInput> {
109109
},
110110
builder: (FormFieldState<dynamic> field) {
111111
return ChipsInput(
112+
key: ObjectKey(field.value),
112113
initialValue: field.value,
113114
enabled: !_readOnly,
114115
decoration: widget.decoration.copyWith(

0 commit comments

Comments
 (0)