From 9e36a849aeb45b7fda2a7423b0d843adf250a793 Mon Sep 17 00:00:00 2001 From: SimonIT Date: Sun, 14 Nov 2021 20:03:25 +0100 Subject: [PATCH] Add onChanged callback fixes #2 --- example/lib/main.dart | 16 +++++++++++++++- lib/checkbox_icon_formfield.dart | 17 +++++++++++++---- lib/checkbox_list_tile_formfield.dart | 8 +++++++- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 1723978..75eded6 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -52,14 +52,28 @@ Widget createScaffold() { return 'False!'; } }, + onChanged: (value) { + if (value) { + print("ListTile Checked :)"); + } else { + print("ListTile Not Checked :("); + } + }, ), CheckboxIconFormField( initialValue: true, enabled: false, iconSize: 32, onSaved: (bool? value) {}, + onChanged: (value) { + if (value) { + print("Icon Checked :)"); + } else { + print("Icon Not Checked :("); + } + }, ), - RaisedButton( + ElevatedButton( onPressed: () { if (_formKey.currentState!.validate()) { _formKey.currentState!.save(); diff --git a/lib/checkbox_icon_formfield.dart b/lib/checkbox_icon_formfield.dart index b5f771a..5880d36 100644 --- a/lib/checkbox_icon_formfield.dart +++ b/lib/checkbox_icon_formfield.dart @@ -7,6 +7,7 @@ class CheckboxIconFormField extends FormField { BuildContext? context, FormFieldSetter? onSaved, bool initialValue = false, + ValueChanged? onChanged, AutovalidateMode autovalidateMode, bool enabled = true, IconData trueIcon = Icons.check, @@ -29,19 +30,27 @@ class CheckboxIconFormField extends FormField { return Padding( padding: EdgeInsets.all(padding), child: state.value! - ? _createTappableIcon(state, enabled, trueIcon, + ? _createTappableIcon(state, enabled, trueIcon, onChanged, trueIconColor, disabledColor, iconSize) - : _createTappableIcon(state, enabled, falseIcon, + : _createTappableIcon(state, enabled, falseIcon, onChanged, falseIconColor, disabledColor, iconSize)); }, ); - static Widget _createTappableIcon(FormFieldState state, bool enabled, - IconData icon, Color? iconColor, Color? disabledColor, double? iconSize) { + static Widget _createTappableIcon( + FormFieldState state, + bool enabled, + IconData icon, + ValueChanged? onChanged, + Color? iconColor, + Color? disabledColor, + double? iconSize, + ) { return IconButton( onPressed: enabled ? () { state.didChange(!state.value!); + if (onChanged != null) onChanged(state.value!); } : null, icon: Icon(icon, diff --git a/lib/checkbox_list_tile_formfield.dart b/lib/checkbox_list_tile_formfield.dart index e2d1f6d..9ba1866 100644 --- a/lib/checkbox_list_tile_formfield.dart +++ b/lib/checkbox_list_tile_formfield.dart @@ -9,6 +9,7 @@ class CheckboxListTileFormField extends FormField { FormFieldSetter? onSaved, FormFieldValidator? validator, bool initialValue = false, + ValueChanged? onChanged, AutovalidateMode autovalidateMode, bool enabled = true, bool dense = false, @@ -33,7 +34,12 @@ class CheckboxListTileFormField extends FormField { activeColor: activeColor, checkColor: checkColor, value: state.value, - onChanged: enabled ? state.didChange : null, + onChanged: enabled + ? (value) { + state.didChange(value); + if (onChanged != null) onChanged(value!); + } + : null, subtitle: state.hasError ? Text( state.errorText!,