Here, I have a utility class in which I have a function for showing DialogBox, so I'm trying to make an AlertDialog Box which can be used anywhere in the whole project.
So, I have to pass Title, description as an argument and also want to pass Class name so that when the button inside the alert dialog is pressed we can navigate to that screen
class DialogBox {
static DialogBox dialogBox = null;
static DialogBox getInstance() {
if (dialogBox == null) {
dialogBox = DialogBox();
}
return dialogBox;
}
showAlertDialog(BuildContext context, String alertTitle, String alertMessage) {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
title: Center(child: Text(alertTitle)),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
alertMessage,
textAlign: TextAlign.center,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
FlatButton(
child: Center(
child: Text(
'Ok',
textAlign: TextAlign.center,
)),
onPressed: () {
Navigator.of(context).pop();
// Navigator.of(context).push(MaterialPageRoute(
// builder: (BuildContext context) {
// return Home();//Intead of giving Home() anything can be passed here
// }));
},
),
])
],
),
);
});
}
}
Right now I have just kept closing the dialog box but there I want to navigate to another class.
Steps to Pass Data to Stateful Widget in Flutter To pass data to stateful widget, first of all, create two pages. Now from the first page open the second page and pass the data. Inside the second page, access the variable using the widget. For example widget.
Passing class name is a bad idea – class can require parameters for constructor, it's not type safe, and it requires reflection, which Flutter doesn't support.
You can instead pass a function that will create a widget of needed type:
showAlertDialog(
BuildContext context,
String alertTitle,
String alertMessage,
Widget Function() createPage,
) {
// ...
onPressed: () {
Navigator.of(context).pop();
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) {
return createPage();
}));
},
// ...
}
and call it e.g. like this:
showAlertDialog(context, title, message, () => Home())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With