flutter - 如何在 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),
),
),
),
),
);
}
}
解决方案
将字符串值替换为控制器值
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');
},
休息在任何你想要的地方应用你的逻辑。
推荐阅读
- python - groupby中特定行的Python pandas差异
- windows - Cmake 无法链接到 SFML
- node.js - Connections 6.5 组件包搜索 API 调用(OrientMe)中来自 IHS 的错误请求/无效主机名
- android - 与现有项目集成时,GiftedChat 屏幕不可见
- sql - 找出所有能被 7 整除但不是 5 倍数的数
- laravel - Laravel 的刀片模板
- elasticsearch - 在 ElascicSearch 中查找空嵌套归档文档
- javascript - 在 Angular 中重写 AJAX 调用
- integration - 工作日集成任务失败
- python-3.x - 从不同的烧瓶路由渲染时,客户端无法连接到烧瓶套接字服务器 [已解决]