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.
-
Are you tried with stateful dialog?Gursewak Singh– Gursewak Singh2021-09-06 06:48:09 +00:00Commented Sep 6, 2021 at 6:48
-
must be stateful but I am frustrated in how I could do itKYflutterrookie– KYflutterrookie2021-09-06 06:58:01 +00:00Commented Sep 6, 2021 at 6:58
-
1please provide more code snippets to understand your problem in detail.Tushar Patel– Tushar Patel2021-09-06 06:58:38 +00:00Commented Sep 6, 2021 at 6:58
Add a comment
|
1 Answer
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())