【问题标题】:Flutter Dialog as separat file and as a classFlutter Dialog 作为单独的文件和类
【发布时间】:2020-09-15 15:28:46
【问题描述】:

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

它似乎不起作用。我需要使用我在调用类时定义的自定义文本来制作它。因此,当您单击按钮时,信息文本必须是 X,而另一个是 Y。

PopupMenu 文件:

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: [...]
        );
      }
  );
}

【问题讨论】:

  • 当您说“它似乎不起作用”时,可以提供更多详细信息吗?看起来代码很好。
  • 您好,请查看答案。

标签: flutter menu dialog popup popupmenu


【解决方案1】:

您可以查看以下代码。

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();
     },
  )

【讨论】:

  • 哇,非常感谢 :) 工作 100%。我没有提供我的菜单应有的具体方式,但我可以轻松修改您的弹出菜单以满足我的需要:)
猜你喜欢
  • 1970-01-01
  • 2021-02-22
  • 2012-04-02
  • 2020-09-16
  • 1970-01-01
  • 2018-09-11
  • 2023-01-03
  • 1970-01-01
  • 2012-01-09
相关资源
最近更新 更多