FormSwitch<TValue> constructor

FormSwitch<TValue>({
  1. FormController<TValue>? form,
  2. Key? key,
  3. FormStyle? style,
  4. TValue onSaved(
    1. bool value
    )?,
  5. bool initialValue = false,
  6. bool enabled = true,
  7. void onChanged(
    1. bool value
    )?,
  8. bool keepAlive = true,
  9. FocusNode? focusNode,
  10. bool autofocus = false,
  11. bool readOnly = false,
  12. String? labelText,
  13. Widget? labelWidget,
  14. FormAffixStyle? prefix,
  15. FormAffixStyle? suffix,
})

This widget is used to display switches and save ON/OFF of switches.

If labelText or labelWidget is specified, the switch is displayed with a label. Only one of labelText and labelWidget should be specified.

Place under the Form that gave FormController.key, or pass FormController to form.

When FormController is passed to form, onSaved must also be passed together. The contents of onSaved will be used to save the data.

Enter the initial value given by FormController.value in initialValue.

Each time the content is changed, onChanged is executed.

When FormController.validate is executed, validation and data saving are performed.

If enabled is false, the switch is deactivated.

If readOnly is set to true, it will show enabled, but the value cannot be changed.

スイッチを表示して、スイッチのON・OFFを保存するためのウィジェットです。

labelTextもしくはlabelWidgetを指定するとラベル付きでスイッチが表示されます。 labelTextlabelWidgetはどちらか一方のみを指定してください。

FormController.keyを与えたForm配下に配置、もしくはformFormControllerを渡します。

formFormControllerを渡した場合、一緒にonSavedも渡してください。データの保存はonSavedの内容が実行されます。

initialValueFormController.valueから与えられた初期値を入力します。

内容が変更される度onChangedが実行されます。

FormController.validateが実行された場合、バリデーションとデータの保存を行ないます。

enabledfalseになるとスイッチが非有効化されます。

readOnlytrueになっている場合は、有効化の表示になりますが、値が変更できなくなります。

Implementation

