Dart 如何在颤振中用分隔符显示EMI计算器中的货币格式

Dart 如何在颤振中用分隔符显示EMI计算器中的货币格式,dart,flutter,decimal,currency,Dart,Flutter,Decimal,Currency,我在颤振中构建了一个EMI计算器,但我的结果显示为例如1250568.00,但希望它显示为N$1250568.00 我尝试了intl包,但在文本(f.format(_tiResults)),上出现了一个错误,正如如何实现它所解释的那样。还尝试了MoneyMask软件包,但没有成功 import 'package:homenet/pages/home_page.dart'; import 'dart:math'; class MyHomePage extends StatefulWidget {

我在颤振中构建了一个EMI计算器,但我的结果显示为例如
1250568.00
,但希望它显示为
N$1250568.00

我尝试了intl包,但在
文本(f.format(_tiResults)),
上出现了一个错误,正如如何实现它所解释的那样。还尝试了MoneyMask软件包,但没有成功

import 'package:homenet/pages/home_page.dart';
import 'dart:math';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List _durationTypes = ['Month(s)', 'Year(s)'];
  String _durationType = "Year(s)";
  String _miResult = "";
  String _tiResult = "";
  String _tcResult = "";
  bool _switchValue = true;

  final TextEditingController _principalAmount = TextEditingController();
  final TextEditingController _interestRate = TextEditingController();
  final TextEditingController _loanDuration = TextEditingController();

  _onClear(){
    setState(() {
      _principalAmount.text = "";
      _interestRate.text = "";
      _loanDuration.text = "";
      _miResult = "";
      _tiResult = "";
      _tcResult = "";
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      resizeToAvoidBottomPadding: false,
      appBar: AppBar(
        backgroundColor: new Color(0xFFFA983A),
        title: InkWell(
          onTap: (){
            Navigator.push(context,
              MaterialPageRoute(builder: (context) => new HomePage()));},
          child: Image.asset(
            'assets/images/logo_white.png',
            fit: BoxFit.cover,
          ),
        ),
        elevation: 0.0,
        centerTitle: true,
        actions: <Widget>[
          new IconButton(
            icon: new Icon(Icons.cancel, size: 30,),
            onPressed: () {
              _onClear();
            },
          ),
        ],
      ),
      body: Center(
        child: Container(
          margin: EdgeInsets.all(24),
          child: Column(
            children: <Widget>[
              Container(
                child: TextField(
                  cursorColor: Color(0xFFFA983A),
                  controller: _principalAmount,
                  decoration:
                      InputDecoration(
                  icon: Icon(Icons.monetization_on),
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(25),
                  gapPadding: 5),
                  labelText: "Enter Principal Amount"),

                  keyboardType: TextInputType.numberWithOptions(),
                ),
              ),
              SizedBox(
                height: 12,
              ),
              Container(
                child: TextField(
                  cursorColor: Color(0xFFFA983A),
                  controller: _interestRate,
                  decoration:
                      InputDecoration(
                          icon: Icon(Icons.show_chart),
                          border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(25),
                              gapPadding: 5),
                          labelText: "Interest Rate per Annum %"),
                  keyboardType: TextInputType.numberWithOptions(),
                ),
              ),
              SizedBox(
                height: 12,
              ),
              Row(
                children: <Widget>[
                  Flexible(
                    flex: 3,
                    child: Container(
                      child: TextField(
                        cursorColor: Color(0xFFFA983A),
                        controller: _loanDuration,
                        decoration: InputDecoration(
                            icon: Icon(Icons.date_range),
                            border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(25),
                                gapPadding: 5),
                            labelText: "Loan Duration"),
                        keyboardType: TextInputType.numberWithOptions(),
                      ),
                    ),
                  ),
//                  TODO: ========= SWITCH ================
                  Flexible(
                    flex: 1,
                    child: Column(
                      children: <Widget>[
                        Text(
                          _durationType,
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        Switch(
                            activeColor: Color(0xFFFA983A),
                            value: _switchValue,
                            onChanged: (bool value) {
                              print(value);

                              if (value) {
                                _durationType = _durationTypes[1];
                              } else {
                                _durationType = _durationTypes[0];
                              }

                              setState(() {
                                _switchValue = value;
                              });
                            }),
                      ],
                    ),
                  ),
                ],
              ),
              SizedBox(
                height: 12,
              ),
//              TODO: ============== Button ============
              Flexible(
                child: FlatButton(
                  padding: EdgeInsets.fromLTRB(48, 8, 48, 8),
                  onPressed: _handleCalculation,
                  child: Text(
                    "CALCULATE",
                    style: TextStyle(color: Colors.white),
                  ),
                  color: Color(0xFFFA983A),
                ),
              ),
              SizedBox(
                height: 12,
              ),
//              TODO: Results Widget =====================================
              monthlyInstalmentsResult(_miResult),
              SizedBox(
                height: 12,
              ),
              totalInterestResult(_tiResult),
              SizedBox(
                height: 12,
              ),
              totalCostResult(_tcResult),
              SizedBox(
                height: 12,
              ),
              Container(
                child: Text(
                  "Disclaimer* This is just an approximate amount"
                  "and in no way reflect the exact figures, please consult your bank.",
                  style: TextStyle(
                    color: Colors.grey,
                    fontSize: 10,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _handleCalculation() {
//    TODO: Amortization
    //    TODO: A = Payment amount per period
    //    TODO: P = Initial Principal (Loan Amount)
    //    TODO: r = interest Rate
    //    TODO: n = Total number of payments

    double A = 0.0;
    double I = 0.0;
    double T = 0.0;
    double P = double.parse(_principalAmount.text);
    double r = double.parse(_interestRate.text) / 12 / 100;
    int n = _durationType == "Year(s)"
        ? int.parse(_loanDuration.text) * 12
        : int.parse(_loanDuration.text);

    A = (P * r * pow((1 + r), n) / (pow((1 + r), n) - 1));
    T = (A * n);
    I = (T - P);

    _miResult = A.toStringAsFixed(2);
    setState(() {});
    _tiResult = I.toStringAsFixed(2);
    setState(() {});
    _tcResult = T.toStringAsFixed(2);
    setState(() {});
  }

  Widget monthlyInstalmentsResult(miResults) {

//    var f = new NumberFormat("#,###,###.0#");
//    var f = new NumberFormat("###.0#", "en_US");
    bool canShow = false;
    String _miResults = miResults;

    if (_miResults.length > 0) {
      canShow = true;
    }
    return Container(

        child: canShow
            ? Row(
                children: <Widget>[
                  Text(
                    "Monthly Instalments: ",
                    style: TextStyle(
                      color: Colors.grey,
                      fontSize: 18,
                    ),
                  ),
                  Text(
                    "N\$ ",
                    style: TextStyle(
                      color: Color(0xFFFA983A),
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  Text(
                    _miResult,
                    style: TextStyle(
                      color: Color(0xFFFA983A),
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                    ),
                  )
                ],
              )
            : Row());
  }

  Widget totalInterestResult(tiResults) {
    bool canShow = false;
    String _miResults = tiResults;

    if (_miResults.length > 0) {
      canShow = true;
    }
    return Container(
        child: canShow
            ? Row(
                children: <Widget>[
                  Text(
                    "Total Interest: ",
                    style: TextStyle(
                      color: Colors.grey,
                      fontSize: 18,
                    ),
                  ),
                  Text(
                    "N\$ ",
                    style: TextStyle(
                      color: Color(0xFFFA983A),
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  Text(
                    _tiResult,
                    style: TextStyle(
                      color: Color(0xFFFA983A),
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                    ),
                  )
                ],
              )
            : Row());
  }

  Widget totalCostResult(tcResults) {
    bool canShow = false;
    String _miResults = tcResults;

    if (_miResults.length > 0) {
      canShow = true;
    }
    return Container(
        child: canShow
            ? Row(
                children: <Widget>[
                  Text(
                    "Total Cost: ",
                    style: TextStyle(
                      color: Colors.grey,
                      fontSize: 18,
                    ),
                  ),
                  Text(
                    "N\$ ",
                    style: TextStyle(
                      color: Color(0xFFFA983A),
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  Text(
                    _tcResult,
                    style: TextStyle(
                      color: Color(0xFFFA983A),
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                    ),
                  )
                ],
              )
            : Row());
  }
}
import'package:homenet/pages/home_page.dart';
导入“dart:math”;
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>new_MyHomePageState();
}
类_MyHomePageState扩展状态{
列出_durationTypes=[“月”,“年];
字符串_durationType=“年”;
字符串_miResult=“”;
字符串_tiResult=“”;
字符串tcResult=“”;
bool _switchValue=true;
最终TextEditingController _principalAmount=TextEditingController();
最终TextEditingController _interestRate=TextEditingController();
最终TextEditingController _loanDuration=TextEditingController();
_onClear(){
设置状态(){
_principalAmount.text=“”;
_interestRate.text=“”;
_loanDuration.text=“”;
_miResult=“”;
_tiResult=“”;
_tcResult=“”;
});
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
resizeToAvoidBottomPadding:false,
appBar:appBar(
背景颜色:新颜色(0xFFFA983A),
标题:墨水井(
onTap:(){
Navigator.push(上下文,
MaterialPackageRoute(生成器:(上下文)=>新主页());},
子:Image.asset(
“资产/图像/徽标_white.png”,
适合:BoxFit.cover,
),
),
标高:0.0,
标题:对,
行动:[
新图标按钮(
图标:新图标(Icons.cancel,大小:30,),
已按下:(){
_onClear();
},
),
],
),
正文:中(
子:容器(
保证金:全部(24),
子:列(
儿童:[
容器(
孩子:TextField(
光标颜色:颜色(0xFFFA983A),
控制器:_principalAmount,
装饰:
输入装饰(
图标:图标(图标。货币化),
边框:大纲输入边框(
边界半径:边界半径。圆形(25),
加丁:5),
labelText:“输入本金金额”),
键盘类型:TextInputType.numberWithOptions(),
),
),
大小盒子(
身高:12,
),
容器(
孩子:TextField(
光标颜色:颜色(0xFFFA983A),
控制器:_interestRate,
装饰:
输入装饰(
图标:图标(图标。显示图表),
边框:大纲输入边框(
边界半径:边界半径。圆形(25),
加丁:5),
标签文本:“年利率%”,
键盘类型:TextInputType.numberWithOptions(),
),
),
大小盒子(
身高:12,
),
划船(
儿童:[
灵活的(
弹性:3,
子:容器(
孩子:TextField(
光标颜色:颜色(0xFFFA983A),
控制器:_loandration,
装饰:输入装饰(
图标:图标(图标。日期范围),
边框:大纲输入边框(
边界半径:边界半径。圆形(25),
加丁:5),
labelText:“贷款期限”),
键盘类型:TextInputType.numberWithOptions(),
),
),
),
//待办事项:开关================
灵活的(
弹性:1,
子:列(
儿童:[
正文(
_持续时间类型,
样式:TextStyle(
fontWeight:fontWeight.bold,
),
),
开关(
activeColor:Color(0xFFFA983A),
值:_开关值,
一旦更改:(布尔值){
印刷品(价值);
如果(值){
_持续时间类型=_持续时间类型[1];
}否则{
_持续时间类型=_持续时间类型[0];
}
设置状态(){
_开关值=开关值;
});
}),
],
),
),
],
),
大小盒子(
身高:12,
),
//待办事项:按钮============
灵活的(
孩子:扁平按钮(
填充:从LTRB(48,8,48,8)开始的边缘设置,
按下:手动计算,
子:文本(
“计算”,
样式:TextStyle(颜色:Colors.white),
),
颜色:颜色(0xFFFA983A),
),
),
大小盒子(
身高:12,
),
//TODO:结果
  void _handleCalculation() {
    //    TODO: Amortization
    //    TODO: A = Payment amount per period
    //    TODO: P = Initial Principal (Loan Amount)
    //    TODO: r = interest Rate
    //    TODO: n = Total number of payments

    double A = 0.0;
    double I = 0.0;
    double T = 0.0;
    double P = double.parse(_principalAmount.text);
    double r = double.parse(_interestRate.text) / 12 / 100;
    int n = _durationType == "Year(s)"
        ? int.parse(_loanDuration.text) * 12
        : int.parse(_loanDuration.text);

    A = (P * r * pow((1 + r), n) / (pow((1 + r), n) - 1));
    T = (A * n);
    I = (T - P);

    NumberFormat format = NumberFormat('#,###,###.00');
    setState(() {
      _miResult = format.format(A);
      _tiResult = format.format(I);
      _tcResult = format.format(T);
    });
  }