flutter - NumerFormat 不允许我添加“,”
问题描述
我想要货币格式匹配意大利语(欧洲大部分地区)语义。为此,我编写了一种数字格式功能,但它不允许我添加“,”作为值的小数部分。
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));
}
}
解决方案
添加依赖 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)}',
)
],
),
),
),
);
}
}
推荐阅读
- c - 如何使这项工作适用于 3 个用户并通过而不是 1 C
- angular - 带有参数的特定路由的用户访问
- python - 通过硒,Python中的xpath检测用户可见元素(仅在视口中)
- c# - 如何让 C# 中的按钮上显示的文本?
- reactjs - 动作类型说明
- mysql-python - 如何在 Python 2.6 中使用 mysql.connector
- swift - 在 SpriteKit swift4 Xcode 中停止一个动作并设置一个定时器来切换玩家
- r - 根据列名和因子将数据从一个数据框收集到另一个数据框
- android - getUserConfirmation 不起作用反应本机
- directory - 我在哪里可以找到星际争霸II 中的地图文件夹?