🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / FLUTTER / TextFormField keyboardType inputFormatters double vs

1-) FLUTTER - TextFormField keyboardType inputFormatters double vs

 

1-) EN SON GÜZEL OLAN

 

   keyboardType: TextInputType.numberWithOptions(decimal: true),
      inputFormatters: <
TextInputFormatter>[
        
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}'))
      ],

TAM KOD AŞAĞIDA  KAYNAK : https://stackoverflow.com/questions/61212332/how-to-display-in-textfield-only-double-value

 

 

TextEditingController txtTip = new TextEditingController(text: "0");

Container txtTipContent() {
  
return Container(
    width:
150,
    child:
TextFormField(
      keyboardType:
TextInputType.numberWithOptions(decimal: true),
      inputFormatters: <
TextInputFormatter>[
        
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}'))
      ],
      controller:
txtTip,
      onChanged: (value){
        print(value+
"*****");
       },
      onTap: () =>
txtTip.selection = TextSelection(
          baseOffset:
0, extentOffset: txtTip.value.text.length),
      style:
TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
      decoration:
InputDecoration(
        icon:
Icon(Icons.account_balance_wallet_outlined),
        labelText:
"Tip",
        labelStyle:
TextStyle(fontSize: 15),
        contentPadding:
            
const EdgeInsets.symmetric(vertical: 5.0, horizontal: 10.0),
      ),
    ),
  );
}

 

 

 

 

 

 

 

 

 

 

2-) DİĞER

 

 

0- sadece sayı olmasını istiyorsan ve para birimi formatlı olsun diyorsan

 

termianlden indir -> flutter pub add currency_text_input_formatter

 

genel tanımlama

CurrencyTextInputFormatter formatter = CurrencyTextInputFormatter( decimalDigits: 0,locale: 'tr',
   symbol:
'');

 

textformfield tanımlama

keyboardType: TextInputType.number,
inputFormatters: [
formatter],

 

sadece değerini alma

onPressed: () {
 
String butce= formatter.getUnformattedValue().toString();
  
EasyLoading.showToast(butce);
},

 

eksik girmemesini istiyorsan

onChanged: (String deger) {
  
if (deger != null && deger.contains("-")) {
   
txtButce.clear();
  }
},

 

 

 

 

kaynak : https://pub.dev/packages/currency_text_input_formatter

 

 

1- sadece sayı olmasını istiyorsan

 

keyboardType: TextInputType.number,
inputFormatters: <
TextInputFormatter>[
  
FilteringTextInputFormatter.digitsOnly
],

 

2- double olmasını istiyorsan. ve kuruş iki basamak

 

keyboardType:  TextInputType.numberWithOptions(decimal: true, signed: false),

inputFormatters: <TextInputFormatter>[
      
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}'))
    ]

3- tam kod

 

TextFormField(
  style:
TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
  decoration:
InputDecoration(
    icon:
Icon(Icons.account_balance_wallet_outlined),
    labelText:
"Bütçe",
    border:
InputBorder.none,
    labelStyle:
TextStyle(fontSize: 15),
    contentPadding:
    
const EdgeInsets.symmetric(vertical: 5.0, horizontal: 10.0),
  ),
  keyboardType:  
TextInputType.numberWithOptions(decimal: true, signed: false),

 inputFormatters: <TextInputFormatter>[
      
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}'))
    ]

  onChanged: (butce){
    
if(butce!="")
    
this.butce=double.parse(butce);
  },
  
  ),

 

 

açıklama : aşağıdaki regex noktadan sonra 2 basamak al demek ve virgül olmayan double ifade demek

 

RegExp(r'^(\d+)?\.?\d{0,2}'

 

 

 

diğer ;

 

// inputFormatters: [
  //   FilteringTextInputFormatter.allow(RegExp(r"[0-9.]")),
  //   TextInputFormatter.withFunction((oldValue, newValue) {
  //     try {
  //       final text = newValue.text;
  //       if (text.isNotEmpty) double.parse(text);
  //       return newValue;
  //     } catch (e) {}
  //     return oldValue;
  //   }),
  // ],

 

 

 

 

 

 2022 Aralık 01 Perşembe
 368