Flutter 在按下按钮或 onpressed 事件后授予相机和麦克风权限

Posted

技术标签:

【中文标题】Flutter 在按下按钮或 onpressed 事件后授予相机和麦克风权限【英文标题】:Flutter give camera and microphone permissions after pressing button or on onpressed event 【发布时间】:2020-12-08 00:41:36 【问题描述】:

我需要在单击按钮时授予相机和麦克风权限。我希望我的权限一个接一个地出现,但在这种情况下,只出现麦克风权限。要授予相机权限,我必须再次按下按钮。

我怎样才能做到这一点?

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

            class IframeScreen extends StatefulWidget 
            @override
           _IframeScreenState createState() => _IframeScreenState();
             

             class _IframeScreenState extends State<IframeScreen> 
              InAppWebViewController _webViewController;

              Future webViewMethod() async 
              print('In Microphone permission method');
              WidgetsFlutterBinding.ensureInitialized();
            
              Permission.microphone.request();
              WebViewMethodForCamera();

             
              Future WebViewMethodForCamera() async
               print('In Camera permission method');
               WidgetsFlutterBinding.ensureInitialized();
               Permission.camera.request();
              



            @override
            Widget build(BuildContext context) 
            return Scaffold(
            appBar: AppBar(
            title: Text('Check this frame'),
           ),
             body: Column(
      children: <Widget>[
        RaisedButton(
          onPressed: webViewMethod,
          child: Text('Join'),
          textColor: Colors.black,
        ),

        Expanded(
          child: Container(
            child: InAppWebView(
                initialUrl: "https://appr.tc/r/158489234",
                initialOptions: InAppWebViewGroupOptions(
                  crossPlatform: InAppWebViewOptions(
                    mediaPlaybackRequiresUserGesture: false,
                    debuggingEnabled: true,
                  ),
                ),
                onWebViewCreated: (InAppWebViewController controller) 
                  _webViewController = controller;
                ,
                androidOnPermissionRequest: (
                    InAppWebViewController controller, String origin,
                    List<String> resources) async 
                  return PermissionRequestResponse(resources: resources,
                      action: PermissionRequestResponseAction.GRANT);
                
            ),
          ),
        )

      ],
    )
);

【问题讨论】:

【参考方案1】:

您可以在下面复制粘贴运行完整代码 你可以等待Permission.microphone.request();await Permission.camera.request(); 代码sn-p

Future webViewMethod() async 
    print('In Microphone permission method');
    //WidgetsFlutterBinding.ensureInitialized();

    await Permission.microphone.request();
    WebViewMethodForCamera();

  
  
Future WebViewMethodForCamera() async
    print('In Camera permission method');
    //WidgetsFlutterBinding.ensureInitialized();
    await Permission.camera.request();

工作演示

完整代码

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

class IframeScreen extends StatefulWidget 
  @override
  _IframeScreenState createState() => _IframeScreenState();


class _IframeScreenState extends State<IframeScreen> 
  InAppWebViewController _webViewController;

  Future webViewMethod() async 
    print('In Microphone permission method');
    //WidgetsFlutterBinding.ensureInitialized();

    await Permission.microphone.request();
    WebViewMethodForCamera();

  
  Future WebViewMethodForCamera() async
    print('In Camera permission method');
    //WidgetsFlutterBinding.ensureInitialized();
    await Permission.camera.request();
  



  @override
  Widget build(BuildContext context) 
    return Scaffold(
        appBar: AppBar(
          title: Text('Check this frame'),
        ),
        body: Column(
          children: <Widget>[
            RaisedButton(
              onPressed: webViewMethod,
              child: Text('Join'),
              textColor: Colors.black,
            ),

            Expanded(
              child: Container(
                child: InAppWebView(
                    initialUrl: "https://appr.tc/r/158489234",
                    initialOptions: InAppWebViewGroupOptions(
                      crossPlatform: InAppWebViewOptions(
                        mediaPlaybackRequiresUserGesture: false,
                        debuggingEnabled: true,
                      ),
                    ),
                    onWebViewCreated: (InAppWebViewController controller) 
                      _webViewController = controller;
                    ,
                    androidOnPermissionRequest: (
                        InAppWebViewController controller, String origin,
                        List<String> resources) async 
                      return PermissionRequestResponse(resources: resources,
                          action: PermissionRequestResponseAction.GRANT);
                    
                ),
              ),
            )

          ],
        )
    );
   

void main() 
  runApp(MyApp());


class MyApp extends StatelessWidget   
  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(       
        primarySwatch: Colors.blue,       
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: IframeScreen(),
    );
  


class MyHomePage extends StatefulWidget 
  MyHomePage(Key key, this.title) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();


class _MyHomePageState extends State<MyHomePage> 
  int _counter = 0;

  void _incrementCounter() 
    setState(()      
      _counter++;
    );
  

  @override
  Widget build(BuildContext context)     
    return Scaffold(
      appBar: AppBar(       
        title: Text(widget.title),
      ),
      body: Center(        
        child: Column(          
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  

【讨论】:

很高兴为您提供帮助。如果对您有帮助,请将其标记为答案。谢谢。 @chunhunghan.我正在使用 InAppWebView 进行文件上传,但我无法上传图像。我可以从设备中选择图像,但无法上传,因为它一直在加载。我需要在 Android 中提供一些额外的权限吗? @utkarsh,你能把你的重现代码发布到一个新问题上吗?谢谢。【参考方案2】:

尝试

await WebViewMethodForCamera();

【讨论】:

以上是关于Flutter 在按下按钮或 onpressed 事件后授予相机和麦克风权限的主要内容,如果未能解决你的问题,请参考以下文章

Android:如何在按下按钮时关闭一个活动并在按下另一个按钮时关闭整个后台堆栈? [复制]

更改按钮颜色和文本 onPress

如何在按下按钮时下载 Flutter Web 应用程序?

为啥我的 onPressed 回调不能使用 Bloc 调用我的 Bloc?

如何在 react-native 中的 onPress 按钮上滚动顶部?

如何在 react-native 的 onPress 按钮上滚动底部?