close

How to create number input field in Flutter?

You can indicate the number as keyboardType for the TextField Widget utilizing:

keyboardType: TextInputType.number

Through this alternative, you can carefully confine another roast without number

inputFormatters: [FilteringTextInputFormatter.digitsOnly],
keyboardType: TextInputType.number,

For utilizing the above choice you need to import this.

import 'package:flutter/services.dart';

utilizing this sort of choice client can not glue singe in a Textfield.

Clients can likewise make a custom capacity like underneath:

String numberValidator(String value) {
  if(value == null) {
    return null;
  }
  final n = num.tryParse(value);
  if(n == null) {
    return '"$value" is not a valid number';
  }
  return null;
}

Utilize the above capacity in TextField Widget Validator like underneath:

TextField(
    keyboardType: TextInputType.number, 
    validator: numberValidator, 
    textAlign: TextAlign.right
    ...
    )

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment