TxNumberFormField constructor

TxNumberFormField({
  1. bool? clearable,
  2. Key? key,
  3. FormFieldSetter<num>? onSaved,
  4. FormFieldValidator<num>? validator,
  5. bool? enabled,
  6. AutovalidateMode? autovalidateMode = AutovalidateMode.onUserInteraction,
  7. String? restorationId,
  8. InputDecoration? decoration,
  9. ValueChanged<num?>? onChanged,
  10. bool? required,
  11. num? initialValue,
  12. bool? bordered,
  13. String? hintText,
  14. num? maxValue,
  15. num? minValue,
  16. bool? stepped,
  17. num? step,
  18. int? precision,
  19. bool? stepStrictly,
  20. TextEditingController? controller,
  21. UndoHistoryController? undoController,
  22. TextInputAction? textInputAction,
  23. TextCapitalization? textCapitalization,
  24. TextStyle? style,
  25. StrutStyle? strutStyle,
  26. TextAlign? textAlign,
  27. TextAlignVertical? textAlignVertical,
  28. TextDirection? textDirection,
  29. bool? readOnly,
  30. bool? showCursor,
  31. bool? autofocus,
  32. MaterialStatesController? statesController,
  33. String? obscuringCharacter,
  34. bool? obscureText,
  35. bool? autocorrect,
  36. SmartDashesType? smartDashesType,
  37. SmartQuotesType? smartQuotesType,
  38. bool? enableSuggestions,
  39. int? maxLines,
  40. int? minLines,
  41. bool? expands,
  42. int? maxLength,
  43. MaxLengthEnforcement? maxLengthEnforcement,
  44. VoidCallback? onEditingComplete,
  45. ValueChanged<String>? onFieldSubmitted,
  46. AppPrivateCommandCallback? onAppPrivateCommand,
  47. List<TextInputFormatter>? inputFormatters,
  48. double? cursorWidth,
  49. double? cursorHeight,
  50. Radius? cursorRadius,
  51. bool? cursorOpacityAnimates,
  52. Color? cursorColor,
  53. Color? cursorErrorColor,
  54. BoxHeightStyle? selectionHeightStyle,
  55. BoxWidthStyle? selectionWidthStyle,
  56. Brightness? keyboardAppearance,
  57. EdgeInsets? scrollPadding,
  58. DragStartBehavior? dragStartBehavior,
  59. bool? enableInteractiveSelection,
  60. TextSelectionControls? selectionControls,
  61. GestureTapCallback? onTap,
  62. bool? onTapAlwaysCalled,
  63. TapRegionCallback? onTapOutside,
  64. MouseCursor? mouseCursor,
  65. InputCounterWidgetBuilder? buildCounter,
  66. ScrollController? scrollController,
  67. ScrollPhysics? scrollPhysics,
  68. Iterable<String>? autofillHints,
  69. ContentInsertionConfiguration? contentInsertionConfiguration,
  70. Clip? clipBehavior,
  71. bool? scribbleEnabled,
  72. bool? enableIMEPersonalizedLearning,
  73. EditableTextContextMenuBuilder? contextMenuBuilder,
  74. bool? canRequestFocus,
  75. SpellCheckConfiguration? spellCheckConfiguration,
  76. TextMagnifierConfiguration? magnifierConfiguration,
  77. Widget? label,
  78. String? labelText,
  79. TextAlign? labelTextAlign,
  80. TextOverflow? labelOverflow,
  81. EdgeInsetsGeometry? padding,
  82. FieldActionsBuilder<num>? actionsBuilder,
  83. TextStyle? labelStyle,
  84. double? horizontalGap,
  85. Color? tileColor,
  86. Axis? layoutDirection,
  87. TxFormFieldBuilder<num>? trailingBuilder,
  88. Widget? leading,
  89. VisualDensity? visualDensity,
  90. ShapeBorder? shape,
  91. Color? iconColor,
  92. Color? textColor,
  93. TextStyle? leadingAndTrailingTextStyle,
  94. double? minLeadingWidth,
  95. double? minLabelWidth,
  96. double? minVerticalPadding,
  97. bool? dense,
  98. ValueChanged<TxFormFieldState<num>>? onFieldTap,
  99. bool? colon,
  100. Color? focusColor,
  101. FocusNode? focusNode,
})

