Home > Software design >  How to input the value received as a parameter into the TextField Flutter
How to input the value received as a parameter into the TextField Flutter

Time:04-07

I made a function that pops up a dialog with a TextField.

I want to know how to input the text received as a parameter into a 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')
          )
        ],
      )
  );

CodePudding user response:

Set controller.text = text, Hope this works

CodePudding user response:

@Fahmida's answer is correct. I am just going to elaborate her answer

Create a controller for your TextField TextEditingController _controller = new TextEditingController();

Add this _controller to your TextField as : TextField(controller:_controller),

Now whenever you want to set a text to that TextField use :

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

Thats all :)

CodePudding user response:

You can use TextEditingController class like this:
controller: TextEditingController(text: text),

CodePudding user response:

Try this

  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')
      )
    ],
   )
);

then call your dialog

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