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

guruprakash gupta picture guruprakash gupta · Oct 23, 2019 · Viewed 8.5k times · Source

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.

Answer

Kirill Bubochkin picture Kirill Bubochkin · Oct 23, 2019

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())