ios 设置权限和开关

inkz8wg9  于 2023-02-26  发布在  iOS
关注(0)|答案(1)|浏览(216)

bounty将在3天后过期。回答此问题可获得+50的声望奖励。Laurent Thomas正在寻找规范答案

我试图创建一个设置页面。在它,我想有几个开关。用户将使用它们来管理权限,相机,微型和更多。这里是我写的代码,但显然是错误的,因为它是不能正常工作。当我试图获得IOS权限,我只得到假,即使在IOS设置,权限打开了。2我也编辑了info.plist文件。3请你给点建议好吗?4谢谢。

import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:settings_ui/settings_ui.dart';

class PermissionsSettingsPage extends StatefulWidget {
  const PermissionsSettingsPage({super.key});

  @override
 State<PermissionsSettingsPage> createState() => _PermissionsSettingsPageState();
}

class _PermissionsSettingsPageState extends State<PermissionsSettingsPage> {
  var _cameraPermissionGranted ;
  var _microphonePermissionGranted ;
  var _contactPermissionGranted;
  var _notificationsPermissionGranted;

  @override
  void initState() {
    super.initState();
    _checkPermissions();
  }

  void _checkPermissions() async {
    final cameraStatus = await Permission.camera.status.isGranted;
    final microphoneStatus = await Permission.microphone.status.isGranted;
    final contactStatus = await Permission.contacts.status.isGranted;
    final notificationStatus = await Permission.notification.status.isGranted;
    setState(() {

      if (cameraStatus==true) {_cameraPermissionGranted = true;}
      if (microphoneStatus==true) {_microphonePermissionGranted = true;}
      if (contactStatus==true) {_contactPermissionGranted = true;}
      if (notificationStatus==true) {_notificationsPermissionGranted = true;}

      // _cameraPermissionGranted = cameraStatus.isGranted;

     // _microphonePermissionGranted = microphoneStatus.isGranted;
    });
  }

  void _requestPermission(Permission permission) async {
    final status = await permission.request();
    if (status.isGranted) {
      setState(() {
        if (permission == Permission.camera) {
          _cameraPermissionGranted = true;
        } else if (permission == Permission.microphone) {
          _microphonePermissionGranted = true;
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Permissions'),
      ),
      body: SettingsList(
        sections: [
          SettingsSection(
            title: const Text('Permissions'),
            tiles: [
              SettingsTile.switchTile(
                title: const Text('Camera'),
                leading: const Icon(Icons.camera_alt),
                initialValue: _cameraPermissionGranted,
                onToggle: (value) {
                  /*if (_cameraPermissionGranted) {
                    _requestPermission(Permission.camera);
                  } else {
                    openAppSettings();
                  }*/
                },
              ),
              SettingsTile.switchTile(
                title: const Text('Microphone'),
                leading: const Icon(Icons.mic),
                initialValue: _microphonePermissionGranted,
                onToggle: (value) {
                  /*if (_microphonePermissionGranted) {
                    _requestPermission(Permission.microphone);
                  } else {
                    openAppSettings();
                  }*/
                },
              ),

              SettingsTile.switchTile(
                title: const Text('Contacts'),
                leading: const Icon(Icons.contacts),
                initialValue: _contactPermissionGranted,
                onToggle: (value) {
                  /*if (_microphonePermissionGranted) {
                    _requestPermission(Permission.microphone);
                  } else {
                    openAppSettings();
                  }*/
                },
              ),

              SettingsTile.switchTile(
                title: const Text('Notification'),
                leading: const Icon(Icons.notifications),
                initialValue: _notificationsPermissionGranted,
                onToggle: (value) {
                  /*if (_microphonePermissionGranted) {
                    _requestPermission(Permission.microphone);
                  } else {
                    openAppSettings();
                  }*/
                },
              ),

            ],
          ),
          SettingsSection(
            title: const Text('Settings'),
            tiles: [
              SettingsTile(
                title: const Text('App Settings'),
                leading: const Icon(Icons.settings),
                onPressed: (_) {
                  openAppSettings();
                },
              ),
            ],
          ),
        ],
      ),
    );
  }
}
pbpqsu0x

pbpqsu0x1#

看起来你从来没有_requestPermission()过。这些调用在每个瓷砖中都被注解掉了,但即使在那里,它似乎也是不正确的。
您需要:

if (_microphonePermissionGranted) {
  _requestPermission(Permission.microphone);

但这似乎并不正确。如果granted,那么就不需要请求权限。相反,您应该先请求权限,然后才能获得granted
您的代码仅支持两种情况:"准予"和"否认"。但我认为你需要支持第三个"未知"。
检查文档中的所有可能值:PermissionStatus class.
当你第一次打开设置页面时,如果你从未请求过访问权限,那么它是"未确定"的。如果未确定,你会显示一个按钮来请求它。请求后,状态应该变为"已授予"或"已拒绝"(或其他状态)。如果已授予,我们就可以了。如果已拒绝,你希望这些按钮将用户导航到应用程序设置。

class _PermissionsSettingsPageState extends State<PermissionsSettingsPage> {
  var _cameraStatus;

  @override
  void initState() {
    super.initState();
    _checkPermissions();
  }

  void _checkPermissions() async {
    setState(() {
      _cameraStatus = Permission.camera.status;
      // I don't think await is needed here since
      //   checking the status should not be async.
      //
      // You'd also want to store the full status
      //   because isGranted only has two states and
      //   it does not support "undetermined".
    });
  }

  PermissionStatus _togglePermission(Permission permission) async {
    final status = permission.status;
    switch (status) {
      case PermissionStatus.unknown:
        return await permission.request();
      case PermissionStatus.granted:
        // no work needed
        break;

      case PermissionStatus.denied:
        openAppSettings();
        break

      case PermissionStatus.disabled:
        // should be unreachable
        break;

      case PermissionStatus.restricted:
        // disabled by iOS policies like MDM,
        //   parental control, privacy settings, etc
        //
        // You might want to show a special message
        //   in that case.
        break;
    }
    return status;
  }

              SettingsTile.switchTile(
                title: const Text('Camera'),
                leading: const Icon(Icons.camera_alt),
                initialValue: _cameraStatus.isGranted,
                onToggle: (value) {
                  setState({
                    _cameraStatus = _togglePermission(Permission.camera);
                  })
                },

最后我觉得这里不适合使用"开关"控件。是的,你可以通过请求权限来打开它,但实际上你不能把它切换回"关闭"状态。所以这只是单向交互,在这里一个请求权限的按钮应该更好。

相关问题