import 'package:baseproject/core/components/form/form_builder_field.dart'; import 'package:baseproject/core/theme/custom_color.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; /// On/Off switch field class CustomSwitch extends FormBuilderField { /// A widget to display on the opposite side of the tile from the switch. /// /// Typically an [Icon] widget. final Widget? secondary; /// The color to use when this switch is on. /// /// Defaults to [ThemeData.toggleableActiveColor]. final Color? activeColor; /// The color to use on the track when this switch is on. /// /// Defaults to [ThemeData.toggleableActiveColor] with the opacity set at 50%. /// /// Ignored if this switch is created with [Switch.adaptive]. final Color? activeTrackColor; /// The color to use on the thumb when this switch is off. /// /// Defaults to the colors described in the Material design specification. /// /// Ignored if this switch is created with [Switch.adaptive]. final Color? inactiveThumbColor; /// The color to use on the track when this switch is off. /// /// Defaults to the colors described in the Material design specification. /// /// Ignored if this switch is created with [Switch.adaptive]. final Color? inactiveTrackColor; /// The tile's internal padding. /// /// Insets a [SwitchListTile]'s contents: its [title], [subtitle], /// [secondary], and [Switch] widgets. /// /// If null, [ListTile]'s default of `EdgeInsets.symmetric(horizontal: 16.0)` /// is used. final EdgeInsets contentPadding; /// Whether to render icons and text in the [activeColor]. /// /// No effort is made to automatically coordinate the [selected] state and the /// [value] state. To have the list tile appear selected when the switch is /// on, pass the same value to both. /// /// Normally, this property is left to its default value, false. final bool selected; /// {@macro flutter.widgets.Focus.autofocus} final bool autofocus; /// Creates On/Off switch field CustomSwitch({ Key? key, //From Super String name = "switch", FormFieldValidator? validator, bool? initialValue, InputDecoration decoration = const InputDecoration( border: InputBorder.none, focusedBorder: InputBorder.none, enabledBorder: InputBorder.none, errorBorder: InputBorder.none, disabledBorder: InputBorder.none, ), ValueChanged? onChanged, ValueTransformer? valueTransformer, bool enabled = true, FormFieldSetter? onSaved, AutovalidateMode autovalidateMode = AutovalidateMode.disabled, VoidCallback? onReset, FocusNode? focusNode, this.activeColor = CustomColor.primaryColor, this.activeTrackColor, this.inactiveThumbColor = CustomColor.textGray, this.inactiveTrackColor, this.secondary, this.contentPadding = EdgeInsets.zero, this.autofocus = false, this.selected = false, }) : super( key: key, initialValue: initialValue, name: name, validator: validator, valueTransformer: valueTransformer, onChanged: onChanged, autovalidateMode: autovalidateMode, onSaved: onSaved, enabled: enabled, onReset: onReset, decoration: decoration, focusNode: focusNode, builder: (FormFieldState field) { final state = field as _SwitchState; return Transform.scale( scale: 0.8, child: CupertinoSwitch( value: state.value!, onChanged: state.enabled ? (val) { state.requestFocus(); field.didChange(val); } : null, activeColor: activeColor, ), ); }, ); @override _SwitchState createState() => _SwitchState(); } class _SwitchState extends FormBuilderFieldState { @override void didUpdateWidget(covariant CustomSwitch oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.initialValue != widget.initialValue) { setState(() { didChange(widget.initialValue); }); } } }