CheckboxSuperFormField<T>.listTile constructor

CheckboxSuperFormField<T>.listTile({
  1. Key? key,
  2. required String name,
  3. required List<CheckboxOption<T>> options,
  4. List<SuperFormFieldRule>? rules,
  5. Color? activeColor,
  6. Color? checkColor,
  7. Color? tileColor,
  8. Widget subtitle(
    1. CheckboxOption<T> state
    )?,
  9. bool isThreeLine = false,
  10. bool? dense,
  11. Widget? secondary,
  12. bool selected(
    1. CheckboxOption<T> option
    )?,
  13. ListTileControlAffinity controlAffinity = ListTileControlAffinity.platform,
  14. bool autofocus = false,
  15. EdgeInsetsGeometry? contentPadding,
  16. ShapeBorder? shape,
  17. Color? selectedTileColor,
  18. void onChanged(
    1. T value,
    2. bool checked
    )?,
  19. bool? enabled,
})

Creates a Column of connected CheckboxListTiles which represent the options.

Check CheckboxListTile documentation for arguments documentation.

Diffences between CheckboxListTile and this builder are:

  • No tristate available
  • subtitle is a function so developers can customize it per option
  • selected is a function so developers can customize it per option

Implementation

CheckboxSuperFormField.listTile({
  Key? key,
  required String name,
  required this.options,
  List<SuperFormFieldRule>? rules,
  Color? activeColor,
  Color? checkColor,
  Color? tileColor,
  Widget Function(CheckboxOption<T> state)? subtitle,
  bool isThreeLine = false,
  bool? dense,
  Widget? secondary,
  bool Function(CheckboxOption<T> option)? selected,
  ListTileControlAffinity controlAffinity = ListTileControlAffinity.platform,
  bool autofocus = false,
  EdgeInsetsGeometry? contentPadding,
  ShapeBorder? shape,
  Color? selectedTileColor,
  void Function(T value, bool checked)? onChanged,
  this.enabled,
}) : super(
        key: key,
        name: name,
        rules: rules ?? [],
        builder: (context, fieldState, formState) {
          final List<T> currentValue =
              List.from(fieldState.data?.value as List? ?? []);

          void effectiveOnChanged(T value, bool checked) {
            List<T> newValue = currentValue;

            if (checked) {
              if (!currentValue.contains(value)) {
                newValue = <T>[...currentValue, value];
              }
            } else {
              newValue = currentValue
                  .where((element) => element != value)
                  .toList(growable: false);
            }

            SuperFormFieldData newData = fieldState.data!.copyWithValue(
              value: newValue,
              touched: true,
            );

            // If the field was tried to be submitted it should be now revalidated every change
            if (formState.validationMode == ValidationMode.onChange ||
                newData.submitted) {
              newData = newData.validate(rules ?? []);
            }

            formState.updateFieldData(newData);

            if (onChanged != null) {
              onChanged(value, checked);
            }
          }

          final effectiveEnabled = enabled ?? formState.enabled;

          return listTileCheckboxBuilder(
            context,
            CheckboxState<T>(
              options,
              currentValue,
              effectiveEnabled ? effectiveOnChanged : null,
              fieldState.focusNode,
            ),
            activeColor: activeColor,
            checkColor: checkColor,
            tileColor: tileColor,
            subtitle: subtitle,
            isThreeLine: isThreeLine,
            dense: dense,
            secondary: secondary,
            selected: selected,
            controlAffinity: controlAffinity,
            autofocus: autofocus,
            contentPadding: contentPadding,
            shape: shape,
            selectedTileColor: selectedTileColor,
          );
        },
      );