简体   繁体   English

是否可以在 Flutter 中的 function 中将 class 作为参数传递?

[英]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.在这里,我有一个实用程序 class,其中我有一个 function 用于显示 DialogBox,所以我正在尝试制作一个 AlertDialog Box,它可以在整个项目的任何地方使用。

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 名称,以便在按下警报对话框内的按钮时,我们可以导航到该屏幕

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.现在我一直关闭对话框,但我想导航到另一个 class。

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.传递 class 名称是个坏主意 - class 可能需要构造函数的参数,它不是类型安全的,并且需要反射,而 Flutter 不支持。

You can instead pass a function that will create a widget of needed type:您可以改为传递 function ,它将创建所需类型的小部件:

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 eg like this:并像这样称呼它:

showAlertDialog(context, title, message, () => Home())

Thanks.谢谢。 I was looking exactly for this.我正是在寻找这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM