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({
  final String? labelPrefix,
  final String? label,
  final Widget? labelWidget,
  this.controller,
  final String? Function(bool value)? validator,
  final void Function(bool value)? onSaved,
  final bool? initialValue,
  super.enabled,
  final AutovalidateMode autoValidateMode = AutovalidateMode.disabled,
  this.onChanged,
  final bool filled = false,
  final Color? fillColor,
  final bool adaptive = false,
  final Color? activeColor,
  final InputDecoration? decoration,
  final EdgeInsets padding = const EdgeInsets.all(8),
  final Widget? prefixIcon,
  final Widget? suffixIcon,
  final String? hintText,
  final EdgeInsets? contentPadding,
  final 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
           ? (final bool? value) => validator(value ?? false)
           : (_) => null,
       onSaved: enabled && onSaved != null
           ? (final bool? value) => onSaved(value ?? false)
           : null,
       autovalidateMode: autoValidateMode,
       builder: (final 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?.withValues(alpha: enabled ? 1 : 0.4),
               overflow: textOverflow,
             );

         return Padding(
           padding: padding,
           child: Builder(
             builder: (final 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,
                           // TODO(edufolly): Use activeThumbColor or
                           //  activeTrackColor instead.
                           activeThumbColor: activeColor,
                           activeTrackColor: activeColor,
                         )
                       else
                         Switch(
                           value: state._effectiveController.value,
                           onChanged: enabled ? state.didChange : null,
                           // TODO(edufolly): Use activeThumbColor or
                           //  activeTrackColor instead.
                           activeThumbColor: activeColor,
                           activeTrackColor: activeColor,
                         ),
                     ],
                   ),
                 ),
               );
             },
           ),
         );
       },
     );