Skip to content

Commit bcc2f65

Browse files
committed
Added reset button (optional), doesn't reset all fields - needs fixing
1 parent 60a8043 commit bcc2f65

File tree

4 files changed

+86
-125
lines changed

4 files changed

+86
-125
lines changed

.idea/workspace.xml

Lines changed: 45 additions & 107 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/main.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class MyHomePage extends StatelessWidget {
2828
child: FormBuilder(
2929
context,
3030
autovalidate: true,
31+
// showResetButton: true,
32+
// resetButtonContent: Text("Clear Form"),
3133
controls: [
3234
FormBuilderInput.textField(
3335
type: FormBuilderInput.TYPE_TEXT,
@@ -181,7 +183,7 @@ class MyHomePage extends StatelessWidget {
181183
]),
182184
],
183185
onChanged: () {
184-
print("Form Changed");
186+
print("Form value changed");
185187
},
186188
onSubmit: (formValue) {
187189
if (formValue != null) {

lib/src/form_builder.dart

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ class FormBuilder extends StatefulWidget {
1313
final List<FormBuilderInput> controls;
1414
final Function onSubmit;
1515
final bool autovalidate;
16+
final bool showResetButton;
1617
final Widget submitButtonContent;
18+
final Widget resetButtonContent;
1719

1820
const FormBuilder(
1921
this.context, {
2022
@required this.controls,
2123
@required this.onSubmit,
2224
this.onChanged,
2325
this.autovalidate = false,
26+
this.showResetButton = false,
2427
this.onWillPop,
2528
this.submitButtonContent,
26-
});
29+
this.resetButtonContent,
30+
}): assert(resetButtonContent == null || showResetButton);
2731

2832
@override
2933
_FormBuilderState createState() => _FormBuilderState(controls);
@@ -41,7 +45,7 @@ class _FormBuilderState extends State<FormBuilder> {
4145
return Form(
4246
key: _formKey,
4347
onChanged: widget.onChanged,
44-
//TODO: Allow user to update field value based on changes in others (e.g. Summations)
48+
//TODO: Allow user to update field value or validate based on changes in others (e.g. Summations, Confirm Password)
4549
onWillPop: widget.onWillPop,
4650
autovalidate: widget.autovalidate,
4751
child: ListView(
@@ -193,7 +197,6 @@ class _FormBuilderState extends State<FormBuilder> {
193197
style: TextStyle(color: Colors.grey),
194198
),
195199
DropdownButton(
196-
//TODO: Is it possible to clear dropdown selection?
197200
isExpanded: true,
198201
hint: Text(formControl.hint ?? ''),
199202
items: formControls[count].options.map((option) {
@@ -700,19 +703,36 @@ class _FormBuilderState extends State<FormBuilder> {
700703

701704
formControlsList.add(Container(
702705
margin: EdgeInsets.only(top: 10.0),
703-
child: MaterialButton(
704-
color: Theme.of(context).accentColor,
705-
textColor: Colors.white,
706-
onPressed: () {
707-
if (_formKey.currentState.validate()) {
708-
_formKey.currentState.save();
709-
widget.onSubmit(formData);
710-
} else {
711-
debugPrint("Validation failed");
712-
widget.onSubmit(null);
713-
}
714-
},
715-
child: widget.submitButtonContent ?? Text('Submit'),
706+
child: Row(
707+
children: [
708+
widget.showResetButton ? Expanded(
709+
child: OutlineButton(
710+
borderSide: BorderSide(color: Theme.of(context).accentColor),
711+
textColor: Theme.of(context).accentColor,
712+
onPressed: () {
713+
_formKey.currentState.reset(); //FIXME: only resets TextField based inputs
714+
},
715+
child: widget.resetButtonContent ?? Text('Reset'),
716+
),
717+
) : SizedBox(),
718+
Expanded(
719+
child: MaterialButton(
720+
color: Theme.of(context).accentColor,
721+
textColor: Colors.white,
722+
onPressed: () {
723+
if (_formKey.currentState.validate()) {
724+
_formKey.currentState.save();
725+
widget.onSubmit(formData);
726+
} else {
727+
debugPrint("Validation failed");
728+
widget.onSubmit(null);
729+
}
730+
},
731+
child: widget.submitButtonContent ?? Text('Submit'),
732+
),
733+
),
734+
735+
],
716736
),
717737
));
718738

0 commit comments

Comments
 (0)