checkbox_formfield/lib/checkbox_list_tile_formfield.dart

54 lines
1.7 KiB
Dart

import 'package:flutter/material.dart';
/// Use CheckboxListTile as part of Form
class CheckboxListTileFormField extends FormField<bool> {
CheckboxListTileFormField({
Key? key,
Widget? title,
BuildContext? context,
FormFieldSetter<bool>? onSaved,
FormFieldValidator<bool>? validator,
bool initialValue = false,
ValueChanged<bool>? onChanged,
AutovalidateMode? autovalidateMode,
bool enabled = true,
bool dense = false,
Color? errorColor,
Color? activeColor,
Color? checkColor,
ListTileControlAffinity controlAffinity = ListTileControlAffinity.leading,
Widget? secondary,
}) : super(
key: key,
onSaved: onSaved,
validator: validator,
initialValue: initialValue,
autovalidateMode: autovalidateMode,
builder: (FormFieldState<bool> state) {
errorColor ??=
(context == null ? Colors.red : Theme.of(context).errorColor);
return CheckboxListTile(
title: title,
dense: dense,
activeColor: activeColor,
checkColor: checkColor,
value: state.value,
onChanged: enabled
? (value) {
state.didChange(value);
if (onChanged != null) onChanged(value!);
}
: null,
subtitle: state.hasError
? Text(
state.errorText!,
style: TextStyle(color: errorColor),
)
: null,
controlAffinity: controlAffinity,
secondary: secondary,
);
},
);
}