Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow only two decimal number in flutter input?

I want only two digits after the decimal point in the flutter input.User can't add more than two digits after the decimal point.

like image 444
Hitesh Danidhariya Avatar asked Jan 31 '19 06:01

Hitesh Danidhariya


People also ask

How do you add decimals on flutter?

I think MaskedTextController is unnecessary in your case, simply change int inputMaxValue to double inputMaxValue and possibly add . toStringAsFixed(3) to achieve 3 decimals after the dot. e.g. double inputMaxValue = 1.0; String inputIndexValue = "${inputMaxValue.

How do you use TextField in flutter?

In Flutter, this can be done using TextEditingController . First, create a TextEditingController and set it as a controller property of your TextField widget. In this example, I have added an extra Button and Text widget which will show the added text when you click the “Show Text” button.

How do you open the number keyboard in flutter?

Steps to show numeric input keyboard in Flutter Step 1: Add TextField widget to your dart file. Step 2: Add keyboardType parameter and assign the TextInputType. number. Step 3: Add inputFormatters parameter and assign the [FilteringTextInputFormatter.


1 Answers

Here you go! Hope it helps :)

import 'package:flutter/material.dart'; import 'package:flutter/services.dart';  import 'dart:math' as math;  void main() {   runApp(new MaterialApp(home: new MyApp())); }  class MyApp extends StatefulWidget {   @override   _MyAppState createState() => _MyAppState(); }  class _MyAppState extends State<MyApp> {   @override   Widget build(BuildContext context) {     return Scaffold(       appBar: AppBar(         title: Text("Flutter"),       ),       body: Form(         child: ListView(           children: <Widget>[             TextFormField(               inputFormatters: [DecimalTextInputFormatter(decimalRange: 2)],               keyboardType: TextInputType.numberWithOptions(decimal: true),             )           ],         ),       ),     );   } }  class DecimalTextInputFormatter extends TextInputFormatter {   DecimalTextInputFormatter({this.decimalRange})       : assert(decimalRange == null || decimalRange > 0);    final int decimalRange;    @override   TextEditingValue formatEditUpdate(     TextEditingValue oldValue, // unused.     TextEditingValue newValue,   ) {     TextSelection newSelection = newValue.selection;     String truncated = newValue.text;      if (decimalRange != null) {       String value = newValue.text;        if (value.contains(".") &&           value.substring(value.indexOf(".") + 1).length > decimalRange) {         truncated = oldValue.text;         newSelection = oldValue.selection;       } else if (value == ".") {         truncated = "0.";          newSelection = newValue.selection.copyWith(           baseOffset: math.min(truncated.length, truncated.length + 1),           extentOffset: math.min(truncated.length, truncated.length + 1),         );       }        return TextEditingValue(         text: truncated,         selection: newSelection,         composing: TextRange.empty,       );     }     return newValue;   } } 
like image 199
Ajay Kumar Avatar answered Sep 22 '22 18:09

Ajay Kumar