简体   繁体   English

Flutter中TextFormField的输入问题

[英]Input problems of TextFormField in Flutter

I need to update the values of the screen.我需要更新屏幕的值。 When I pushed the button it has to set the values of the screen.当我按下按钮时,它必须设置屏幕的值。 It is doing but when I try to change one field and go to the next field, the first value changes back to the first value.它正在执行,但是当我尝试更改一个字段并转到下一个字段时,第一个值更改回第一个值。 Here is my code part:这是我的代码部分:

class _AccountScreenState extends State<AccountScreen> {
  final api = ApiServices();
  var formKey = GlobalKey<FormState>();
  var emailController = TextEditingController();
  var pwdController = TextEditingController();
  var phoneController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    emailController.text = widget.account.email!;
    phoneController.text = widget.account.phoneNumber!;
...
 child: TextFormField(controller: emailController,
                      decoration: InputDecoration(prefixText: "Email: ",
                                                  prefixStyle: TextStyle(fontWeight: FontWeight.w600),
                                                  labelText: "E-mail",
                                                  border: OutlineInputBorder(borderSide: BorderSide.none),
                                                  fillColor: Colors.white,
                                                  filled: true),
                        ),
...

You can remove your code from Build method because when you use setstate method all Build method Rebuild.您可以从 Build 方法中删除您的代码,因为当您使用 setstate 方法时,所有 Build 方法都是 Rebuild。 so you can just put you code in initstate method.所以你可以把你的代码放在 initstate 方法中。

also you can use late keyword.您也可以使用后期关键字。

late TextEditingController emailController;
  @override
  void initState() {
  emailController = TextEditingController(text: widget.account.email!);
    // TODO: implement initState
    super.initState();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM