android - Navigator.pop 不会在 flutter 中关闭 simpledialog

标签 android flutter navigator showdialog simpledialog

我正在使用 Flutter 构建 Android 应用程序。我在以编程方式关闭简单对话框时遇到问题。

现在我有一个名为 ListVessel 的有状态页面。此页面包含数组 otherVessels 中的 listTile。
以下是此页面的代码。

class ListVessel extends StatefulWidget {
  final Function() notifyParent;
  ListVessel({Key key, @required this.notifyParent}) : super(key: key);

  @override
  _ListVesselState createState() => _ListVesselState();
}

class _ListVesselState extends State<ListVessel> {
  @override
  Widget build(BuildContext context) {
      return ListView.separated(
        separatorBuilder: (context, index) => Divider(color: Colors.blueGrey),
        itemCount: otherVessels.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text("Name: "+otherVessels[index]["shipName"]),
            onTap: () {
              showDialog (
                  context: context,
                  builder: (_){
                    return otherTap(idx:index);
                  }
              );
            }
          );
        },
      );
    }
  }
}

从上面的代码中,可以点击每个瓦片(容器)并调用 otherTap() 方法。 otherTap() 方法显示一个简单的对话框(弹出窗口),其中包含被点击容器的详细信息。
下面是 otherTap() 的代码。

class otherTap extends StatefulWidget{
  otherTap({Key key, @required this.idx}) : super(key: key);
  final int idx;
  @override
  _otherTapState createState() => new _otherTapState();
}

class _otherTapState extends State<otherTap>{
  @override
  Widget build(BuildContext context){
  _isDialogShowing = true;
       return SimpleDialog(
        title: Text(otherVessels[widget.idx]["shipName"]),
        children: <Widget>[
          SimpleDialogOption(
            child: Text('MMSI : ' + otherVessels[widget.idx]['MMSI']),
          )
        ],
      );
  }
}

如果对话框正在显示,我有一个全局 bool 变量 (_isDialogShowing) 来保持跟踪。
现在我希望显示对话框(弹出窗口)在 5 秒后关闭。
我使用 Navigator.pop() 关闭 MyApp 函数中的对话框。我把它放在 setstate() 函数中。

void main() {
  runApp(
    MyApp(storage: CounterStorage()),
  );
}

class MyApp extends StatefulWidget {
  MyApp({Key key, @required this.storage}) : super(key: key);
  final CounterStorage storage;
  @override
  State<StatefulWidget> createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
  final appTitle = 'Testing applicatin';

  void _update(BuildContext context) async {
    await Future.delayed(Duration(milliseconds: 5000));

    setState(() {
      if(_isDialogShowing){
        _isDialogShowing = false;
         Navigator.pop(context);
         //Navigator.of(context).pop();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    _update(context);
    return new WillPopScope(
      onWillPop:  null,
      child: new MaterialApp(
        debugShowCheckedModeBanner: false,
        title: appTitle,
        home: MyHomePage(title: appTitle),
        routes: {
          Routes.home: (context) => MyHomePage(),
          Routes.settings: (context) => SettingsPage(),
        },
      ),
    );
  }
}

但是上面的 navigator.pop 方法不会关闭弹出窗口。
谁能帮忙?

最佳答案

您需要在 showDialog() 的构建器中收到的上下文调用 pop,只有这样,由 showDialog() 创建的对话框才会弹出。

将您的 showDialog() 替换为以下内容,它将为您工作:

showDialog(
  context: context,
  builder: (BuildContext context) {
    Future.delayed(Duration(seconds: 5)).then((_) {
      Navigator.pop(context);
    });
    return otherTap(idx:index);
  },
);

关于android - Navigator.pop 不会在 flutter 中关闭 simpledialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59385404/

相关文章:

flutter - 如何修复http put请求上的错误

ios - Flutter 使用嵌套导航器向后滑动手势

android - 将带有对象的 ArrayList 传递给新的 Activity?

android - 每个列表项打开不同的 Activity

android - 将应用程序背景设置为与主屏幕壁纸相同

Flutter 导航键 + GlobalKey : "method ' pushedName' was called on null"

ios - 如何在 iOS 中使用 Wix 导航器关闭 React Native 中的当前模态后立即推送新模态?

java - 使用 String 变量将 SVG 动态添加到 ImageView

flutter - 如何摆脱Container固定宽高引起的报错

flutter - 'flutter pub get' 在 VSCode 中不起作用