Kinda Code
Home/Flutter/Flutter: TextField and Negative Numbers

Flutter: TextField and Negative Numbers

Last updated: October 06, 2022

In Flutter, if you want to create a TextField that is optimized to take numerical inputs, 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 an iOS user cannot enter a negative number.

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

Example

Screenshot:

The code:

 Scaffold(
      appBar: AppBar(
        title: const Text('KindaCode.com'),
      ),
      body: const Padding(
        padding: EdgeInsets.all(30),
        child: Center(
          child: TextField(
              keyboardType:
                  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.