【问题标题】:How to keep asking location permission as long as the status is not granted-Flutter只要状态没有被授予,如何一直询问位置权限-Flutter
【发布时间】:2022-01-19 05:13:42
【问题描述】:

我试图在我的项目应用程序中询问用户的位置权限,因此在用户拒绝该权限后,我想在用户尝试访问菜单时继续显示询问位置权限的弹出窗口。我通过使用此功能成功地做到了这一点

trial() async {
    await Permission.locationAlways.request();
    print(Permission.locationAlways.status.then((value) {
      print("value:$value");
    }));
  }

但问题是......在状态为PermissionStatus.permanentlyDenied 后,询问位置权限的弹出窗口停止显示。有没有办法在未授予权限的情况下始终请求权限或打开位置权限设置的方法?

【问题讨论】:

    标签: flutter permissions location permission-denied


    【解决方案1】:

    请参考以下代码

    class MyApp extends StatelessWidget {
      MyApp({Key key}) : super(key: key);
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primaryColor: Color(0xfff00B074),
            textTheme: const TextTheme(
              bodyText1: TextStyle(
                  fontSize: 18.0,
                  fontFamily: 'Barlow-Medium',
                  color: Color(0xff464255)),
            ),
          ),
          home: PermissionHandlerScreen(),
        );
      }
    }
    
    class PermissionHandlerScreen extends StatefulWidget {
      @override
      _PermissionHandlerScreenState createState() =>
          _PermissionHandlerScreenState();
    }
    
    class _PermissionHandlerScreenState extends State<PermissionHandlerScreen> {
      @override
      void initState() {
        super.initState();
        permissionServiceCall();
      }
    
      permissionServiceCall() async {
        await permissionServices().then(
          (value) {
            if (value != null) {
              if (value[Permission.location].isGranted ) {
                /* ========= New Screen Added  ============= */
    
                Navigator.pushReplacement(
                  context,
                  MaterialPageRoute(builder: (context) => SplashScreen()),
                );
              }
            }
          },
        );
      }
    
      /*Permission services*/
      Future<Map<Permission, PermissionStatus>> permissionServices() async {
        // You can request multiple permissions at once.
        Map<Permission, PermissionStatus> statuses = await [
          Permission.location,
         
          //add more permission to request here.
        ].request();
    
        if (statuses[Permission.location].isPermanentlyDenied) {
          await openAppSettings().then(
            (value) async {
              if (value) {
                if (await Permission.location.status.isPermanentlyDenied == true &&
                    await Permission.location.status.isGranted == false) {
                  // openAppSettings();
                  permissionServiceCall(); /* opens app settings until permission is granted */
                }
              }
            },
          );
        } else {
          if (statuses[Permission.location].isDenied) {
            permissionServiceCall();
          }
        }
        
        /*{Permission.camera: PermissionStatus.granted, Permission.storage: PermissionStatus.granted}*/
        return statuses;
      }
    
      @override
      Widget build(BuildContext context) {
        permissionServiceCall();
        return WillPopScope(
          onWillPop: () {
            SystemNavigator.pop();
          },
          child: Scaffold(
            body: Container(
              child: Center(
                child: InkWell(
                    onTap: () {
                      permissionServiceCall();
                    },
                    child: Text("Click here to enable Enable Permission Screen")),
              ),
            ),
          ),
        );
      }
    }
    
    class SplashScreen extends StatelessWidget {
      const SplashScreen({Key key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return WillPopScope(
          onWillPop: () {
            SystemNavigator.pop();
          },
          child: Scaffold(
            body: Center(
              child: Text(
                "Splash Screen",
              ),
            ),
          ),
        );
      }
    }
    
    

    【讨论】:

      【解决方案2】:

      您可以使用permission_handler 8.3.0 包。

      并用作:

      if (await Permission.locationAlways.request().isGranted) {
      print(Permission.locationAlways.status.then((value) {
        print("value:$value");
      }));
      }
      

      更多详情:permission_handler

      实施于:implementation on

      【讨论】:

      • 嗨,对不起我的错误..在我说我使用位置包之前的问题中,在我重新检查它是权限包之后。我已经尝试了您的代码,但仍然得到相同的结果。未显示状态 value:PermissionStatus.permanentlyDenied 和请求权限的弹出窗口
      • 尝试在...上使用实现
      • 我看到了,我明白了。非常感谢您的帮助:D 这对我有帮助
      猜你喜欢
      • 2019-05-21
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 2022-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      相关资源
      最近更新 更多