Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass class as a parameter in a function in Flutter?

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.

like image 341
guruprakash gupta Avatar asked Oct 23 '19 11:10

guruprakash gupta


People also ask

How do you pass parameters to widgets in flutter?

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.


1 Answers

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())
like image 61
Kirill Bubochkin Avatar answered Sep 19 '22 18:09

Kirill Bubochkin