opvsvjoo
opvsvjoo

Reputation: 57

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

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

Upvotes: 1

Views: 1436

Answers (4)

Yakubu
Yakubu

Reputation: 1089

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")

Upvotes: 0

Aaron Katema
Aaron Katema

Reputation: 1

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

Upvotes: 0

Vishnu E R
Vishnu E R

Reputation: 105

@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 :)

Upvotes: 0

Fahmida
Fahmida

Reputation: 1210

Set controller.text = text, Hope this works

Upvotes: 3

Related Questions