Flutter:如何实时收听权限 [英] Flutter: How do I listen to permissions real time
本文介绍了Flutter:如何实时收听权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个应用程序,在该应用程序中,我想不断听取位置和电池权限。
I am working on an app in which I want to continuously listen to location and battery permissions.
示例场景:
- 用户打开应用程序
- 授予权限访问权限
- 进入设置并撤消权限
- 再次打开应用程序
- 应用程序显示小吃栏,通知用户权限已被撤消。
- The user opens the app
- Grants permission access
- Goes to settings and revokes the permissions
- Opens the app again
- The app displays a snackbar that informs the user that permission has been revoked.
我是初学者,正在使用 flutter-permissions-handler ,下面的代码显示了我的用法。
I am a beginner and I am using the flutter-permissions-handler and the piece of code below shows my usage.
_listenForLocationPermission() {
Future<PermissionStatus> status = PermissionHandler()
.checkPermissionStatus(PermissionGroup.locationWhenInUse);
status.then((PermissionStatus status) {
setState(() {
_permissionStatus = status;
if (_permissionStatus != PermissionStatus.granted) {
_renderOfflineSnackbar('Offline');
}
});
});
}
任何有关上述建议的建议。
Any advice on the above is appreciated.
推荐答案
我在同一条船上,并且发现这行得通
I'm in the same boat and have found that this works
您需要扩展WidgetsBindingObserver类
You need to extend your class with WidgetsBindingObserver
class _AppState extends State<App> with WidgetsBindingObserver {
PermissionStatus _status;
...
...
然后将这些方法添加到您的班级
then add these methods to your class
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
// check permissions when app is resumed
// this is when permissions are changed in app settings outside of app
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
PermissionHandler()
.checkPermissionStatus(PermissionGroup.locationWhenInUse)
.then(_updateStatus);
}
}
下面是我的完整代码,但我没有包括构建小部件,以使其简短
My full code is below, but I've not included the build widget to keep it brief
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
void main() => runApp(App());
class App extends StatefulWidget {
@override
_AppState createState() => _AppState();
}
class _AppState extends State<App> with WidgetsBindingObserver {
PermissionStatus _status;
// check permissions
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
PermissionHandler() // Check location permission has been granted
.checkPermissionStatus(PermissionGroup
.locationWhenInUse) //check permission returns a Future
.then(_updateStatus); // handling in callback to prevent blocking UI
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
// check permissions when app is resumed
// this is when permissions are changed in app settings outside of app
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
PermissionHandler()
.checkPermissionStatus(PermissionGroup.locationWhenInUse)
.then(_updateStatus);
}
}
override
Widget build(BuildContext context) {
return MaterialApp(
...
...
}
void _updateStatus(PermissionStatus status) {
if (status != _status) {
// check status has changed
setState(() {
_status = status; // update
});
} else {
if (status != PermissionStatus.granted) {
PermissionHandler().requestPermissions(
[PermissionGroup.locationWhenInUse]).then(_onStatusRequested);
}
}
}
}
void _askPermission() {
PermissionHandler().requestPermissions(
[PermissionGroup.locationWhenInUse]).then(_onStatusRequested);
}
void _onStatusRequested(Map<PermissionGroup, PermissionStatus> statuses) {
final status = statuses[PermissionGroup.locationWhenInUse];
if (status != PermissionStatus.granted) {
// On iOS if "deny" is pressed, open App Settings
PermissionHandler().openAppSettings();
} else {
_updateStatus(status);
}
}
我希望这会有所帮助
这篇关于Flutter:如何实时收听权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文