我想采用符合意大利(欧洲大部分地区)语义的货币格式。为此,我编写了一种数字格式功能,但不允许我在值的小数部分添加“,”。
class CurrencyInputFormatter extends TextInputFormatter{
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
// TODO: implement formatEditUpdate
if (newValue.selection.baseOffset == 0) {
print(true);
return newValue;
}
double value = double.parse(newValue.text);
final formatter = new NumberFormat("#,###.##", "it_IT");
String newText = formatter.format(value);
return newValue.copyWith(
text: newText,
selection: new TextSelection.collapsed(offset: newText.length));
}
}
答案 0 :(得分:1)
添加依赖项pubspec.yaml文件。
intl: ^0.15.8
以下是示例
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
NumberFormat currencyFormat = new NumberFormat("#,###.##", "it_IT");
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Number Format'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'${currencyFormat.format(200011.56)}',
)
],
),
),
),
);
}
}