In order for my app's Alert Dialogs to be consistent, I have created a function that returns an Alert Dialog. The function does a bit of "painting", to make it look the way I want, but its basic structure is:
AlertDialog myAlertDialog({
@required BuildContext context,
@required Widget myContent,
@required Function onAccept,}){
return AlertDialog(
content: Column(
children = [
myContent,
FlatButton(child: Text("ok"), onPressed: onAccept),
FlatButton(child: Text("cancel"), onPressed: Navigator.pop(context),
]
)
);
}
In this particular occasion, I am sending a Form to the myContent parameter. I am currently stuck on how to retrieve the data from the form in myContent and pass it to the function onAccept.
In case it might be helpful, the following is the basic structure of my form:
class myForm extends StatefulWidget {
@override
_myFormState createState() => _myFormState();
}
class _myFormState extends State<myForm> {
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: ListView(
children: [
TextFormField(onChanged: (val) {setState(() {var1 = val;});}, ... ), //Field 1
TextFormField(onChanged: (val) {setState(() {var2 = val;});}, ... ), //Field 2
//...
],
),
);
}
}
Thanks!
EDIT I noticed the following might also be useful/needed in order for my current problem to be more clear:
This is how I call the AlertDialog from the main widget.
class MainWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: FlatButton(
child: Text("launch Alert Dialog",),
onPressed: () {showDialog(
context: context,
builder: (BuildContext context) {
return _launchAlertDialog(context);
},
);},
),
);
}
}
AlertDialog _launchAlertDialog(BuildContext context){
return myAlertDialog(
context: context,
content: myForm(), //Data is captured here
onAccept: (){}, //But I need to use it here
);
}