0

After I clicked the button in the dialog, the content would change from a list of buttons into a form with textfields. However, I am frustrated in codes to achieve it.

3
  • Are you tried with stateful dialog? Commented Sep 6, 2021 at 6:48
  • must be stateful but I am frustrated in how I could do it Commented Sep 6, 2021 at 6:58
  • 1
    please provide more code snippets to understand your problem in detail. Commented Sep 6, 2021 at 6:58

1 Answer 1

1

You just need a stateful dialog and a bool state. Here is an example:

class MyDialog extends StatefulWidget {
  const MyDialog({Key? key}) : super(key: key);

  @override
  MyDialogState createState() => MyDialogState();
}

class MyDialogState extends State<MyDialog> {
  /// When this value is false, it shows list of buttons
  /// When this value is true, it shows list of textfields
  bool isForm = false;

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text('AlertDialog Title'),
      // Here, we conditionally change content
      content: isForm
          ? Column(
              children: [
                TextFormField(),
                TextFormField(),
                TextFormField(),
                TextFormField(),
              ],
            )
          : Column(
              children: [
                TextButton(onPressed: () {}, child: Text('Button')),
                TextButton(onPressed: () {}, child: Text('Button')),
                TextButton(onPressed: () {}, child: Text('Button')),
                TextButton(onPressed: () {}, child: Text('Button')),
              ],
            ),
      actions: [
        TextButton(
          // Here isForm is switched to change the content
          onPressed: () => setState(() => isForm = !isForm),
          child: const Text('Switch'),
        ),
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: const Text('Cancel'),
        ),
        TextButton(
          onPressed: () => Navigator.pop(context, true),
          child: const Text('OK'),
        ),
      ],
    );
  }
}

Use it with showDialog(context: context, builder: (context) => MyDialog())

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.