EditInt function

Widget EditInt({
  1. Key? key,
  2. OptionalValueListener<int>? valueListener,
  3. TextEditingController? controller,
  4. int? initialValue,
  5. int? minValue,
  6. int? maxValue,
  7. bool signed = true,
  8. bool allowEmpty = true,
  9. void onSubmitted(
    1. String
    )?,
  10. void onChanged(
    1. String
    )?,
  11. String? label,
  12. String? hint,
  13. Widget? prefixIcon,
  14. String? helperText,
  15. String? errorText,
  16. int? maxLength,
  17. bool clear = false,
  18. Widget? suffixIcon,
  19. FocusNode? focusNode,
  20. Color? cursorColor,
  21. TextInputType? keyboardType,
  22. TextInputAction? textInputAction = TextInputAction.next,
  23. InputDecoration? decoration,
  24. InputBorder? border,
})

Implementation

Widget EditInt({
  Key? key,
  OptionalValueListener<int>? valueListener,
  TextEditingController? controller,
  int? initialValue,
  int? minValue,
  int? maxValue,
  bool signed = true,
  bool allowEmpty = true,
  void Function(String)? onSubmitted,
  void Function(String)? onChanged,
  String? label,
  String? hint,
  Widget? prefixIcon,
  String? helperText,
  String? errorText,
  int? maxLength,
  bool clear = false,
  Widget? suffixIcon,
  FocusNode? focusNode,
  Color? cursorColor,
  TextInputType? keyboardType,
  TextInputAction? textInputAction = TextInputAction.next,
  InputDecoration? decoration,
  InputBorder? border,
}) {
  TextEditingController? c = controller ?? (clear && suffixIcon == null && decoration == null ? TextEditingController() : null);
  FocusNode node = focusNode ?? FocusNode();
  void onTextChanged(String text) {
    valueListener?.onChanged(text.toInt);
  }

  return TextFormField(
    key: key,
    controller: c,
    initialValue: initialValue?.toString() ?? valueListener?.value?.toString(),
    onChanged: onChanged ?? onTextChanged,
    onFieldSubmitted: onSubmitted ?? onTextChanged,
    focusNode: node,
    maxLength: maxLength ?? 20,
    validator: IntValidator(minValue: minValue, maxValue: maxValue, allowEmpty: allowEmpty),
    keyboardType: keyboardType ?? TextInputType.numberWithOptions(signed: signed, decimal: false),
    inputFormatters: [signed ? InputFormats.reals : InputFormats.realsUnsigned],
    textInputAction: textInputAction,
    onTapOutside: (e) => node.unfocus(),
    cursorColor: cursorColor,
    decoration:
        decoration ??
        InputDecoration(
          labelText: label,
          hintText: hint,
          helperText: helperText,
          errorText: errorText,
          counterText: "",
          border: border,
          prefixIcon: prefixIcon,
          suffixIcon: suffixIcon ?? (!clear ? null : IconButton(icon: const Icon(Icons.clear), onPressed: () => c?.clear())),
        ),
  );
}