gpt4 book ai didi

flutter - NumerFormat 不允许我添加 ","

转载 作者:IT王子 更新时间:2023-10-29 07:17:32 24 4
gpt4 key购买 nike

我希望货币格式符合意大利语(欧洲大部分地区)的语义。为此,我编写了一个数字格式功能,但它不允许我添加“,”作为值的小数部分。

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)}',
)
],
),
),
),
);
}
}

关于flutter - NumerFormat 不允许我添加 ",",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56886928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com