ObserverFormField<T> constructor

ObserverFormField<T>({
  1. required ObservableBase<T> observable,
  2. TextFormatter<T>? formatter,
  3. TextInputType? keyboardType,
  4. Key? key,
  5. InputDecoration decoration = const InputDecoration(),
  6. TextInputAction? textInputAction,
  7. TextCapitalization textCapitalization = TextCapitalization.none,
  8. TextStyle? style,
  9. StrutStyle? strutStyle,
  10. TextAlign textAlign = TextAlign.start,
  11. TextAlignVertical? textAlignVertical,
  12. TextDirection textDirection = TextDirection.ltr,
  13. bool readOnly = false,
  14. ToolbarOptions? toolbarOptions,
  15. bool showCursor = true,
  16. bool autofocus = false,
  17. String obscuringCharacter = '•',
  18. bool obscureText = false,
  19. bool autocorrect = true,
  20. SmartDashesType? smartDashesType,
  21. SmartQuotesType? smartQuotesType,
  22. bool enableSuggestions = true,
  23. int? maxLines = 1,
  24. int? minLines,
  25. bool expands = false,
  26. int? maxLength,
  27. MaxLengthEnforcement? maxLengthEnforcement,
  28. ValueChanged<T>? onChanged,
  29. VoidCallback? onEditingComplete,
  30. bool enabled = true,
  31. double cursorWidth = 2.0,
  32. double? cursorHeight,
  33. Radius? cursorRadius,
  34. Color? cursorColor,
  35. Brightness? keyboardAppearance,
  36. EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  37. bool enableInteractiveSelection = true,
  38. TextSelectionControls? selectionControls,
  39. VoidCallback? onTap,
  40. MouseCursor? mouseCursor,
  41. InputCounterWidgetBuilder? buildCounter,
  42. ScrollController? scrollController,
  43. ScrollPhysics? scrollPhysics,
  44. Iterable<String>? autofillHints,
  45. DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  46. bool enableIMEPersonalizedLearning = true,
  47. AppPrivateCommandCallback? onAppPrivateCommand,
  48. BoxHeightStyle selectionHeightStyle = BoxHeightStyle.tight,
  49. BoxWidthStyle selectionWidthStyle = BoxWidthStyle.tight,
  50. ValueChanged<T>? onSubmitted,
  51. String? restorationId,
})

Create new form field for an observable

For documentation about the various parameters, see the TextField class and new TextField, the constructor.

Implementation

ObserverFormField({
  required this.observable,
  this.formatter,
  this.keyboardType,
  Key? key,
  //copy from constructor TextField
  InputDecoration decoration = const InputDecoration(),
  TextInputAction? textInputAction,
  TextCapitalization textCapitalization = TextCapitalization.none,
  TextStyle? style,
  StrutStyle? strutStyle,
  TextAlign textAlign = TextAlign.start,
  TextAlignVertical? textAlignVertical,
  TextDirection textDirection = TextDirection.ltr,
  bool readOnly = false,
  ToolbarOptions? toolbarOptions,
  bool showCursor = true,
  bool autofocus = false,
  String obscuringCharacter = '•',
  bool obscureText = false,
  bool autocorrect = true,
  SmartDashesType? smartDashesType,
  SmartQuotesType? smartQuotesType,
  bool enableSuggestions = true,
  int? maxLines = 1,
  int? minLines,
  bool expands = false,
  int? maxLength,
  MaxLengthEnforcement? maxLengthEnforcement,
  ValueChanged<T>? onChanged,
  VoidCallback? onEditingComplete,
  bool enabled = true,
  double cursorWidth = 2.0,
  double? cursorHeight,
  Radius? cursorRadius,
  Color? cursorColor,
  Brightness? keyboardAppearance,
  EdgeInsets scrollPadding = const EdgeInsets.all(20.0),
  bool enableInteractiveSelection = true,
  TextSelectionControls? selectionControls,
  VoidCallback? onTap,
  MouseCursor? mouseCursor,
  InputCounterWidgetBuilder? buildCounter,
  ScrollController? scrollController,
  ScrollPhysics? scrollPhysics,
  Iterable<String>? autofillHints,
  DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  bool enableIMEPersonalizedLearning = true,
  AppPrivateCommandCallback? onAppPrivateCommand,
  BoxHeightStyle selectionHeightStyle = BoxHeightStyle.tight,
  BoxWidthStyle selectionWidthStyle = BoxWidthStyle.tight,
  ValueChanged<T>? onSubmitted,
  String? restorationId,
}) : super(
        key: key,
        initialValue: observable.value,
        enabled: enabled,
        builder: (field) {
          final state = field as _ObserverFormFieldState<T>;
          return ObserverWidget(
            observable: state._err!,
            builder: (context, String? err) => TextField(
              controller: state._controller,
              decoration: decoration
                  .applyDefaults(Theme.of(context).inputDecorationTheme)
                  .copyWith(errorText: err),
              keyboardType: state._keyboardType,
              focusNode: state._focusNode,
              textInputAction: textInputAction,
              textCapitalization: textCapitalization,
              style: style,
              strutStyle: strutStyle,
              textAlign: textAlign,
              textAlignVertical: textAlignVertical,
              textDirection: textDirection,
              readOnly: readOnly && observable is ObservableWritable,
              toolbarOptions: toolbarOptions,
              showCursor: showCursor,
              autofocus: autofocus,
              obscuringCharacter: obscuringCharacter,
              obscureText: obscureText,
              autocorrect: autocorrect,
              smartDashesType: smartDashesType ??
                  (obscureText
                      ? SmartDashesType.disabled
                      : SmartDashesType.enabled),
              smartQuotesType: smartQuotesType ??
                  (obscureText
                      ? SmartQuotesType.disabled
                      : SmartQuotesType.enabled),
              enableSuggestions: enableSuggestions,
              maxLines: maxLines,
              minLines: minLines,
              expands: expands,
              maxLength: maxLength,
              onChanged: (val) {
                if (state._formatter!.isValid(val)) {
                  final v = state._getValue(val);
                  if (observable is ObservableWritable<T> && v is T) {
                    state._updating = true;
                    observable.value = v;
                  }
                  onChanged?.call(v);
                }
              },
              onEditingComplete: onEditingComplete,
              inputFormatters: <TextInputFormatter>[state._formatter!],
              enabled: enabled,
              cursorWidth: cursorWidth,
              cursorHeight: cursorHeight,
              cursorRadius: cursorRadius,
              cursorColor: cursorColor,
              keyboardAppearance: keyboardAppearance,
              scrollPadding: scrollPadding,
              enableInteractiveSelection: enableInteractiveSelection,
              selectionControls: selectionControls,
              onTap: onTap,
              mouseCursor: mouseCursor,
              buildCounter: buildCounter,
              scrollController: scrollController,
              scrollPhysics: scrollPhysics,
              autofillHints: autofillHints,
              dragStartBehavior: dragStartBehavior,
              enableIMEPersonalizedLearning: enableIMEPersonalizedLearning,
              maxLengthEnforcement: maxLengthEnforcement,
              onAppPrivateCommand: onAppPrivateCommand,
              selectionHeightStyle: selectionHeightStyle,
              selectionWidthStyle: selectionWidthStyle,
              onSubmitted: onSubmitted != null
                  ? (_) => onSubmitted.call(observable.peek)
                  : null,
              restorationId: restorationId,
            ),
          );
        },
      );