【问题标题】:AlertDialog should show another AlertDialog but it's not happening till i close it and open it againAlertDialog 应该显示另一个 AlertDialog 但直到我关闭它并再次打开它才发生
【发布时间】:2020-08-23 11:52:35
【问题描述】:

我想在单击其中一个子项时显示另一个 AlertDialog

但是当我点击它时 显示它,直到我关闭警报并再次打开它

我想导航到 Second AlertDialog 而不关闭它

我们将不胜感激

任何让它打开另一个对话框的方法或关闭它并再次打开它的方法

这是代码

          padding: const EdgeInsets.only(top: 12.0),
          child: ListView(children: [
            FutureBuilder<DropDown>(
                future: getDropData(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    DropDown dropdown = snapshot.data;
                    return RaisedButton(
                      color: maincolor,
                      splashColor: accentcolor,
                      onPressed: () {
                        showDialog(
                            context: context,
                            useSafeArea: true,
                            child: Center(
                              child: Padding(
                                padding: const EdgeInsets.only(top: 20),
                                child: ListView.builder(
                                    itemCount: dropdown.categories.length,
                                    itemBuilder: (context, index) {
                                      return Padding(
                                        padding: const EdgeInsets.only(
                                            top: 8.0, right: 8, left: 8),
                                        child: Container(
                                         
                                          ),
                                          child: FlatButton(
                                              onPressed: () {
                                                setState(() {
                                                  categoryID = dropdown
                                                      .categories[index]
                                                      .categoryId;
                                                });
                                                getDropData();
                                              },
                                              child: Text(
                                                dropdown.categories[index].name,
                                               
                                              )),
                                        ),
                                      );
                                    }),
                              ),
                            ));```

【问题讨论】:

  • 警报对话框代码在哪里?
  • 点击 ctrl+f 并输入 showDialog - 试图评论但字符太长

标签: flutter dart


【解决方案1】:

我认为您只需要使用setState() 来包装showDialog(),但我无法测试它,因为我没有DropDown 类。

编辑

我尝试了一个简单的结构,它工作正常。正如Dung Ngo 提到的,只需使用StatefulBuilder 来构建第一个AlertDialog 小部件的内容。在内部触发另一个 showDialog() 以调出第二个 AlertDialog 小部件。

class _YourWidgetState extends State<YourWidget> {

  AlertDialog alert = AlertDialog(content: Center(child:Text("Second Alert Dialog")));

  @override
  Widget build(BuildContext context) {
    return RaisedButton(onPressed: (){
      showDialog(
        context: context,
        builder: (_) => AlertDialog(
          content: StatefulBuilder(
            builder: (BuildContext context, StateSetter setState){
              return Column(
                children: <Widget>[
                  RaisedButton( onPressed: (){
                    showDialog(context: context, builder: (_) => alert);
                  }),
                  RaisedButton(onPressed: (){
                    showDialog(context: context, builder: (_) => alert);
                  }),
                ],
              );
            }
          ),
        )
      );
    });
  }
}

结果

【讨论】:

  • 如果您希望我编辑问题以添加模型代码,请告诉我同样的问题。
【解决方案2】:

setState 在您关闭第一个之前不起作用,因为它属于主页的上下文,而不是您的第一个对话框的上下文。

您可以使用 StatefulBuilder 创建一个 StateSetter,它根据第一个对话框的上下文调用重建:https://api.flutter.dev/flutter/widgets/StatefulBuilder-class.html

【讨论】:

  • 我认为StatefulBuilder 确实是您所需要的。我编辑了我的答案,检查它是否符合您的需要。 @AbdelrahmanM.Elmarakby
  • 谢谢,Dung,我好像不太理解,谢谢你的努力,继续努力,乐于助人
  • 很高兴它对您有所帮助。如果您的问题得到解决,您应该将我或@Kennith 的答案标记为您的问题的正确答案
【解决方案3】:

感谢 Dung Ngo 和 Kennith 的回答,这对我很有帮助,我从你的回答中学到了很多,顺便说一句,你的回答是正确的

我做了什么

打开第二个对话框的方法

    showGeneralDialog(
      barrierLabel: "Barrier",
      barrierDismissible: true,
      barrierColor: maincolor,
      transitionDuration: Duration(milliseconds: 200),
      context: context,
      pageBuilder: (_, __, ___) {
        return FutureBuilder<Manufacturer>(
            future: getManufucturer(categoryID, parentID),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                Manufacturer dropdown = snapshot.data;

                return Container(
                  height: 400,
                  width: 200,
                  child: ListView.builder(
                    itemCount: dropdown.manufacturers.length,
                    itemBuilder: (context, index) {
                      return Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 60),
                        child: RaisedButton(
                            child: Text(
                              dropdown.manufacturers[index].name,
                              style: GoogleFonts.cairo(
                                  color: maincolor,
                                  fontSize: 14,
                                  fontWeight: FontWeight.bold),
                            ),
                            onPressed: () async {

                              parentID =
                                  dropdown.manufacturers[index].manufacturerId;
                              manufacturerID = parentID;
                              print(parentID);

                              Manufacturer newDropDown =
                                  await getManufucturer(categoryID, parentID);

                              Navigator.pop(context);
                              Navigator.pop(context);
                            }),
                      );
                    },
                  ),
                );
              } else {
                return Center(
                  child: CircularProgressIndicator(
                    strokeWidth: 12,
                    backgroundColor: maincolor,
                  ),
                );
              }
            });
      },
      transitionBuilder: (_, anim, __, child) {
        return ScaleTransition(
          scale: Tween(begin: 0.0, end: 1.0).animate(anim),
          child: child,
        );
      },
    );
  }

然后我创建了打开第一个对话框的按钮,然后我创建了那个对话框

调用我之前定义的方法

再次感谢你的努力 Dung 和 kennith 你真的帮助了我

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多