Flutter Textfield 捕获美元金额并收取费用

Flutter Textfield capture dollar amount and apply a fee

我们有一个 flutter 文本字段,用户可以在其中输入一个 int 的金额,我们想要做的是 int 计算一个金额减去费用。可能是我在这里无法解决我做错了什么

TextFormField(
                          controller: _offerAmountController,
                          textInputAction: TextInputAction.next,
                          decoration: const InputDecoration(
                            hintText: '5-9999',
                            prefixIcon: Icon(Icons.attach_money_rounded),
                            // labelText: 'Your Offer',
                          ),
                          keyboardType: TextInputType.number,
                          onChanged: (offerAmount) {
                            
                            double sum = int.parse(offerAmount) * 0.1;
                            _offer = sum as int;
                          }),

Text(formatCurrency.format(_offer),
                                style: Theme.of(context)
                                    .textTheme
                                    .headlineSmall
                                    .copyWith(color: Colors.blue[700]))

final formatCurrency =
    NumberFormat.simpleCurrency(locale: 'en_AU', decimalDigits: 0);

试试这个:

setState(() {
  _offer = sum.toInt();
});

试试这个

setState(() {
  _offer = (sum).round();
});

或这个

setState(() {
  _offer = sum.toInt();
});

需要下面代码的也可以试试这个

double d = 50.5;

int i = d.toInt(); // i = 50
int i = d.round(); // i = 51
int i = d.ceil();  // i = 51
int i = d.floor(); // i = 50