flutter 处理摄像机许可结果-抖动

k10s72fa  于 2022-12-19  发布在  Flutter
关注(0)|答案(2)|浏览(140)

我正在使用以下方法来检查是否授予了Camera权限:

//Global variable
int cameraPermission = -1;

Future<void> checkCameraPermission() async {
cameraPermissionStatus = await Permission.camera.status;
print(">>>>>>>>>>>>>>>>>>>>>>>>>>\ncamera permission >> " +
    cameraPermissionStatus.name);
if (cameraPermissionStatus.name == "granted") {
  cameraPermission = 1;
  scheduleTimeout(4 * 1000);
} else if (cameraPermissionStatus.name == "denied") {
  cameraPermission = 2;
} else {
  cameraPermission=0;
}
    setState(() {});
}

addPostFrameCallback内的initState()中调用上述方法
构建方法中的反射视图如下所示:

return Column(
            children: [
              cameraPermission==1
                  ? Expanded(flex: 2, child: buildCameraPreview())
                  : cameraPermission==2
                      ? Container(
                          child: InkWell(
                          onTap: () async {
                            await Permission.camera.request();
                          },
                          child: Padding(
                              padding: EdgeInsets.only(top: 200.h),
                              child: Text(
                                  "Tap here to grant Camera Permission")),
                        ))
                      : cameraPermission==0
                          ? Center(
                              child: InkWell(
                              onTap: () {
                                openAppSettings();
                              },
                              child: Text(
                                  "Tap here to grant Camera permission\nfrom application Settings"),
                            ))
                          : Container(),

问题是默认情况下显示“拒绝”视图(cameraPermission==2),这是正常的,但由于我在addPostFrameCallback内的init中调用了checkCameraPermission方法,
它应该反映,一旦我授予或允许相机权限从打开的权限对话框,但它不反映视图。返回前一个屏幕后,回来的工作。
有什么问题吗?先谢了。

5rgfhyps

5rgfhyps1#

要在Flutter中处理摄像机权限结果,可以使用flutter_permission_handler包。
以下是如何在Flutter中请求摄影机权限并处理结果的示例:

import 'package:flutter/material.dart';
import 'package:flutter_permission_handler/flutter_permission_handler.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: RaisedButton(
            onPressed: () async {
              // Request camera permission
              PermissionStatus permission = await PermissionHandler().requestPermission(PermissionGroup.camera);

              // Handle the permission result
              if (permission == PermissionStatus.granted) {
                print('Permission granted');
                // You can now use the camera
              } else {
                print('Permission denied');
                // Handle permission denied
              }
            },
            child: Text('Request Camera Permission'),
          ),
        ),
      ),
    );
  }
}

此代码将显示一个按钮,当按下该按钮时,将向用户请求摄像头权限。如果用户赠款权限,则将返回PermissionStatus.granted值,您可以使用摄像头。如果用户拒绝权限,则将返回PermissionStatus.denied值,您可以相应地处理权限拒绝。

e7arh2l6

e7arh2l62#

addPostFrameCallback会在渲染完该帧后立即被调用,但您的摄像头权限仍然会被拒绝。因此,您需要在checkCameraPermission方法中请求权限,或者在请求权限后调用它。

Container(
   child: InkWell(
      onTap: () async {
         await checkCameraPermission();
      },
      child: Padding(
         padding: EdgeInsets.only(top: 200.h),
         child: Text("Tap here to grant Camera Permission"),
      ),
   ),
),

并更改实现,例如:

Future<void> checkCameraPermission() async {
   cameraPermissionStatus = await Permission.camera.request();
   print(">>>>>>>>>>>>>>>>>>>>>>>>>>\ncamera permission >> " + cameraPermissionStatus.name);
   if (cameraPermissionStatus.name == "granted") {
      cameraPermission = 1;
      scheduleTimeout(4 * 1000);
   } else if (cameraPermissionStatus.name == "denied") {
      cameraPermission = 2;
   } else {
      cameraPermission=0;
   }
   setState(() {});
}

相关问题