BoolField constructor

BoolField({
  1. String? labelPrefix,
  2. String? label,
  3. Widget? labelWidget,
  4. ValueNotifier<bool>? controller,
  5. String? validator(
    1. bool value
    )?,
  6. void onSaved(
    1. bool value
    )?,
  7. bool? initialValue,
  8. bool enabled = true,
  9. AutovalidateMode autoValidateMode = AutovalidateMode.disabled,
  10. dynamic onChanged(
    1. bool value
    )?,
  11. bool filled = false,
  12. Color? fillColor,
  13. bool adaptive = false,
  14. Color? activeColor,
  15. InputDecoration? decoration,
  16. EdgeInsets padding = const EdgeInsets.all(8),
  17. Widget? prefixIcon,
  18. Widget? suffixIcon,
  19. String? hintText,
  20. EdgeInsets? contentPadding,
  21. TextOverflow textOverflow = TextOverflow.ellipsis,
  22. int? sizeExtraSmall,
  23. int? sizeSmall,
  24. int? sizeMedium,
  25. int? sizeLarge,
  26. int? sizeExtraLarge,
  27. double? minHeight,
  28. Key? key,
})

Implementation

BoolField({
  String? labelPrefix,
  String? label,
  Widget? labelWidget,
  this.controller,
  String? Function(bool value)? validator,
  void Function(bool value)? onSaved,
  bool? initialValue,
  super.enabled,
  AutovalidateMode autoValidateMode = AutovalidateMode.disabled,
  this.onChanged,
  bool filled = false,
  Color? fillColor,
  bool adaptive = false,
  Color? activeColor,
  InputDecoration? decoration,
  EdgeInsets padding = const EdgeInsets.all(8),
  Widget? prefixIcon,
  Widget? suffixIcon,
  String? hintText,
  EdgeInsets? contentPadding,
  TextOverflow textOverflow = TextOverflow.ellipsis,
  super.sizeExtraSmall,
  super.sizeSmall,
  super.sizeMedium,
  super.sizeLarge,
  super.sizeExtraLarge,
  super.minHeight,
  super.key,
})  : assert(
        initialValue == null || controller == null,
        'initialValue or controller must be null.',
      ),
      assert(
        label == null || labelWidget == null,
        'label or labelWidget must be null.',
      ),
      super(
        initialValue: controller != null ? controller.value : initialValue,
        validator: enabled && validator != null
            ? (bool? value) => validator(value ?? false)
            : (_) => null,
        onSaved: enabled && onSaved != null
            ? (bool? value) => onSaved(value ?? false)
            : null,
        autovalidateMode: autoValidateMode,
        builder: (FormFieldState<bool> field) {
          _BoolFieldState state = field as _BoolFieldState;

          InputDecoration effectiveDecoration = (decoration ??
                  InputDecoration(
                    border: const OutlineInputBorder(),
                    filled: filled,
                    fillColor: fillColor,
                    counterText: '',
                    enabled: enabled,
                    contentPadding: contentPadding ??
                        const EdgeInsets.symmetric(
                          vertical: 10,
                          horizontal: 8,
                        ),
                    prefixIcon: prefixIcon,
                    suffixIcon: suffixIcon,
                    hintText: hintText,
                  ))
              .applyDefaults(Theme.of(field.context).inputDecorationTheme);

          Color? textColor =
              Theme.of(field.context).textTheme.titleMedium?.color;

          TextStyle textStyle =
              Theme.of(field.context).textTheme.titleMedium!.copyWith(
                    color: textColor?.withOpacity(enabled ? 1 : 0.4),
                    overflow: textOverflow,
                  );

          return Padding(
            padding: padding,
            child: Builder(
              builder: (BuildContext context) {
                return InkWell(
                  canRequestFocus: false,
                  onTap: enabled
                      ? () => state.didChange(!(state.value ?? false))
                      : null,
                  child: InputDecorator(
                    decoration: effectiveDecoration.copyWith(
                      errorText: enabled ? field.errorText : null,
                      enabled: enabled,
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.symmetric(
                              horizontal: 4,
                            ),
                            child: label == null
                                ? labelWidget
                                : Text(
                                    labelPrefix?.isEmpty ?? true
                                        ? label
                                        : '$labelPrefix - $label',
                                    style: textStyle,
                                  ),
                          ),
                        ),
                        if (adaptive)
                          Switch.adaptive(
                            value: state._effectiveController.value,
                            onChanged: enabled ? state.didChange : null,
                            activeColor: activeColor,
                          )
                        else
                          Switch(
                            value: state._effectiveController.value,
                            onChanged: enabled ? state.didChange : null,
                            activeColor: activeColor,
                          ),
                      ],
                    ),
                  ),
                );
              },
            ),
          );
        },
      );