9.8 C
New York
Thursday, November 30, 2023

Flutter: TextField and Negative Numbers

In Flutter, if you want to create a TextField that is optimized to take numerical information, you can set the keyboardType parameter to TextInputType.number. However, with this implementation, the soft keyboard on iOS doesn’t have a minus symbol (while the soft keyboard on Android does) so that an iOS user cannot enter a negative number.

To make a number TextField that can take negative numbers from the soft keyboard, you can set the keyboardType parameter to TextInputType.numberWithOptions(signed: true,).Advertisements



The code:

      appBar: AppBar(
        title: const Text('KindaCode.com'),
      body: const Padding(
        padding: EdgeInsets.all(30),
        child: Center(
          child: TextField(
                  TextInputType.numberWithOptions(signed: true, decimal: true),
              decoration: InputDecoration(
                  label: Text('Enter a number'), border: OutlineInputBorder())),

Further reading:

You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.


Related Articles

Latest Articles