如何获取文本字段的值并在文本中但在另一个屏幕中返回它?

问题描述

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Todo Add'),centerTitle: true,),body: Container(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 12,vertical: 10),child: Column(
            children: [
              TextField(
                controller: controller,onChanged: (value) => title = value,decoration: Inputdecoration(hintText: 'Enter your title'),SizedBox(
                height: 10,TextField(
                controller: controller1,onChanged: (val) => body = val,decoration: Inputdecoration(hintText: 'Enter your body'),SizedBox(
                height: 40,ElevatedButton(
                onpressed: () {
                  setText();
                },child: Text("Submit"),Text(
                'Title : $text',style: TextStyle(fontSize: 16),SizedBox(height: 10,Text(
                'Body : $desc',],);
  }

解决方法

您可以使用控制器获取 TextField 的文本。

String textfield1 = controller.text;

并将这个变量像参数一样发送到另一个屏幕

Navigator.pushNamed(
      context,route,arguments: [controller.text,controller1.text]
    );

并在另一个屏幕中获取此参数

final args = ModalRoute.of(context).settings.arguments;