ChoiceChipField<T> constructor
ChoiceChipField<T> ({
- String? labelPrefix,
- String? label,
- Widget? labelWidget,
- ChoiceChipFieldController<
T> ? controller, - FormFieldValidator<
Set< ? validator,T> ?> - FormFieldSetter<
Set< ? onSaved,T> > - Set<
T> ? initialValue, - Map<
T, ChipEntry> ? items, - bool enabled = true,
- AutovalidateMode autoValidateMode = AutovalidateMode.disabled,
- dynamic onChanged(
- T? value, {
- required bool selected,
- bool filled = false,
- Color? fillColor,
- Color? focusColor,
- bool? showCheckMark,
- InputDecoration? decoration,
- EdgeInsets padding = const EdgeInsets.all(8),
- WrapAlignment wrapAlignment = WrapAlignment.spaceEvenly,
- WrapCrossAlignment wrapCrossAlignment = WrapCrossAlignment.center,
- String? hintText,
- EdgeInsets? contentPadding,
- Widget? prefix,
- Widget? suffix,
- EdgeInsets chipExternalPadding = EdgeInsets.zero,
- bool multiple = false,
- int? sizeExtraSmall,
- int? sizeSmall,
- int? sizeMedium,
- int? sizeLarge,
- int? sizeExtraLarge,
- double? minHeight,
- Key? key,
Implementation
ChoiceChipField({
String? labelPrefix,
String? label,
Widget? labelWidget,
this.controller,
FormFieldValidator<Set<T>?>? validator,
super.onSaved,
Set<T>? initialValue,
this.items,
super.enabled,
AutovalidateMode autoValidateMode = AutovalidateMode.disabled,
Function(T? value, {required bool selected})? onChanged,
bool filled = false,
Color? fillColor,
Color? focusColor,
bool? showCheckMark,
InputDecoration? decoration,
EdgeInsets padding = const EdgeInsets.all(8),
WrapAlignment wrapAlignment = WrapAlignment.spaceEvenly,
WrapCrossAlignment wrapCrossAlignment = WrapCrossAlignment.center,
String? hintText,
EdgeInsets? contentPadding,
Widget? prefix,
Widget? suffix,
EdgeInsets chipExternalPadding = EdgeInsets.zero,
bool multiple = false,
super.sizeExtraSmall,
super.sizeSmall,
super.sizeMedium,
super.sizeLarge,
super.sizeExtraLarge,
super.minHeight,
super.key,
}) : assert(
initialValue == null || controller == null,
'initialValue or controller must be null.',
),
assert(
label == null || labelWidget == null,
'label or labelWidget must be null.',
),
super(
initialValue: controller != null ? controller.value : initialValue,
validator: enabled ? validator : (_) => null,
autovalidateMode: autoValidateMode,
builder: (FormFieldState<Set<T>?> field) {
_ChoiceChipFieldState<T> state = field as _ChoiceChipFieldState<T>;
InputDecoration effectiveDecoration = (decoration ??
InputDecoration(
border: const OutlineInputBorder(),
filled: filled,
fillColor: fillColor,
label: labelWidget,
labelText: (labelPrefix?.isEmpty ?? true)
? label
: '$labelPrefix - $label',
counterText: '',
focusColor: focusColor,
hintText: hintText,
contentPadding: contentPadding,
prefix: prefix,
suffix: suffix,
))
.applyDefaults(Theme.of(field.context).inputDecorationTheme);
return Padding(
padding: padding,
child: InputDecorator(
decoration: effectiveDecoration.copyWith(
errorText: enabled ? field.errorText : null,
enabled: enabled,
),
child: ValueListenableBuilder<Set<T>>(
valueListenable: state._effectiveController,
builder: (BuildContext context, Set<T> value, _) => Wrap(
alignment: wrapAlignment,
crossAxisAlignment: wrapCrossAlignment,
children:
state._effectiveController.items!.entries.map<Widget>(
(MapEntry<T, ChipEntry> e) {
bool selected = value.contains(e.key);
Color? labelColor = _getLabelColor(
context: context,
entry: e.value,
selected: selected,
);
return Padding(
padding: chipExternalPadding,
child: FilterChip(
label: Text(e.value.label),
padding: e.value.padding,
backgroundColor: e.value.color,
selected: selected,
selectedColor: e.value.selectedColor ??
Theme.of(state.context).colorScheme.primary,
showCheckmark: showCheckMark,
checkmarkColor: labelColor,
labelStyle: TextStyle(
color: labelColor,
),
onSelected: (bool selected) {
state.update(
e.key,
selected: selected,
multiple: multiple,
);
if (onChanged != null) {
onChanged(e.key, selected: selected);
}
},
),
);
},
).toList(),
),
),
),
);
},
);