TxSwitchFormField constructor
TxSwitchFormField({
- Key? key,
- FormFieldSetter<
bool> ? onSaved, - FormFieldValidator<
bool> ? validator, - bool? enabled,
- AutovalidateMode? autovalidateMode = AutovalidateMode.onUserInteraction,
- String? restorationId,
- InputDecoration? decoration,
- ValueChanged<
bool?> ? onChanged, - bool? required,
- bool? 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,
- Widget? label,
- String? labelText,
- TextAlign? labelTextAlign,
- TextOverflow? labelOverflow,
- EdgeInsetsGeometry? padding,
- FieldActionsBuilder<
bool> ? actionsBuilder, - TextStyle? labelStyle,
- double? horizontalGap,
- Color? tileColor,
- TxFormFieldBuilder<
bool> ? trailingBuilder, - Widget? leading,
- VisualDensity? visualDensity,
- ShapeBorder? shape,
- Color? iconColor,
- Color? textColor,
- TextStyle? leadingAndTrailingTextStyle,
- GestureTapCallback? onTap,
- double? minLeadingWidth,
- bool? dense,
- bool? colon,
- double? minLabelWidth,
- 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;
},
);