Forked from andre-bahia/CurrencyPtBrInputFormatter.dart
Created
April 11, 2024 14:48
-
-
Save albertosales/e462096f792bdbb6b4bc059cf981cdf3 to your computer and use it in GitHub Desktop.
Flutter TextInputFormatter Currency pt_BR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/services.dart'; | |
import 'package:intl/intl.dart'; | |
class CurrencyPtBrInputFormatter extends TextInputFormatter { | |
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) { | |
if(newValue.selection.baseOffset == 0){ | |
return newValue; | |
} | |
double value = double.parse(newValue.text); | |
final formatter = new NumberFormat("#,##0.00", "pt_BR"); | |
String newText = "R\$ " + formatter.format(value/100); | |
return newValue.copyWith( | |
text: newText, | |
selection: new TextSelection.collapsed(offset: newText.length)); | |
} | |
} | |
// how to use | |
Widget _fieldValues() { | |
return Padding( | |
padding: EdgeInsets.only(top: 10, bottom: 60), | |
child: TextFormField( | |
decoration: InputDecoration( | |
icon: Icon(Icons.monetization_on), | |
labelText: 'Valor *', | |
), | |
keyboardType: TextInputType.number, | |
inputFormatters: [ | |
FilteringTextInputFormatter.digitsOnly, | |
CurrencyPtBrInputFormatter() | |
] | |
); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment