首页 > 解决方案 > 如何在 Flutter 中保存 TextFormField 中的 int 数据

问题描述

我做了一些TextFormField,我想在int按下 时保存数据FlatButton。当我按 时FlatButton,我要他检查TextFormField支出和储蓄的总和是否不大于TextFormField收入。如果TextTormField费用和储蓄的总和更大,我想errortext在 textformfield 储蓄下显示“你的开支和储蓄大于你的收入”

class BigNotePage extends StatefulWidget {
  @override
  _BigNotePageState createState() => _BigNotePageState();
}

class _BigNotePageState extends State<BigNotePage> {
  final _formKey = GlobalKey<FormState>();
  String _income;
  String _expenses;
  String _savings;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: kPading,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          TitlePage('Big Note'),
          Expanded(
            child: Form(
              key: _formKey,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  TxtField(
                    label: 'Income',
                    function: (value) => _income = value,
                  ),
                  TxtField(
                    label: 'Expenses',
                    function: (value) => _expenses = value,
                  ),
                  TxtField(
                    label: 'Savings',
                    function: (value) => _savings = value,
                  ),
                  FlatButton(
                    onPressed: () {
                      int.parse(_income) >=
                              int.parse(_expenses) + int.parse(_savings)
                          ? _formKey.currentState.save()
                          : print('null');
                    },
                    child: Text(
                      'WRITE THAT',
                      style: TextStyle(letterSpacing: 1.25),
                    ),
                    color: Colors.yellow,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(30.0),
                    ),
                  ),
                ],
              ),
            ),
          ),
          Container(
            width: 250.0,
            child: Text(
              '*if you get another income for this mounth, input the income again.',
              style: TextStyle(fontSize: 12.0),
            ),
          ),
        ],
      ),
    );
  }
}

class TxtField extends StatelessWidget {
  TxtField({this.label, this.function});

  final String label;
  final Function function;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 8.0),
      child: TextFormField(
        onSaved: function,
        keyboardType: TextInputType.numberWithOptions(decimal: true),
        decoration: InputDecoration(
          labelText: label,
          prefix: Container(
            padding: EdgeInsets.all(8.0),
            child: Text(
              'IDR',
              style:
                  TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
            ),
          ),
        ),
      ),
    );
  }
}

我的应用看起来像这样

标签: flutter

解决方案


将字符串值替换为控制器值

final _incomeController = TextEditingController();
final _expenseController = TextEditingController();
final _savingController = TextEditingController();

另外,添加

布尔_验证=假;

将 String 值转换为 TextField 的 int:

        TextField(
                controller: _incomeController,
                label: 'Income',
                 decoration: InputDecoration(
                  errorText: _validate ? 'Your message' : null,
                 ),
              ),
              TextField( 
                controller: _expenseController ,
                label: 'Expenses',
                  decoration: InputDecoration(
                  errorText: _validate ? 'Your message' : null,
                  ),
              ),
              TextField(
               controller: _savingController,
                label: 'Savings',
                 decoration: InputDecoration(
                  errorText: _validate ? 'Your message' : null,
                 ),
              ),

现在 FlatButton 逻辑:

              onPressed: () {

                     String _income = _incomeController.text;
                     String _expenses = _expenseController.text;
                     String _savings = _savingController.text;
  
                      int.parse(_income) >=
                              int.parse(_expenses) + int.parse(_savings)
                          ? _formKey.currentState.save()
                          : print('null');
                    },

休息在任何你想要的地方应用你的逻辑。


推荐阅读