Implementation

TxNumberFormField({
  bool? clearable,
  super.key,
  super.onSaved,
  FormFieldValidator<num>? validator,
  super.enabled,
  super.autovalidateMode,
  super.restorationId,
  super.decoration,
  super.onChanged,
  super.required,
  super.initialValue,
  super.bordered,
  String? hintText,
  this.maxValue,
  this.minValue,
  bool? stepped,
  this.step,
  this.precision,
  bool? stepStrictly,
  super.controller,
  super.undoController,
  super.textInputAction,
  super.textCapitalization,
  super.style,
  super.strutStyle,
  TextAlign? textAlign,
  super.textAlignVertical,
  super.textDirection,
  super.readOnly,
  super.showCursor,
  super.autofocus,
  super.statesController,
  super.obscuringCharacter,
  super.obscureText,
  super.autocorrect,
  super.smartDashesType,
  super.smartQuotesType,
  super.enableSuggestions,
  super.maxLines,
  super.minLines,
  super.expands,
  super.maxLength,
  super.maxLengthEnforcement,
  super.onEditingComplete,
  super.onFieldSubmitted,
  super.onAppPrivateCommand,
  List<TextInputFormatter>? inputFormatters,
  super.cursorWidth,
  super.cursorHeight,
  super.cursorRadius,
  super.cursorOpacityAnimates,
  super.cursorColor,
  super.cursorErrorColor,
  super.selectionHeightStyle,
  super.selectionWidthStyle,
  super.keyboardAppearance,
  super.scrollPadding,
  super.dragStartBehavior,
  super.enableInteractiveSelection,
  super.selectionControls,
  super.onTap,
  super.onTapAlwaysCalled,
  super.onTapOutside,
  super.mouseCursor,
  super.buildCounter,
  super.scrollController,
  super.scrollPhysics,
  super.autofillHints,
  super.contentInsertionConfiguration,
  super.clipBehavior,
  super.scribbleEnabled,
  super.enableIMEPersonalizedLearning,
  super.contextMenuBuilder,
  super.canRequestFocus,
  super.spellCheckConfiguration,
  super.magnifierConfiguration,
  super.label,
  super.labelText,
  super.labelTextAlign,
  super.labelOverflow,
  super.padding,
  super.actionsBuilder,
  super.labelStyle,
  super.horizontalGap,
  super.tileColor,
  super.layoutDirection,
  super.trailingBuilder,
  super.leading,
  super.visualDensity,
  super.shape,
  super.iconColor,
  super.textColor,
  super.leadingAndTrailingTextStyle,
  super.minLeadingWidth,
  super.minLabelWidth,
  super.minVerticalPadding,
  super.dense,
  super.onFieldTap,
  super.colon,
  super.focusColor,
  super.focusNode,
})  : stepped = stepped ?? false,
      stepStrictly = stepStrictly ?? false,
      super(
        hintText: hintText ?? '请输入',
        clearable: clearable ?? false,
        displayTextMapper: (context, val) => val.toString(),
        keyboardType: TextInputType.number,
        textAlign: textAlign ?? (stepped == true ? TextAlign.center : null),
        onInputChanged: (field, val) {
          final num? number = num.tryParse(val);
          if (number != field.value) {
            (field as TxCommonTextFormFieldState).didChange(number);
          }
        },
        inputFormatters: [
          ...?inputFormatters,
          NumberInputFormatter(
            min: minValue,
            max: maxValue,
            precision: precision,
            step: step,
            stepStrictly: stepStrictly,
          ),
        ],
        validator: (value) {
          if (required == true && value == null) {
            return '请输入';
          }

          if (validator != null) {
            final String? errorText = validator(value);
            if (errorText != null) {
              return errorText;
            }
          }

          /// 如果最小值不为空,判断输入值是否小于最小值
          if (minValue != null && value != null && value < minValue) {
            return '输入值需大于或等于$minValue';
          }

          /// 如果最小数量不为空,判断已选数量是否小于最小数量
          if (maxValue != null && value != null && value > maxValue) {
            return '输入值需小于或等于$maxValue';
          }

          return null;
        },
      );