Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinmungai/227369b664c21f4183e38a60fe2d3171 to your computer and use it in GitHub Desktop.
Save kevinmungai/227369b664c21f4183e38a60fe2d3171 to your computer and use it in GitHub Desktop.
Flutter TextInputFormatter Currency pt_BR
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: [
WhitelistingTextInputFormatter.digitsOnly,
CurrencyPtBrInputFormatter()
]
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment