checkbox_formfield/lib/checkbox_icon_formfield.dart

61 lines
1.8 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2020-04-22 15:38:03 +00:00
/// Use Icon as checkbox
class CheckboxIconFormField extends FormField<bool> {
CheckboxIconFormField({
2021-08-17 11:48:08 +00:00
Key? key,
BuildContext? context,
FormFieldSetter<bool>? onSaved,
bool initialValue = false,
2021-11-14 19:03:25 +00:00
ValueChanged<bool>? onChanged,
2021-10-23 12:43:43 +00:00
AutovalidateMode? autovalidateMode,
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,
double padding = 24.0,
2021-08-17 11:48:08 +00:00
double? iconSize,
}) : super(
key: key,
onSaved: onSaved,
initialValue: initialValue,
autovalidateMode: autovalidateMode,
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,
trueIconColor, disabledColor, iconSize)
2021-11-14 19:03:25 +00:00
: _createTappableIcon(state, enabled, falseIcon, onChanged,
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,
) {
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!);
}
: null,
icon: Icon(icon,
color: enabled ? iconColor : disabledColor, size: iconSize),
);
}
}