Flutter: TextField and Negative Numbers

Last updated on October 6, 2022 A Goodman Loading... Post a comment

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,).



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.

Notify of
Inline Feedbacks
View all comments

You May Also Like