flutter - 如何设置表单字段的当前值,例如具有当前用户详细信息的配置文件更新表单
问题描述
我有我的更新用户资料。我希望在用户可以进行更改之前将初始用户配置文件值填充到表单字段中作为初始值。
**这是一个表单域**
final TextEditingController _firstNameController = TextEditingController();
Widget _buildFirstNameInput() {
return TextFormField(
controller: _firstNameController,
style: inputTextStyle,
decoration: formInputDecoration.copyWith(
labelText: 'First Name',
),
validator: (String value) {
print(value);
if (value.isEmpty) {
return 'First name is Required';
}
return null;
},
onChanged: (String value){
setState(() {
isEdited=true;
});
},
);
}
当用户导航到此编辑配置文件小部件时,用户配置文件实例作为参数传递给小部件
class ProfilePage extends StatefulWidget {
final User userProfile;
ProfilePage({this.userProfile});
...some other code...
我已经尝试通过使用 initstate 中输入字段的控制器来设置输入字段的初始值,如下所示
void intitState(){
super.initState();
_firstNameController.text=widget.userProfile.name;
}
但这似乎不起作用。我怎样才能实现它?
解决方案
TextEditingController.text
在你的设置initState()
是完全有效的。
完整的源代码
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: ProfilePage(
userName: 'Thierry',
),
),
);
}
class ProfilePage extends StatefulWidget {
final String userName;
ProfilePage({this.userName});
@override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
final TextEditingController _firstNameController = TextEditingController();
@override
void initState() {
super.initState();
_firstNameController.text = widget.userName;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: _buildFirstNameInput()),
);
}
Widget _buildFirstNameInput() {
return TextFormField(
controller: _firstNameController,
decoration: InputDecoration(
labelText: 'First Name',
),
validator: (String value) {
print(value);
if (value.isEmpty) {
return 'First name is Required';
}
return null;
},
onChanged: (value) {},
);
}
}
!!!另一个答案是错误的
如果您TextEditingController.text
在开头设置 ,_buildFirstNameInput()
它将在每次重建时重置表单字段:
轻松复制粘贴的完整源代码:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: ProfilePage(
userName: 'Thierry',
),
),
);
}
class ProfilePage extends StatefulWidget {
final String userName;
ProfilePage({this.userName});
@override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
final TextEditingController _firstNameController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: [
_buildFirstNameInput(),
const SizedBox(height: 48.0),
ElevatedButton(
onPressed: () {
setState(() {});
},
child: Text('MESS AROUND WITH THE STATE'),
),
],
)),
);
}
Widget _buildFirstNameInput() {
_firstNameController.text = widget.userName;
return TextFormField(
controller: _firstNameController,
decoration: InputDecoration(
labelText: 'First Name',
),
validator: (String value) {
print(value);
if (value.isEmpty) {
return 'First name is Required';
}
return null;
},
onChanged: (value) {},
);
}
}
推荐阅读
- c++ - 在 C++ 中使用 2D 矢量时出现明显的内存泄漏
- excel - 为什么我的 Excel VBA 代码在执行 Range("K2:K" & LR).Calculate 函数时不断重启宏?
- python - 从 plotly 保存图像
- haskell - 函数的类型定义
- c# - 发送端口 Web HTTP 中的 BizTalk REST 获取方法
- bash - 在 gnu / linux 中,我如何将文件从初始字符串(foo)扫描到最终字符串(bar)
- python - Python 字节文字
- django - 删除设置为在表单集中删除的行并重复问题
- php - 带有两个按钮和 ajax 的 Sumbit 表单
- powershell - 将文件从父文件夹传输到多个子文件夹