Flutter Dialog 作为单独的文件和 class

Flutter Dialog as separat file and as a class

在我的应用程序中,我有一个弹出菜单,该菜单在应用程序外有几个触发器。所以我试着把它做成一个单独的文件,然后在我需要的时候调用它。

好像不行。我需要使用我在调用 class 时定义的自定义文本来完成此操作。所以说 info-text 在你点击按钮时必须是 X,而在另一个按钮上是 Y。

弹出菜单文件:

import [...]

void customWebLaunch(command) async {
 [...]
}

class PopUpMenu extends StatelessWidget {

  final String title;
  final String info;

  const PopUpMenu({Key key, @required this.title, @required this.info})
      : assert(title != null),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder (
        borderRadius: BorderRadius.circular(borderRadius*3/2),
      ),
      child: [...]
    );
  }
}

我也试过这个,但是我无法输入自定义标题和文本:

[under Widget build{  [...]  }]

sendEmailPopUp(BuildContext context) {
  showDialog(
      context: context,
      builder: (BuildContext context){
        return Dialog(
          shape: RoundedRectangleBorder (
            borderRadius: BorderRadius.circular(borderRadius*3/2),
          ),
          child: [...]
        );
      }
  );
}

您可以查看下面的代码。

class test   {
  final BuildContext context;
  final bool crossButton;
  // ignore: non_constant_identifier_names
  final bool cross_Button_Left;
  final double message_to_button_gap;

  test({
    Key key,
    @required this.context,
    this.crossButton = false,
    this.cross_Button_Left = false,
    this.message_to_button_gap = 18.0,
  });
  @override
  Future show() => showDialog(
      context: this.context,
      builder: (BuildContext context) {
        return Theme(
          data: ThemeData(dialogBackgroundColor: Colors.lightGreenAccent),
          child: AlertDialog(
            elevation: 10,
            clipBehavior: Clip.antiAliasWithSaveLayer,
            title: Column(
              children: <Widget>[
                crossButton
                    ? Align(
                        alignment: cross_Button_Left
                            ? Alignment.topRight
                            : Alignment.topLeft,
                        child: InkWell(
                          child: Icon(Icons.close),
                          onTap: () {
                            Navigator.pop(context);
                          },
                        ),
                      )
                    : Container(),
                crossButton
                    ? SizedBox(
                        height: 20,
                        width: 8,
                      )
                    : Container(),
                Icon(
                  Icons.dashboard,
                  color: Colors.red,
                  size: 22,
                ),
                SizedBox(
                  height: 8,
                  width: 8,
                ),
                new Text(
                  "message",
                  style: TextStyle(fontSize: 22, color: Colors.black),
                ),
                SizedBox(
                  height: message_to_button_gap,
                  width: 8,
                ),
                FloatingActionButton.extended(
                  onPressed: () async {
                    Navigator.pop(context);
                  },
                  backgroundColor: Color(0xff8154b7),
                  label: Text(
                    "buttonName",
                    style: TextStyle(color: Color(0xff230238)),
                  ),
                ),
              ],
            ),
          ),
        );
      });
}

然后从另一个文件调用它,如下所示。

 RaisedButton(
    child: Text("abc"),
    onPressed: () {
       PositiveAlert(context: context, messageIcon: false).show();
     },
  )