Has*_*sef 70
下面我完美地工作在这两个Android和iOS,我用exit(0)从dart:io
import 'dart:io';
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new ... (...),
floatingActionButton: new FloatingActionButton(
onPressed: ()=> exit(0),
tooltip: 'Close app',
child: new Icon(Icons.close),
),
);
}
Run Code Online (Sandbox Code Playgroud)
更新2019年1月优先 解决方案是:
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
Run Code Online (Sandbox Code Playgroud)
如上所述这里
Col*_*son 22
你可以这样做SystemNavigator.pop().
Bla*_*nka 11
已经提供了答案,但是请不要在不知道自己在做什么的情况下将其复制粘贴到代码库中:
如果您使用的SystemChannels.platform.invokeMethod('SystemNavigator.pop');注意,DOC明显提的是:
指示系统导航器从堆栈中删除此活动并返回上一个活动。
在iOS上,对此方法的调用将被忽略,因为Apple的人机界面指南指出应用程序不应自行退出。
您可以使用exit(0)。这将立即使用给定的退出代码终止Dart VM进程。但是请记住该文档说:
这不会等待任何异步操作终止。因此,使用出口很可能会丢失数据。
无论如何,该文档还指出SystemChannels.platform.invokeMethod('SystemNavigator.pop');:
与调用dart:io的exit方法相比,此方法应该更为可取,因为后者可能导致基础平台的运行就像应用程序崩溃一样。
因此,请记住您在做什么。
小智 8
我更喜欢使用
Future.delayed(const Duration(milliseconds: 1000), () {
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
});
Run Code Online (Sandbox Code Playgroud)
虽然 exit(0) 也可以工作,但应用程序突然关闭并且看起来不太好,似乎应用程序崩溃了。
Future.delayed(const Duration(milliseconds: 1000), () {
exit(0);
});
Run Code Online (Sandbox Code Playgroud)
您可以通过检查平台相关条件来调用它。对 android 和 ios 执行不同的点击操作。
import 'dart:io' show Platform;
import 'package:flutter/services.dart';
RaisedButton(
onPressed: () {
if (Platform.isAndroid) {
SystemNavigator.pop();
} else if (Platform.isIOS) {
exit(0);
}
},
child: Text("close app"),
)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我看到的唯一解决方案是:
对于安卓:
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
对于ios:
不可能关闭应用程序,但您可以使用https://pub.dev/packages/minimize_app将其移动到后台,例如:
MinimizeApp.minimizeApp();
请享用!
SystemNavigator.pop():无效
exit(0):有效,但是Apple可能会暂停您的APP,因为违反Apple Human Interface指南以编程方式退出该应用。
SystemNavigator.pop():有效,并且是退出应用程序的推荐方式。
exit(0):也可以使用,但不建议这样做,因为它会立即终止Dart VM进程,并且用户可能会认为该应用程序刚刚崩溃。
| 归档时间: |
|
| 查看次数: |
21397 次 |
| 最近记录: |