Merge pull request #6 from SimonIT/OnChanged_missing

Add onChanged callback
This commit is contained in:
reidha 2021-11-28 21:14:46 +08:00 committed by GitHub
commit e73e604177
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 6 deletions

View file

@ -52,14 +52,28 @@ Widget createScaffold() {
return 'False!'; return 'False!';
} }
}, },
onChanged: (value) {
if (value) {
print("ListTile Checked :)");
} else {
print("ListTile Not Checked :(");
}
},
), ),
CheckboxIconFormField( CheckboxIconFormField(
initialValue: true, initialValue: true,
enabled: false, enabled: false,
iconSize: 32, iconSize: 32,
onSaved: (bool? value) {}, onSaved: (bool? value) {},
onChanged: (value) {
if (value) {
print("Icon Checked :)");
} else {
print("Icon Not Checked :(");
}
},
), ),
RaisedButton( ElevatedButton(
onPressed: () { onPressed: () {
if (_formKey.currentState!.validate()) { if (_formKey.currentState!.validate()) {
_formKey.currentState!.save(); _formKey.currentState!.save();

View file

@ -7,6 +7,7 @@ class CheckboxIconFormField extends FormField<bool> {
BuildContext? context, BuildContext? context,
FormFieldSetter<bool>? onSaved, FormFieldSetter<bool>? onSaved,
bool initialValue = false, bool initialValue = false,
ValueChanged<bool>? onChanged,
AutovalidateMode? autovalidateMode, AutovalidateMode? autovalidateMode,
bool enabled = true, bool enabled = true,
IconData trueIcon = Icons.check, IconData trueIcon = Icons.check,
@ -29,19 +30,27 @@ class CheckboxIconFormField extends FormField<bool> {
return Padding( return Padding(
padding: EdgeInsets.all(padding), padding: EdgeInsets.all(padding),
child: state.value! child: state.value!
? _createTappableIcon(state, enabled, trueIcon, ? _createTappableIcon(state, enabled, trueIcon, onChanged,
trueIconColor, disabledColor, iconSize) trueIconColor, disabledColor, iconSize)
: _createTappableIcon(state, enabled, falseIcon, : _createTappableIcon(state, enabled, falseIcon, onChanged,
falseIconColor, disabledColor, iconSize)); falseIconColor, disabledColor, iconSize));
}, },
); );
static Widget _createTappableIcon(FormFieldState<bool> state, bool enabled, static Widget _createTappableIcon(
IconData icon, Color? iconColor, Color? disabledColor, double? iconSize) { FormFieldState<bool> state,
bool enabled,
IconData icon,
ValueChanged<bool>? onChanged,
Color? iconColor,
Color? disabledColor,
double? iconSize,
) {
return IconButton( return IconButton(
onPressed: enabled onPressed: enabled
? () { ? () {
state.didChange(!state.value!); state.didChange(!state.value!);
if (onChanged != null) onChanged(state.value!);
} }
: null, : null,
icon: Icon(icon, icon: Icon(icon,

View file

@ -9,6 +9,7 @@ class CheckboxListTileFormField extends FormField<bool> {
FormFieldSetter<bool>? onSaved, FormFieldSetter<bool>? onSaved,
FormFieldValidator<bool>? validator, FormFieldValidator<bool>? validator,
bool initialValue = false, bool initialValue = false,
ValueChanged<bool>? onChanged,
AutovalidateMode? autovalidateMode, AutovalidateMode? autovalidateMode,
bool enabled = true, bool enabled = true,
bool dense = false, bool dense = false,
@ -33,7 +34,12 @@ class CheckboxListTileFormField extends FormField<bool> {
activeColor: activeColor, activeColor: activeColor,
checkColor: checkColor, checkColor: checkColor,
value: state.value, value: state.value,
onChanged: enabled ? state.didChange : null, onChanged: enabled
? (value) {
state.didChange(value);
if (onChanged != null) onChanged(value!);
}
: null,
subtitle: state.hasError subtitle: state.hasError
? Text( ? Text(
state.errorText!, state.errorText!,