gpt4 book ai didi

android - 返回在 Flutter 中显示黑屏

转载 作者:行者123 更新时间:2023-12-03 02:48:24 33 4
gpt4 key购买 nike

我正在尝试使用 Navigator.pop(context);在应用程序栏中,但问题是它显示黑屏,然后您必须按 android 上的后退按钮,然后它会弹出当前黑屏,所以这个黑屏来自哪里,我不知道,在 iPhone 中有没有后退按钮,所以为什么它卡在那个屏幕上。请帮帮我

这是我使用此导航器代码的代码。

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

最奇怪的是我在另一个类中使用这段代码它工作正常。那么问题出在哪里...

最佳答案

调用 Navigator.pop(context) 后出现黑屏/黑屏的原因是因为当前 Navigator 之外没有小部件/屏幕堆。
在 flutter 中,SystemChannels.platform.invokeMethod('SystemNavigator.pop')用于移除最顶层的 Flutter 实例。如 docs 中所述,该方法应该从 Android 的堆栈中删除当前的 Activity。不过,iOS 上的行为有点不同。
如果您尝试实现此功能以关闭应用程序,强烈建议不要这样做 .这在 Apple's archived doc 上已指出.我正在尝试搜索更新的引用资料,但浏览 Apple 的开发人员文档具有挑战性。
无论如何,如果您想尝试一下,我已经对您的代码 fragment 进行了一些更改。
将此添加到您的导入中

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

关于android - 返回在 Flutter 中显示黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53206928/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com