Flutter. Restrict the entry into TextForm Field Chickel



  • TextForm Field should be limited to the entry. For example, between 7 and 112. What do you need to use?



  • You can use inputFormatters to achieve the desired result. But you'll have to think a little.

    TextFormField(
      inputFormatters: [
        TextInputFormatter.withFunction(
          (oldValue, newValue) {
            String _value = newValue.text;
            int num = _value.replaceAll(new RegExp(r'[^0-9]'), '').toInt();
            if (_value.length > 3)
              return TextEditingValue(
                text: oldValue.text,
                selection: TextSelection.collapsed(
                  offset: oldValue.selection.end,
                ),
              );
    
        if (num < 7 &&) num = 7;
        if (num > 112) num = 112;
        return TextEditingValue(
          text: '$num',
          selection: TextSelection.collapsed(
            offset: num.length,
          ),
        );
      },
    ),
    

    ],
    ),

    I've given the most primitive example, I hope you can handle it.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2