Flutter 如何以编程方式退出应用程序
Flutter how to programmatically exit the app
如何以编程方式关闭 Flutter 应用程序。
我尝试弹出唯一的屏幕,但结果是黑屏。
您可以使用 SystemNavigator.pop()
来做到这一点。
下面在 Android
和 iOS
中与我完美配合,我使用了 dart:io
中的 exit(0)
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),
),
);
}
2019 年 1 月更新
优选的解决方案是:
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
如描述here
答案已经提供,但请不要在不知道自己在做什么的情况下将答案复制粘贴到您的代码库中:
如果您使用 SystemChannels.platform.invokeMethod('SystemNavigator.pop');
请注意 doc 明确提到:
Instructs the system navigator to remove this activity from the stack
and return to the previous activity.
On iOS, calls to this method are ignored because Apple's human
interface guidelines state that applications should not exit
themselves.
您可以使用 exit(0)
。这将使用给定的退出代码立即终止 Dart VM 进程。但请记住 doc 说:
This does not wait for any asynchronous operations to terminate. Using
exit is therefore very likely to lose data.
反正doc也注意到了SystemChannels.platform.invokeMethod('SystemNavigator.pop');
:
This method should be preferred over calling dart:io's exit method, as
the latter may cause the underlying platform to act as if the
application had crashed.
所以,记住你在做什么。
对于iOS
SystemNavigator.pop()
:不工作
exit(0)
:有效,但 Apple 可能 暂停您的应用程序
请看:
https://developer.apple.com/library/archive/qa/qa1561/_index.html
对于Android
SystemNavigator.pop()
:有效并且是推荐退出应用程序的方式。
exit(0)
:也有效,但不推荐,因为它会立即终止 Dart VM 进程,用户可能会认为该应用程序刚刚崩溃。
请看:
https://api.flutter.dev/flutter/services/SystemNavigator/pop.html
这对我有用;
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),
),
);
}
我更喜欢使用
Future.delayed(const Duration(milliseconds: 1000), () {
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
});
虽然 exit(0) 也可以,但应用程序突然关闭并且看起来不太好,应用程序似乎已经崩溃了。
Future.delayed(const Duration(milliseconds: 1000), () {
exit(0);
});
您可以通过检查平台相关条件来调用它。针对 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")
)
到目前为止,我看到的唯一在两家商店都接受的解决方案是:
对于 android:
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
对于 ios:
无法关闭该应用程序,但您可以使用 https://pub.dev/packages/minimize_app 将其移至后台,例如:
MinimizeApp.minimizeApp();
尽情享受吧!
我正在调用 _getOutOfApp 函数,这不是包;
void _getOutOfApp {
if (Platform.isIOS) {
try {
exit(0);
} catch (e) {
SystemNavigator.pop(); // for IOS, not true this, you can make comment this :)
}
} else {
try {
SystemNavigator.pop(); // sometimes it cant exit app
} catch (e) {
exit(0); // so i am giving crash to app ... sad :(
}
}
}
这对我有用。创建一个函数,然后在你的 onPressed
函数中调用这个函数。由于 Apple 不推荐 iOS
应用程序使用 exit(0)
,因此我在这里选择了这个库:https://pub.dev/packages/minimize_app。
下面是代码片段:
void closeApp(){
if(Platform.isAndroid){
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
}else{
MinimizeApp.minimizeApp();
}
}
您可以对 Android 和 [=27= 使用 SystemNavigator.pop();
] 使用 exit(0);
这里是例子-
if (Platform.isIOS) {
exit(0);
} else {
SystemNavigator.pop();
}
不要忘记在顶部导入 import 'dart:io';
如何以编程方式关闭 Flutter 应用程序。 我尝试弹出唯一的屏幕,但结果是黑屏。
您可以使用 SystemNavigator.pop()
来做到这一点。
下面在 Android
和 iOS
中与我完美配合,我使用了 dart:io
exit(0)
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),
),
);
}
2019 年 1 月更新 优选的解决方案是:
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
如描述here
答案已经提供,但请不要在不知道自己在做什么的情况下将答案复制粘贴到您的代码库中:
如果您使用 SystemChannels.platform.invokeMethod('SystemNavigator.pop');
请注意 doc 明确提到:
Instructs the system navigator to remove this activity from the stack and return to the previous activity.
On iOS, calls to this method are ignored because Apple's human interface guidelines state that applications should not exit themselves.
您可以使用 exit(0)
。这将使用给定的退出代码立即终止 Dart VM 进程。但请记住 doc 说:
This does not wait for any asynchronous operations to terminate. Using exit is therefore very likely to lose data.
反正doc也注意到了SystemChannels.platform.invokeMethod('SystemNavigator.pop');
:
This method should be preferred over calling dart:io's exit method, as the latter may cause the underlying platform to act as if the application had crashed.
所以,记住你在做什么。
对于iOS
SystemNavigator.pop()
:不工作
exit(0)
:有效,但 Apple 可能 暂停您的应用程序
请看:
https://developer.apple.com/library/archive/qa/qa1561/_index.html
对于Android
SystemNavigator.pop()
:有效并且是推荐退出应用程序的方式。
exit(0)
:也有效,但不推荐,因为它会立即终止 Dart VM 进程,用户可能会认为该应用程序刚刚崩溃。
请看:
https://api.flutter.dev/flutter/services/SystemNavigator/pop.html
这对我有用;
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),
),
);
}
我更喜欢使用
Future.delayed(const Duration(milliseconds: 1000), () {
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
});
虽然 exit(0) 也可以,但应用程序突然关闭并且看起来不太好,应用程序似乎已经崩溃了。
Future.delayed(const Duration(milliseconds: 1000), () {
exit(0);
});
您可以通过检查平台相关条件来调用它。针对 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")
)
到目前为止,我看到的唯一在两家商店都接受的解决方案是:
对于 android:
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
对于 ios:
无法关闭该应用程序,但您可以使用 https://pub.dev/packages/minimize_app 将其移至后台,例如:
MinimizeApp.minimizeApp();
尽情享受吧!
我正在调用 _getOutOfApp 函数,这不是包;
void _getOutOfApp {
if (Platform.isIOS) {
try {
exit(0);
} catch (e) {
SystemNavigator.pop(); // for IOS, not true this, you can make comment this :)
}
} else {
try {
SystemNavigator.pop(); // sometimes it cant exit app
} catch (e) {
exit(0); // so i am giving crash to app ... sad :(
}
}
}
这对我有用。创建一个函数,然后在你的 onPressed
函数中调用这个函数。由于 Apple 不推荐 iOS
应用程序使用 exit(0)
,因此我在这里选择了这个库:https://pub.dev/packages/minimize_app。
下面是代码片段:
void closeApp(){
if(Platform.isAndroid){
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
}else{
MinimizeApp.minimizeApp();
}
}
您可以对 Android 和 [=27= 使用 SystemNavigator.pop();
] 使用 exit(0);
这里是例子-
if (Platform.isIOS) {
exit(0);
} else {
SystemNavigator.pop();
}
不要忘记在顶部导入 import 'dart:io';