如何在 Flutter 中的 EMI Calculator 中使用分隔符显示货币格式

狮鹫鱼

我在颤振中构建了一个 EMI 计算器,但我的结果显示为 eg1250568.00但希望它显示为N$ 1,250,568.00

我已经尝试了 intl 包,但在Text(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());
  }
}

该代码完全可以重现,就像我在我的应用程序中所拥有的一样......我希望结果 ( miResults,tiResultstcResults) 以金融/货币格式显示。谢谢你。

理查德·希普

试试这个:

  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);
    });
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 Flutter 的 material_tag_editor 中使用“Enter”作为分隔符

如何在swift2中使用打印的分隔符参数

如何在堆叠图中使用 scale_color_discrete 格式化图例并在 y 轴 r 中包含千位分隔符

如何在 Flutter 中显示日期或时间?

如何在 Flutter 中显示 AlertDialog

如何在Flutter中并排显示按钮?

如何在 Flutter 中显示 Android Activity?

如何在 Flutter 中显示 SnackBar?

Flutter 如何在 alertDialog 中显示视频

如何在Flutter的BottomNavigationBar中显示图像?

如何在 Flutter 中显示 ipfs 图像

如何从 Flutter 中的 DataTable 小部件中删除默认的底部分隔符

如何在Flutter中的StaggeredGridView中使用堆栈小部件?

Flutter中如何在ListView.builder中使用ListView

如何在Flutter中的水平ListView中使用ListTile?

如何在Flutter中使用Cardview中的Listview?

如何在 Flutter 中使用 ListView 和 ListTile 中的列表

如何在Flutter中在TextField中使用onKeyUp事件

如何在 Flutter 中的 BottomNavigationBar 项中使用 setState()?

如何在 Flutter 中格式化 DateTime,如何在 Flutter 中获取当前时间?

如何在SSRS中编写100k分隔符的格式表达式?

如何在Rust中更改格式化程序的小数点分隔符?

Flutter DrawerHeader // 如何去掉分隔符

如何在 Flutter 中全屏显示 PageView 中的容器?

如何在Flutter中显示流中的小吃店?

如何在FLUTTER中的步进器中显示列表

如何在flutter中从sqlite在SmoothStarRating中显示评级

如何在 Flutter 中的 Text 小部件中显示值?

如何在 Flutter 中以 24 小时格式显示当前时间,而不使用 AM 或 PM?