What I am trying to do is showing a Dialog
that consists of a Form
within it to the user, then user fills out the 2 TextFormFields
and clicks on the Next ElevatedButton
. Then another similar Dialog/form
will be appear and user can do the same thing until he wants to finish and press the Submit ElevatedButton
(i.e. One user may want to add 3 question/answer and another user 10). Moreover user can click on the Back ElevatedButton
and review the previous Dialog/Form
s.
T have the following code but it doesn’t work because it can not keep the previous form states and also can’t submit the form:
class _FormQuestionsState extends State<FormQuestions> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
children: [
TextFormField(
decoration: const InputDecoration(
labelText: 'Queation',
border: OutlineInputBorder(),
),
validator: (value) {
if (value!.isEmpty) return 'Please enter the title of the question';
},
),
TextFormField(
minLines: 3,
maxLines: 5,
decoration: const InputDecoration(
labelText: 'Answer',
border: OutlineInputBorder(),
),
validator: (value) {
if (value!.isEmpty || value.length < 30) {
return 'Answer must be longer';
}
},
),
Padding(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
flex: 1,
child: ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
showDialog(
context: context,
builder: (context) =>
const Dialog(child: FormQuestions()));
},
child: Text('Back'),
),
),
Expanded(
flex: 2,
child: ElevatedButton(
onPressed: null, // This function should save all the information has been already put on all the forms!
child: Text('Submit'),
),
),
Expanded(
flex: 1,
child: ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
showDialog(
context: context,
builder: (context) =>
const Dialog(child: FormQuestions()));
},
child: Text('Next'),
),
),
],
),
),
],
),
),
);
}
}
I don’t know how to modify it to reach what I want.
2
Answers
You can loop through the dialogs in following way and save the result.
Use TextEditingController(),
use some unique string or index for question/answer as key