如何将接收到的值作为参数输入到 TextField Flutter 中

How to input the value received as a parameter into the TextField Flutter

我制作了一个弹出带有 TextField 的对话框的函数。

我想知道如何将接收到的文本作为参数输入到 TextField 中。

Future<String?> openDialog(String title, String text) => showDialog<String>(
      context: context,
      builder: (context) => AlertDialog(
        title: Text(title),
        content: TextField(
          autofocus: true,
          decoration: InputDecoration(
            labelText: '~~',
          ),
          controller: controller,
          onSubmitted: (_) => ok(),
        ),
        actions: [
          TextButton(
              onPressed: ok,
              child: Text('ok')
          )
        ],
      )
  );

设置controller.text = text,希望这有效

@Fahmida 的回答是正确的。我只是要详细说明她的回答

为您的 TextField 创建控制器 TextEditingController _controller = new TextEditingController();

将此 _controller 添加到您的 TextField 中:TextField(controller:_controller),

现在,只要您想为该 TextField 设置文本,请使用:

 setState(() {
  _controller.text = 'Your text';
});

就这些:)

您可以像这样使用 TextEditingController class:
控制器:TextEditingController(text: text),

试试这个

  openDialog({String title, String description}) => showDialog(
  context: context,
  builder: (context) => AlertDialog(
    title: Text(title),
    content: TextField(
      autofocus: true,
      controller: TextEditingController(text: description),
      decoration: InputDecoration(
        labelText: '~~',
      ),
      onSubmitted: (_) => ok(),
    ),
    actions: [
      TextButton(
          onPressed: ok,
          child: Text('ok')
      )
    ],
   )
);

然后调用你的对话框

openDialog(title: " bla bla", description: "Go")