2020-04-12 14:08:32 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
2020-04-22 15:38:03 +00:00
|
|
|
/// Use Icon as checkbox
|
2020-04-12 14:08:32 +00:00
|
|
|
class CheckboxIconFormField extends FormField<bool> {
|
|
|
|
CheckboxIconFormField({
|
2021-08-17 11:48:08 +00:00
|
|
|
Key? key,
|
|
|
|
BuildContext? context,
|
|
|
|
FormFieldSetter<bool>? onSaved,
|
2020-04-12 14:08:32 +00:00
|
|
|
bool initialValue = false,
|
2021-11-14 19:03:25 +00:00
|
|
|
ValueChanged<bool>? onChanged,
|
2021-10-23 12:43:43 +00:00
|
|
|
AutovalidateMode? autovalidateMode,
|
2020-04-12 14:08:32 +00:00
|
|
|
bool enabled = true,
|
|
|
|
IconData trueIcon = Icons.check,
|
|
|
|
IconData falseIcon = Icons.check_box_outline_blank,
|
2021-08-17 11:48:08 +00:00
|
|
|
Color? trueIconColor,
|
|
|
|
Color? falseIconColor,
|
|
|
|
Color? disabledColor,
|
2020-04-12 14:08:32 +00:00
|
|
|
double padding = 24.0,
|
2021-08-17 11:48:08 +00:00
|
|
|
double? iconSize,
|
2020-04-12 14:08:32 +00:00
|
|
|
}) : super(
|
|
|
|
key: key,
|
|
|
|
onSaved: onSaved,
|
|
|
|
initialValue: initialValue,
|
2021-09-06 12:45:53 +00:00
|
|
|
autovalidateMode: autovalidateMode,
|
2020-04-12 14:08:32 +00:00
|
|
|
builder: (FormFieldState<bool> state) {
|
|
|
|
trueIconColor ??= (context == null
|
|
|
|
? null
|
|
|
|
: Theme.of(context).accentIconTheme.color);
|
|
|
|
|
|
|
|
return Padding(
|
|
|
|
padding: EdgeInsets.all(padding),
|
2021-08-17 11:48:08 +00:00
|
|
|
child: state.value!
|
2021-11-14 19:03:25 +00:00
|
|
|
? _createTappableIcon(state, enabled, trueIcon, onChanged,
|
2020-04-12 14:08:32 +00:00
|
|
|
trueIconColor, disabledColor, iconSize)
|
2021-11-14 19:03:25 +00:00
|
|
|
: _createTappableIcon(state, enabled, falseIcon, onChanged,
|
2020-04-12 14:08:32 +00:00
|
|
|
falseIconColor, disabledColor, iconSize));
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-14 19:03:25 +00:00
|
|
|
static Widget _createTappableIcon(
|
|
|
|
FormFieldState<bool> state,
|
|
|
|
bool enabled,
|
|
|
|
IconData icon,
|
|
|
|
ValueChanged<bool>? onChanged,
|
|
|
|
Color? iconColor,
|
|
|
|
Color? disabledColor,
|
|
|
|
double? iconSize,
|
|
|
|
) {
|
2020-04-12 14:08:32 +00:00
|
|
|
return IconButton(
|
|
|
|
onPressed: enabled
|
|
|
|
? () {
|
2021-08-17 11:48:08 +00:00
|
|
|
state.didChange(!state.value!);
|
2021-11-14 19:03:25 +00:00
|
|
|
if (onChanged != null) onChanged(state.value!);
|
2020-04-12 14:08:32 +00:00
|
|
|
}
|
|
|
|
: null,
|
|
|
|
icon: Icon(icon,
|
|
|
|
color: enabled ? iconColor : disabledColor, size: iconSize),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|