checkbox_formfield/lib/checkbox_list_tile_formfield.dart
2024-05-27 10:05:17 +02:00

57 lines
1.8 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,
EdgeInsetsGeometry? contentPadding,
bool autofocus = false,
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).colorScheme.error);
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,
contentPadding: contentPadding,
autofocus: autofocus,
);
},
);
}