checkbox_formfield/lib/checkbox_icon_formfield.dart

52 lines
1.6 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,
bool autovalidate = false,
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,
autovalidate: autovalidate,
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!
? _createTappableIcon(state, enabled, trueIcon,
trueIconColor, disabledColor, iconSize)
: _createTappableIcon(state, enabled, falseIcon,
falseIconColor, disabledColor, iconSize));
},
);
static Widget _createTappableIcon(FormFieldState<bool> state, bool enabled,
2021-08-17 11:48:08 +00:00
IconData icon, Color? iconColor, Color? disabledColor, double? iconSize) {
return IconButton(
onPressed: enabled
? () {
2021-08-17 11:48:08 +00:00
state.didChange(!state.value!);
}
: null,
icon: Icon(icon,
color: enabled ? iconColor : disabledColor, size: iconSize),
);
}
}