ipr*_*ekk 3 permissions android ios dart flutter
我正在开发一个应用程序,我想在其中持续侦听位置和电池权限。
示例场景:
我是初学者,我正在使用flutter-permissions-handler,下面的一段代码显示了我的用法。
_listenForLocationPermission() {
Future<PermissionStatus> status = PermissionHandler()
.checkPermissionStatus(PermissionGroup.locationWhenInUse);
status.then((PermissionStatus status) {
setState(() {
_permissionStatus = status;
if (_permissionStatus != PermissionStatus.granted) {
_renderOfflineSnackbar('Offline');
}
});
});
}
Run Code Online (Sandbox Code Playgroud)
对上述任何建议表示赞赏。
And*_*son 16
我在同一条船上,发现这有效
您需要使用 WidgetsBindingObserver 扩展您的类
class _AppState extends State<App> with WidgetsBindingObserver {
PermissionStatus _status;
...
...
Run Code Online (Sandbox Code Playgroud)
然后将这些方法添加到您的类中
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
我的完整代码如下,但我没有包含构建小部件以保持简短
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助
| 归档时间: |
|
| 查看次数: |
4430 次 |
| 最近记录: |