TxSwitchFormField constructor

TxSwitchFormField({
  1. Key? key,
  2. FormFieldSetter<bool>? onSaved,
  3. FormFieldValidator<bool>? validator,
  4. bool? enabled,
  5. AutovalidateMode? autovalidateMode = AutovalidateMode.onUserInteraction,
  6. String? restorationId,
  7. InputDecoration? decoration,
  8. ValueChanged<bool?>? onChanged,
  9. bool? required,
  10. bool? initialValue,
  11. TextAlign? textAlign = TextAlign.end,
  12. Color? activeColor,
  13. Color? thumbColor,
  14. Color? trackColor,
  15. Color? onLabelColor,
  16. Color? offLabelColor,
  17. DragStartBehavior? dragStartBehavior,
  18. Color? focusColor,
  19. FocusNode? focusNode,
  20. ValueChanged<bool>? onFocusChange,
  21. bool? autofocus,
  22. bool? applyTheme,
  23. Widget? label,
  24. String? labelText,
  25. TextAlign? labelTextAlign,
  26. TextOverflow? labelOverflow,
  27. EdgeInsetsGeometry? padding,
  28. FieldActionsBuilder<bool>? actionsBuilder,
  29. TextStyle? labelStyle,
  30. double? horizontalGap,
  31. Color? tileColor,
  32. TxFormFieldBuilder<bool>? trailingBuilder,
  33. Widget? leading,
  34. VisualDensity? visualDensity,
  35. ShapeBorder? shape,
  36. Color? iconColor,
  37. Color? textColor,
  38. TextStyle? leadingAndTrailingTextStyle,
  39. GestureTapCallback? onTap,
  40. double? minLeadingWidth,
  41. bool? dense,
  42. bool? colon,
  43. double? minLabelWidth,
  44. double? minVerticalPadding,
})

Implementation

TxSwitchFormField({
  super.key,
  super.onSaved,
  FormFieldValidator<bool>? validator,
  super.enabled,
  super.autovalidateMode,
  super.restorationId,
  super.decoration,
  super.onChanged,
  super.required,
  super.initialValue,
  TextAlign? textAlign = TextAlign.end,
  Color? activeColor,
  Color? thumbColor,
  Color? trackColor,
  Color? onLabelColor,
  Color? offLabelColor,
  DragStartBehavior? dragStartBehavior,
  Color? focusColor,
  FocusNode? focusNode,
  ValueChanged<bool>? onFocusChange,
  bool? autofocus,
  bool? applyTheme,
  super.label,
  super.labelText,
  super.labelTextAlign,
  super.labelOverflow,
  super.padding,
  super.actionsBuilder,
  super.labelStyle,
  super.horizontalGap,
  super.tileColor,
  super.trailingBuilder,
  super.leading,
  super.visualDensity,
  super.shape,
  super.iconColor,
  super.textColor,
  super.leadingAndTrailingTextStyle,
  super.onTap,
  super.minLeadingWidth,
  super.dense,
  super.colon,
  super.minLabelWidth,
  super.minVerticalPadding,
}) : super(
        layoutDirection: Axis.horizontal,
        builder: (field) {
          final AlignmentGeometry align = switch (textAlign) {
            null => AlignmentDirectional.centerStart,
            TextAlign.left => Alignment.centerLeft,
            TextAlign.right => Alignment.centerRight,
            TextAlign.center => AlignmentDirectional.center,
            TextAlign.justify => AlignmentDirectional.center,
            TextAlign.start => AlignmentDirectional.centerStart,
            TextAlign.end => AlignmentDirectional.centerEnd,
          };

          final theme = Theme.of(field.context);

          return Align(
            alignment: align,
            child: CupertinoSwitch(
              value: field.value ?? false,
              onChanged: field.didChange,
              activeColor: activeColor ??
                  theme.switchTheme.trackColor
                      ?.resolve({MaterialState.selected}) ??
                  theme.colorScheme.primary,
              thumbColor: thumbColor,
              trackColor: trackColor,
              applyTheme: applyTheme,
              focusColor: focusColor,
              onLabelColor: onLabelColor,
              offLabelColor: offLabelColor,
              focusNode: focusNode,
              autofocus: autofocus ?? false,
              dragStartBehavior: dragStartBehavior ?? DragStartBehavior.start,
              onFocusChange: onFocusChange,
            ),
          );
        },
        validator: (bool? val) {
          if (required == true && val == null) {
            return '请选择';
          }
          if (validator != null) {
            return validator(val);
          }
          return null;
        },
      );