1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter Webview, python

Discussão em 'Mobile' iniciado por Stack, Outubro 7, 2024 às 02:42.

  1. Stack

    Stack Membro Participativo

    I can’t run the project through flutter! It doesn’t take the location on Android, and the video/camera doesn’t display on iPhone! Help with this, other sites with camera and location support are all working

    import 'dart:io';
    import 'package:flutter/material.dart';
    import 'package:permission_handler/permission_handler.dart';
    import 'package:webview_flutter/webview_flutter.dart';
    import 'package:webview_flutter_android/webview_flutter_android.dart';
    import 'package:geolocator/geolocator.dart';

    class FaceIDPage extends StatefulWidget {
    const FaceIDPage({Key? key}) : super(key: key);

    @override
    _FaceIDPageState createState() => _FaceIDPageState();
    }

    class _FaceIDPageState extends State<FaceIDPage> {
    late final WebViewController _controller;

    @override
    void initState() {
    super.initState();
    _requestPermissions();
    _initializeWebView();
    }

    Future<void> _requestPermissions() async {
    if (Platform.isIOS) {
    await [
    Permission.camera,
    Permission.microphone,
    Permission.location,
    ].request();
    } else if (Platform.isAndroid) {
    await [
    Permission.camera,
    Permission.location,
    Permission.microphone,
    ].request();
    }
    }

    Future<Position> _getCurrentPosition() async {
    bool serviceEnabled;
    LocationPermission permission;

    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
    return Future.error('Location services are disabled.');
    }

    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
    if (permission == LocationPermission.denied) {
    return Future.error('Location permissions are denied');
    }
    }

    if (permission == LocationPermission.deniedForever) {
    return Future.error('Location permissions are permanently denied.');
    }

    return await Geolocator.getCurrentPosition();
    }

    void _setGeolocationInWebView(WebViewController controller) async {
    try {
    Position position = await _getCurrentPosition();

    await controller.runJavaScript('''
    navigator.geolocation.getCurrentPosition = function(success, error) {
    success({coords: {latitude: ${position.latitude}, longitude: ${position.longitude}}});
    };
    ''');
    } catch (e) {
    debugPrint('Ошибка при получении координат: $e');
    }
    }

    void _initializeWebView() {
    final PlatformWebViewControllerCreationParams params = PlatformWebViewControllerCreationParams();
    _controller = WebViewController.fromPlatformCreationParams(
    params,
    onPermissionRequest: (resources) async {
    return resources.grant();
    },
    )
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setBackgroundColor(Colors.transparent)
    ..setNavigationDelegate(
    NavigationDelegate(
    onPageFinished: (String url) {
    _setGeolocationInWebView(_controller);
    },
    ),
    )
    ..loadRequest(Uri.parse('https://pyth.***'));

    if (Platform.isAndroid) {
    final AndroidWebViewController androidController = _controller.platform as AndroidWebViewController;
    AndroidWebViewController.enableDebugging(true);
    androidController.setMediaPlaybackRequiresUserGesture(false);
    //androidController.setGeolocationEnabled(true);
    } else if (Platform.isIOS) {
    _controller.runJavaScript('''
    navigator.mediaDevices.getUserMedia({ video: true, audio: true })
    .then(stream => {
    const video = document.createElement('video');
    video.srcObject = stream;
    video.setAttribute('playsinline', true);
    video.play();
    document.body.appendChild(video);
    })
    .catch(error => {
    console.error('Ошибка при доступе к камере: ', error);
    });
    ''');
    }
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
    title: const Text('FaceID Верификация'),
    ),
    body: WebViewWidget(controller: _controller),
    );
    }
    }


    The server part runs on python, flask server, app.py includes index.html and then js css, etc.

    Continue reading...

Compartilhe esta Página