Add onChanged callback

fixes #2
This commit is contained in:
SimonIT 2021-11-14 20:03:25 +01:00
parent a8848b2ff3
commit 9e36a849ae
No known key found for this signature in database
GPG key ID: 8D07E7C11D5705EE
3 changed files with 35 additions and 6 deletions

View file

@ -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();

View file

@ -7,6 +7,7 @@ class CheckboxIconFormField extends FormField<bool> {
BuildContext? context,
FormFieldSetter<bool>? onSaved,
bool initialValue = false,
ValueChanged<bool>? onChanged,
AutovalidateMode autovalidateMode,
bool enabled = true,
IconData trueIcon = Icons.check,
@ -29,19 +30,27 @@ class CheckboxIconFormField extends FormField<bool> {
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<bool> state, bool enabled,
IconData icon, Color? iconColor, Color? disabledColor, double? iconSize) {
static Widget _createTappableIcon(
FormFieldState<bool> state,
bool enabled,
IconData icon,
ValueChanged<bool>? 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,

View file

@ -9,6 +9,7 @@ class CheckboxListTileFormField extends FormField<bool> {
FormFieldSetter<bool>? onSaved,
FormFieldValidator<bool>? validator,
bool initialValue = false,
ValueChanged<bool>? onChanged,
AutovalidateMode autovalidateMode,
bool enabled = true,
bool dense = false,
@ -33,7 +34,12 @@ class CheckboxListTileFormField extends FormField<bool> {
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!,