Last active
August 3, 2020 08:58
-
-
Save ltvu93/af36a18ef6fd74a4a2d903070b5c3687 to your computer and use it in GitHub Desktop.
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 'dart:math'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
import 'package:intl/intl.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
// Customize number format pattern https://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html | |
final vnCurrencyFormat = NumberFormat.currency( | |
locale: 'en-US', | |
customPattern: '###,###', | |
decimalDigits: 0, | |
); // VN currency format with ',' group separator. Ex: 9,999,999. | |
final _controller = TextEditingController(); | |
int currentPrice = 9999999; | |
@override | |
void initState() { | |
super.initState(); | |
_controller.text = vnCurrencyFormat.format(currentPrice); | |
_controller.addListener(() { | |
final price = _controller.text.isNotEmpty | |
? vnCurrencyFormat.parse(_controller.text).toInt() | |
: 0; | |
setState(() { | |
currentPrice = price; | |
}); | |
}); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
TextField( | |
controller: _controller, | |
keyboardType: TextInputType.number, | |
inputFormatters: [ | |
CurrencyTextInputFormatter(currencyFormat: vnCurrencyFormat), | |
], | |
), | |
SizedBox(height: 16.0), | |
Text( | |
'Price: $currentPrice', | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
// https://github.com/gtgalone/currency_text_input_formatter/blob/master/lib/currency_text_input_formatter.dart | |
class CurrencyTextInputFormatter extends TextInputFormatter { | |
final int maxNumbers; | |
NumberFormat currencyFormat; | |
CurrencyTextInputFormatter({ | |
this.maxNumbers = 15, | |
NumberFormat currencyFormat, | |
}) { | |
this.currencyFormat = currencyFormat ?? | |
NumberFormat.currency( | |
locale: 'en-US', | |
customPattern: '###,###', | |
decimalDigits: 0, | |
); | |
} | |
TextEditingValue formatEditUpdate( | |
TextEditingValue oldValue, | |
TextEditingValue newValue, | |
) { | |
final newText = newValue.text.replaceAll(RegExp('[^0-9]'), ''); | |
if (newText.trim() == '' || newText.trim() == '0') { | |
return newValue.copyWith(text: ''); | |
} else if (int.parse(newText) < 1) { | |
return newValue.copyWith(text: ''); | |
} else if (newText.length > maxNumbers) { | |
return TextEditingValue( | |
text: oldValue.text, | |
selection: TextSelection.collapsed( | |
offset: oldValue.selection.end, | |
), | |
); | |
} | |
dynamic newInt = int.parse(newText); | |
var selectionIndexFromTheRight = | |
newValue.text.length - newValue.selection.end; | |
if (currencyFormat.decimalDigits > 0) { | |
newInt /= pow(10, currencyFormat.decimalDigits); | |
} | |
final newString = currencyFormat.format(newInt); | |
return TextEditingValue( | |
text: newString, | |
selection: TextSelection.collapsed( | |
offset: newString.length - selectionIndexFromTheRight, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment