ChoiceChipField<T> constructor

ChoiceChipField<T>({
  1. String? labelPrefix,
  2. String? label,
  3. Widget? labelWidget,
  4. ChoiceChipFieldController<T>? controller,
  5. FormFieldValidator<Set<T>?>? validator,
  6. FormFieldSetter<Set<T>>? onSaved,
  7. Set<T>? initialValue,
  8. Map<T, ChipEntry>? items,
  9. bool enabled = true,
  10. AutovalidateMode autoValidateMode = AutovalidateMode.disabled,
  11. dynamic onChanged(
    1. T? value, {
    2. required bool selected,
    })?,
  12. bool filled = false,
  13. Color? fillColor,
  14. Color? focusColor,
  15. bool? showCheckMark,
  16. InputDecoration? decoration,
  17. EdgeInsets padding = const EdgeInsets.all(8),
  18. WrapAlignment wrapAlignment = WrapAlignment.spaceEvenly,
  19. WrapCrossAlignment wrapCrossAlignment = WrapCrossAlignment.center,
  20. String? hintText,
  21. EdgeInsets? contentPadding,
  22. Widget? prefix,
  23. Widget? suffix,
  24. EdgeInsets chipExternalPadding = EdgeInsets.zero,
  25. bool multiple = false,
  26. int? sizeExtraSmall,
  27. int? sizeSmall,
  28. int? sizeMedium,
  29. int? sizeLarge,
  30. int? sizeExtraLarge,
  31. double? minHeight,
  32. Key? key,
})

Implementation

ChoiceChipField({
  final String? labelPrefix,
  final String? label,
  final Widget? labelWidget,
  this.controller,
  final FormFieldValidator<Set<T>?>? validator,
  super.onSaved,
  final Set<T>? initialValue,
  this.items,
  super.enabled,
  final AutovalidateMode autoValidateMode = AutovalidateMode.disabled,
  final Function(T? value, {required bool selected})? onChanged,
  final bool filled = false,
  final Color? fillColor,
  final Color? focusColor,
  final bool? showCheckMark,
  final InputDecoration? decoration,
  final EdgeInsets padding = const EdgeInsets.all(8),
  final WrapAlignment wrapAlignment = WrapAlignment.spaceEvenly,
  final WrapCrossAlignment wrapCrossAlignment = WrapCrossAlignment.center,
  final String? hintText,
  final EdgeInsets? contentPadding,
  final Widget? prefix,
  final Widget? suffix,
  final EdgeInsets chipExternalPadding = EdgeInsets.zero,
  final 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: (final 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: (final BuildContext context, final Set<T> value, _) =>
                   Wrap(
                     alignment: wrapAlignment,
                     crossAxisAlignment: wrapCrossAlignment,
                     children: state._effectiveController.items!.entries
                         .map<Widget>((final 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: (final bool selected) {
                                 state.update(
                                   e.key,
                                   selected: selected,
                                   multiple: multiple,
                                 );
                                 if (onChanged != null) {
                                   onChanged(e.key, selected: selected);
                                 }
                               },
                             ),
                           );
                         })
                         .toList(),
                   ),
             ),
           ),
         );
       },
     );