《回去》在Flutter中显示黑屏

时间:2018-11-08 11:34:01

标签: android dart flutter

我正在尝试在应用栏中使用Navigator.pop(context);,但问题是它显示黑屏,然后您必须按android上的“后退”按钮,然后弹出当前黑屏,所以黑屏在哪里?因此,在iPhone中没有后退按钮,所以为什么它会卡在该屏幕中。请帮助我

这是我使用此Navigator代码的代码。

 @override
 Widget build(BuildContext context) {
 return new Scaffold(
  backgroundColor: Color.fromRGBO(30, 30, 30, 1.0),
  appBar: new AppBar(
    elevation: 0.0,
    backgroundColor: Color.fromRGBO(30, 30, 30, 1.0),
    actions: <Widget>[
      new IconButton(
          icon: Icon(Icons.settings_power),
          onPressed: () {
            Navigator.pop(context);
          }),
    ],
    title: new Text(
      "PROLOG",
      style: new TextStyle(color: Colors.white),
    ),
    centerTitle: true,
  ),
 );
}

最奇怪的是,我在另一个类中使用了这段代码,但效果很好。那么问题出在哪里...

1 个答案:

答案 0 :(得分:0)

调用Navigator.pop(context)后出现黑屏/黑屏的原因是因为当前Navigator堆栈之外没有小部件/屏幕。

在Flutter中,SystemChannels.platform.invokeMethod('SystemNavigator.pop')用于删除最顶层的Flutter实例。如docs中所述,该方法应从Android的堆栈中删除当前的Activity。但是,这种行为在iOS上有所不同。

如果您尝试实现此功能以关闭应用程序,则不建议这样做。在Apple's archived doc上指出了这一点。我正在尝试搜索更新的参考,但是在Apple的Developer文档中导航非常困难。

无论如何,如果您想尝试一下,我对您的代码段进行了一些更改。

将此添加到您的导入中

import 'dart:io' show Platform, exit;

对于代码,exit(int)用于iOS。建议使用docs中所述的0 ... 127范围内的退出代码。 SystemChannels.platform.invokeMethod('SystemNavigator.pop')用于其他平台(在这种情况下,主要是Android)。

Widget build(BuildContext context) {
  return new Scaffold(
    backgroundColor: Color.fromRGBO(30, 30, 30, 1.0),
    appBar: new AppBar(
      elevation: 0.0,
      backgroundColor: Color.fromRGBO(30, 30, 30, 1.0),
      actions: <Widget>[
        new IconButton(
          icon: Icon(Icons.settings_power),
          // if Platform is iOS call exit(0)
          // else call the preferred method
          // https://api.flutter.dev/flutter/services/SystemNavigator/pop.html
          onPressed: () => Platform.isIOS
              ? exit(0)
              : SystemChannels.platform.invokeMethod('SystemNavigator.pop'),
        ),
      ],
      title: new Text(
        "PROLOG",
        style: new TextStyle(color: Colors.white),
      ),
      centerTitle: true,
    ),
  );
}

演示

enter image description here