FormeFilterChip<T extends Object> constructor

FormeFilterChip<T extends Object>({
  1. Key? key,
  2. String? name,
  3. List<T> initialValue = const [],
  4. FormeAsyncValidator<List<T>>? asyncValidator,
  5. Duration? asyncValidatorDebounce,
  6. AutovalidateMode? autovalidateMode,
  7. bool enabled = true,
  8. FocusNode? focusNode,
  9. FormeFieldInitialized<List<T>>? onInitialized,
  10. FormeFieldSetter<List<T>>? onSaved,
  11. FormeFieldStatusChanged<List<T>>? onStatusChanged,
  12. int? order,
  13. bool quietlyValidate = false,
  14. bool readOnly = false,
  15. bool requestFocusOnUserInteraction = true,
  16. FormeFieldValidationFilter<List<T>>? validationFilter,
  17. FormeValidator<List<T>>? validator,
  18. FormeFieldDecorator<List<T>>? decorator,
  19. required List<FormeChipItem<T>> items,
  20. int? maxSelectedCount,
  21. WrapAlignment alignment = WrapAlignment.start,
  22. ChipThemeData? chipThemeData,
  23. WrapCrossAlignment crossAxisAlignment = WrapCrossAlignment.start,
  24. InputDecoration? decoration,
  25. Axis direction = Axis.horizontal,
  26. VoidCallback? maxSelectedExceedCallback,
  27. EdgeInsets? padding,
  28. WrapAlignment runAlignment = WrapAlignment.start,
  29. double runSpacing = 0.0,
  30. double spacing = 0.0,
  31. TextDirection? textDirection,
  32. VerticalDirection verticalDirection = VerticalDirection.down,
})

Implementation

FormeFilterChip({
  super.key,
  super.name,
  super.initialValue = const [],
  super.asyncValidator,
  super.asyncValidatorDebounce,
  super.autovalidateMode,
  super.enabled = true,
  super.focusNode,
  super.onInitialized,
  super.onSaved,
  super.onStatusChanged,
  super.order,
  super.quietlyValidate = false,
  super.readOnly = false,
  super.requestFocusOnUserInteraction = true,
  super.validationFilter,
  super.validator,
  FormeFieldDecorator<List<T>>? decorator,
  required this.items,
  this.maxSelectedCount,
  this.alignment = WrapAlignment.start,
  this.chipThemeData,
  this.crossAxisAlignment = WrapCrossAlignment.start,
  this.decoration,
  this.direction = Axis.horizontal,
  this.maxSelectedExceedCallback,
  this.padding,
  this.runAlignment = WrapAlignment.start,
  this.runSpacing = 0.0,
  this.spacing = 0.0,
  this.textDirection,
  this.verticalDirection = VerticalDirection.down,
}) : super.allFields(
        decorator: decorator ??
            (decoration == null
                ? null
                : FormeInputDecorationDecorator(
                    decorationBuilder: (context) => decoration,
                    maxLength: maxSelectedCount,
                    childBuilder: (context, child) => Padding(
                      padding: padding ??
                          const EdgeInsets.symmetric(
                              vertical: 5, horizontal: 8),
                      child: child,
                    ),
                  )),
        builder: (state) {
          final bool readOnly = state.readOnly;

          final List<Widget> chips = [];
          for (final FormeChipItem<T> item in items) {
            final bool isReadOnly = readOnly || item.readOnly;
            final FilterChip chip = FilterChip(
              iconTheme: item.iconTheme,
              surfaceTintColor: item.surfaceTintColor,
              clipBehavior: item.clipBehavior,
              selected: state.value.contains(item.data),
              label: item.label,
              avatar: item.avatar,
              padding: item.padding,
              pressElevation: item.pressElevation,
              tooltip: item.tooltip,
              materialTapTargetSize: item.materialTapTargetSize,
              avatarBorder: item.avatarBorder,
              backgroundColor: item.backgroundColor,
              checkmarkColor: item.checkmarkColor,
              showCheckmark: item.showCheckmark,
              shadowColor: item.shadowColor,
              disabledColor: item.disabledColor,
              selectedColor: item.selectedColor,
              selectedShadowColor: item.selectedShadowColor,
              visualDensity: item.visualDensity,
              elevation: item.elevation,
              labelPadding: item.labelPadding,
              labelStyle: item.labelStyle,
              shape: item.shape,
              side: item.side,
              onSelected: isReadOnly
                  ? null
                  : (bool selected) {
                      final List<T> value = List.of(state.value);
                      if (selected) {
                        if (maxSelectedCount != null &&
                            value.length >= maxSelectedCount) {
                          if (maxSelectedExceedCallback != null) {
                            maxSelectedExceedCallback();
                          }
                          return;
                        }
                        state.didChange(value..add(item.data));
                      } else {
                        state.didChange(value..remove(item.data));
                      }
                      state.requestFocusOnUserInteraction();
                    },
            );
            chips.add(Visibility(
                visible: item.visible,
                child: Padding(
                  padding: item.padding,
                  child: chip,
                )));
          }

          final Widget chipWidget = Wrap(
            spacing: spacing,
            runSpacing: runSpacing,
            textDirection: textDirection,
            crossAxisAlignment: crossAxisAlignment,
            verticalDirection: verticalDirection,
            alignment: alignment,
            direction: direction,
            runAlignment: runAlignment,
            children: chips,
          );
          return Focus(
            focusNode: state.focusNode,
            child: ChipTheme(
              data: chipThemeData ?? ChipTheme.of(state.context),
              child: chipWidget,
            ),
          );
        },
      );