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({
  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(),
                ),
              ),
            ),
          );
        },
      );