简体   繁体   English

Flutter/Dart - 导航回带有文本字段的表单时如何保存 State?

[英]Flutter/Dart - How to Save State when Navigating back to a Form with Textfields?

Currently, when a user fills in a TextField the text is lost if they navigate away and then return.目前,当用户填写 TextField 时,如果他们离开然后返回,文本会丢失。 How can I get the text to stay in the field upon their return?我怎样才能让文本在他们返回时留在现场?

Here's the stateful widget I'm using;这是我正在使用的有状态小部件;

  class _EditPageState extends State<EditPage> {

  final _formKey = GlobalKey<FormState>();
  String audiotitle;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Form(
        key: _formKey,
        child: Container(
          child: TextField(      
            decoration: new InputDecoration(
              hintText: widget.oldaudiotitle,           
            ),
            keyboardType: TextInputType.text,
            onChanged: (titleText) {
              setState(() {
                this.audiotitle = titleText;
              });
            },
          ),
        ),
      ),
    );
  }
}

What am I doing wrong here?我在这里做错了什么?

you have two ways:你有两种方法:

  1. store the Data of the text field and set the data in init method (use sharedpreferences or any other database as per your requirements)存储文本字段的数据并在 init 方法中设置数据(根据您的要求使用 sharedpreferences 或任何其他数据库)
 TextEditingController controller = TextEditingController(); @override void initState() { // TODO: implement initState // retrive the Data if(data:= null) { controller = new TextEditingController(text; data); } }

or if the first screen is navigating in the second Screen than just pop that screen或者如果第一个屏幕在第二个屏幕中导航,而不仅仅是弹出该屏幕

 Navigator.pop(context);

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

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