TxSegmentedFormField<T, V> constructor

TxSegmentedFormField<T, V>({
  1. required List<T> source,
  2. required ValueMapper<T, String?> labelMapper,
  3. ValueMapper<T, V?>? valueMapper,
  4. ValueMapper<T, bool>? disabledWhen,
  5. IndexedDataWidgetBuilder<T>? iconBuilder,
  6. ValueMapper<T, String>? tooltipMapper,
  7. ButtonStyle? buttonStyle,
  8. bool? showSelectedIcon,
  9. bool? emptySelectionAllowed,
  10. Widget? selectedIcon,
  11. T? initialData,
  12. V? initialValue,
  13. TextAlign? textAlign,
  14. Key? key,
  15. FormFieldSetter<T>? onSaved,
  16. FormFieldValidator<T>? validator,
  17. bool? enabled,
  18. AutovalidateMode? autovalidateMode = AutovalidateMode.onUserInteraction,
  19. String? restorationId,
  20. InputDecoration? decoration,
  21. ValueChanged<T?>? onChanged,
  22. bool? required,
  23. bool? bordered = false,
  24. Widget? label,
  25. String? labelText,
  26. TextAlign? labelTextAlign,
  27. TextOverflow? labelOverflow,
  28. EdgeInsetsGeometry? padding,
  29. FieldActionsBuilder<T>? actionsBuilder,
  30. TextStyle? labelStyle,
  31. double? horizontalGap,
  32. Color? tileColor,
  33. Axis? layoutDirection,
  34. TxFormFieldBuilder<T>? trailingBuilder,
  35. Widget? leading,
  36. VisualDensity? visualDensity,
  37. ShapeBorder? shape,
  38. Color? iconColor,
  39. Color? textColor,
  40. TextStyle? leadingAndTrailingTextStyle,
  41. GestureTapCallback? onTap,
  42. double? minLeadingWidth,
  43. double? minLabelWidth,
  44. double? minVerticalPadding,
  45. bool? dense,
  46. bool? colon,
  47. Color? focusColor,
})

Implementation

TxSegmentedFormField({
  required List<T> source,
  required ValueMapper<T, String?> labelMapper,
  ValueMapper<T, V?>? valueMapper,
  ValueMapper<T, bool>? disabledWhen,
  IndexedDataWidgetBuilder<T>? iconBuilder,
  ValueMapper<T, String>? tooltipMapper,
  ButtonStyle? buttonStyle,
  bool? showSelectedIcon,
  bool? emptySelectionAllowed,
  Widget? selectedIcon,
  T? initialData,
  V? initialValue,
  super.textAlign,
  // super.focusNode,
  super.key,
  super.onSaved,
  FormFieldValidator<T>? validator,
  super.enabled,
  super.autovalidateMode,
  super.restorationId,
  super.decoration,
  super.onChanged,
  super.required,
  super.bordered = false,
  super.label,
  super.labelText,
  super.labelTextAlign,
  super.labelOverflow,
  super.padding,
  super.actionsBuilder,
  super.labelStyle,
  super.horizontalGap,
  super.tileColor,
  Axis? layoutDirection,
  super.trailingBuilder,
  super.leading,
  super.visualDensity,
  super.shape,
  super.iconColor,
  super.textColor,
  super.leadingAndTrailingTextStyle,
  super.onTap,
  super.minLeadingWidth,
  super.minLabelWidth,
  super.minVerticalPadding,
  super.dense,
  super.colon,
  super.focusColor,
}) : super(
        layoutDirection: layoutDirection ?? Axis.horizontal,
        builder: (field) {
          final AlignmentGeometry align = switch (textAlign) {
            null => AlignmentDirectional.centerEnd,
            TextAlign.left => Alignment.centerLeft,
            TextAlign.right => Alignment.centerRight,
            TextAlign.center => AlignmentDirectional.center,
            TextAlign.justify => AlignmentDirectional.center,
            TextAlign.start => AlignmentDirectional.centerStart,
            TextAlign.end => AlignmentDirectional.centerEnd,
          };

          return Align(
            alignment: align,
            child: SegmentedButton<T>(
              segments: List.generate(source.length, (index) {
                final data = source[index];

                return ButtonSegment<T>(
                  value: data,
                  label: Text(labelMapper(data) ?? ''),
                  icon: iconBuilder == null
                      ? null
                      : iconBuilder(field.context, index, data),
                  tooltip: tooltipMapper == null ? null : tooltipMapper(data),
                  enabled: disabledWhen == null ? true : !disabledWhen(data),
                );
              }),
              style: buttonStyle,
              showSelectedIcon: showSelectedIcon ?? false,
              selectedIcon: selectedIcon,
              multiSelectionEnabled: false,
              emptySelectionAllowed: emptySelectionAllowed ?? true,
              selected: {if (field.value != null) field.value!},
              onSelectionChanged: field.isEnabled
                  ? (data) =>
                      field.didChange(data.isEmpty ? null : data.first)
                  : null,
            ),
          );
        },
        initialValue: TxPickerFormField.initData<T, V>(
          source,
          initialData,
          initialValue,
          valueMapper,
        ),
        validator: (value) => TxPickerFormField.generateValidator<T>(
          value,
          validator,
          required,
        ),
      );