FormSwitch({
  this.form,
  super.key,
  FormStyle? style,
  TValue Function(bool value)? onSaved,
  bool super.initialValue = false,
  super.enabled,
  this.onChanged,
  this.keepAlive = true,
  FocusNode? focusNode,
  bool autofocus = false,
  bool readOnly = false,
  String? labelText,
  Widget? labelWidget,
  FormAffixStyle? prefix,
  FormAffixStyle? suffix,
})  : assert(
        (form == null && onSaved == null) ||
            (form != null && onSaved != null),
        "Both are required when using [form] or [onSaved].",
      ),
      assert(!(labelText != null && labelWidget != null),
          "Please select either [labelText] or [labelWidget]."),
      super(
        onSaved: (val) {
          if (val == null) {
            return;
          }
          final res = onSaved?.call(val);
          if (res == null) {
            return;
          }
          form!.value = res;
        },
        builder: (field) {
          final theme = Theme.of(field.context);
          final mainTextStyle = style?.textStyle?.copyWith(
                color: style.color,
              ) ??
              TextStyle(
                color: style?.color ??
                    Theme.of(field.context).textTheme.titleMedium?.color,
              );
          final subTextStyle = style?.textStyle?.copyWith(
                color: style.subColor,
              ) ??
              TextStyle(
                color: style?.subColor ??
                    style?.color?.withOpacity(0.5) ??
                    Theme.of(field.context)
                        .textTheme
                        .titleMedium
                        ?.color
                        ?.withOpacity(0.5),
              );
          final errorTextStyle = style?.errorTextStyle?.copyWith(
                color: style.errorColor,
              ) ??
              style?.textStyle?.copyWith(
                color: style.errorColor,
              ) ??
              TextStyle(
                color: style?.errorColor ??
                    Theme.of(field.context).colorScheme.error,
              );
          final disabledTextStyle = style?.textStyle?.copyWith(
                color: style.disabledColor,
              ) ??
              TextStyle(
                color: style?.disabledColor ??
                    Theme.of(field.context).disabledColor,
              );
          InputBorder getBorderSide(Color borderColor) {
            switch (style?.borderStyle) {
              case FormInputBorderStyle.outline:
                return OutlineInputBorder(
                  borderRadius: style?.borderRadius ??
                      const BorderRadius.all(Radius.circular(4.0)),
                  borderSide: BorderSide(
                    color: borderColor,
                    width: style?.borderWidth ?? 1.0,
                  ),
                );
              case FormInputBorderStyle.underline:
                return UnderlineInputBorder(
                  borderSide: BorderSide(
                    color: style?.borderColor ?? theme.dividerColor,
                    width: style?.borderWidth ?? 1.0,
                  ),
                  borderRadius: style?.borderRadius ??
                      const BorderRadius.only(
                        topLeft: Radius.circular(4.0),
                        topRight: Radius.circular(4.0),
                      ),
                );
              default:
                return const OutlineInputBorder(
                  borderSide: BorderSide.none,
                );
            }
          }

          final borderSide =
              getBorderSide(style?.borderColor ?? theme.dividerColor);
          final errorBorderSide =
              getBorderSide(style?.errorColor ?? theme.colorScheme.error);
          final disabledBorderSide =
              getBorderSide(style?.disabledColor ?? theme.disabledColor);
          final switchForm = Switch(
            value: field.value ?? false,
            onChanged:
                enabled && !readOnly ? (val) => field.didChange(val) : null,
            activeColor: style?.activeColor ??
                Theme.of(field.context).colorScheme.primary,
            inactiveThumbColor:
                style?.disabledColor ?? Theme.of(field.context).disabledColor,
            focusNode: focusNode,
            autofocus: autofocus,
          );
          if (labelText == null && labelWidget == null) {
            return switchForm;
          }
          return MouseRegion(
            cursor: enabled == false
                ? SystemMouseCursors.forbidden
                : SystemMouseCursors.click,
            child: InputDecorator(
              baseStyle: style?.textStyle,
              textAlign: style?.textAlign,
              textAlignVertical: style?.textAlignVertical,
              decoration: InputDecoration(
                contentPadding: style?.contentPadding ??
                    const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                fillColor: style?.backgroundColor,
                filled: style?.backgroundColor != null,
                isDense: true,
                border: style?.border ?? borderSide,
                enabledBorder: style?.border ?? borderSide,
                disabledBorder: style?.disabledBorder ??
                    style?.border ??
                    disabledBorderSide,
                errorBorder:
                    style?.errorBorder ?? style?.border ?? errorBorderSide,
                focusedBorder: style?.border ?? borderSide,
                focusedErrorBorder:
                    style?.errorBorder ?? style?.border ?? errorBorderSide,
                prefix: prefix?.child ?? style?.prefix?.child,
                suffix: suffix?.child ?? style?.suffix?.child,
                prefixIcon: prefix?.icon ?? style?.prefix?.icon,
                suffixIcon: suffix?.icon ?? style?.suffix?.icon,
                prefixText: prefix?.label ?? style?.prefix?.label,
                suffixText: suffix?.label ?? style?.suffix?.label,
                prefixIconColor:
                    prefix?.iconColor ?? style?.prefix?.iconColor,
                suffixIconColor:
                    suffix?.iconColor ?? style?.suffix?.iconColor,
                prefixIconConstraints:
                    prefix?.iconConstraints ?? style?.prefix?.iconConstraints,
                suffixIconConstraints:
                    suffix?.iconConstraints ?? style?.suffix?.iconConstraints,
                labelStyle: enabled ? mainTextStyle : disabledTextStyle,
                hintStyle: subTextStyle,
                suffixStyle: subTextStyle,
                prefixStyle: subTextStyle,
                counterStyle: subTextStyle,
                helperStyle: subTextStyle,
                errorStyle: errorTextStyle,
              ),
              child: Row(
                children: [
                  switchForm,
                  Expanded(
                    child: labelWidget ??
                        Text(
                          labelText!,
                          style: style?.textStyle,
                        ),
                  ),
                ],
              ),
            ),
          );
        },
      );