文件上传在Webview、Flutter中不起作用

ifmq2ha2  于 2023-05-29  发布在  Flutter
关注(0)|答案(3)|浏览(353)

我有一个网站,我转换到Flutter Android应用程序的网站使用webview_flutter插件,一切都工作正常.
但是有一个问题,网站上有一个表格,表格中有一个文件输入。在网站上一切正常,但当我点击上传文件从Android应用程序,我使用webview_flutter插件创建,文件输入不工作。
当我点击上传文件,它不会打开任何弹出窗口或任何东西,让我选择文件从我的手机,并上传到表格。
这是我的主要.dart代码:

import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:custom_splash/custom_splash.dart';
import 'package:connectivity/connectivity.dart';
import 'package:selfcare/nointernet.dart';

void main() {

  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());

}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: "Self Care",
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primarySwatch: Colors.red,
        ),
        home: Scaffold(body: splash()));
  }
}

class splash extends StatefulWidget {
  @override
  _splashState createState() => _splashState();
}

class _splashState extends State<splash> {
  String result = '';
  var Colorsval = Colors.white;

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

  @override
  Widget build(BuildContext context) {
    if (result != null && result == "Connected") {
      return CustomSplash(
        //backGroundColor: Color(0xFFFF9800),
        imagePath: "assets/images/logo.png",

        home: WebViewClass(),
        duration: 10,
        animationEffect: "zoom-in",
      );
    } else if (result != null && result == "NoInternet") {
      return CustomSplash(
        //backGroundColor: Color(0xFFFF9800),
        imagePath: "assets/images/logo.png",

        home: NoInternetPage(),
        duration: 10,
        animationEffect: "zoom-in",
      );
    } else if (result == null) {
      return CustomSplash(
        //backGroundColor: Color(0xFFFF9800),
        imagePath: "assets/images/logo.png",

        home: NoInternetPage(),
        duration: 10,
        animationEffect: "zoom-in",
      );
    } else {
      return CustomSplash(
        //backGroundColor: Color(0xFFFF9800),
        imagePath: "assets/images/logo.png",

        home: NoInternetPage(),
        duration: 10,
        animationEffect: "zoom-in",
      );
    }
  }

  void CheckStatus() {
    Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
      if (result == ConnectivityResult.mobile ||
          result == ConnectivityResult.wifi) {
        ChangeValues("Connected", Colors.green[900]);
      } else {
        ChangeValues("NoInternet", Colors.red[900]);
      }
    });
  }

  void ChangeValues(String resultval, Color colorval) {
    setState(() {
      result = resultval;
      Colorsval = colorval;
    });
  }
}

class WebViewClass extends StatefulWidget {
  WebViewState createState() => WebViewState();
}

class WebViewState extends State<WebViewClass> {
  num position = 1;

  final key = UniqueKey();

  doneLoading(String A) {
    setState(() {
      position = 0;
    });
  }

  startLoading(String A) {
    setState(() {
      position = 1;
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Permission.mediaLibrary.request();
    Permission.phone.request();
    Permission.photos.request();
    Permission.storage.request();
    Permission.camera.request();
  }
  //Check Internet Code Starts

  //Check Internet Code Ended here
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //appBar: AppBar(title: Text('Show ProgressBar While Loading Webview')),
        appBar: PreferredSize(
          child: Container(),
          preferredSize: Size.fromHeight(0.0),
        ),
        body: IndexedStack(index: position, children: <Widget>[
          WebView(
            initialUrl: 'http://mywebsite.com',
            javascriptMode: JavascriptMode.unrestricted,
            key: key,
            onPageFinished: doneLoading,
            onPageStarted: startLoading,
            //onWebResourceError: ,
          ),
          Container(
            color: Colors.white,
            child: Center(
                child: CircularProgressIndicator(
              valueColor: new AlwaysStoppedAnimation<Color>(Colors.red),
            )),
          ),
        ]));
  }
}

这是我使用的flutter webview插件:

dependencies:
  webview_flutter: ^1.0.7

我也使用了一些权限来摆脱这个问题,但没有解决它,权限:

Permission.mediaLibrary.request();
Permission.phone.request();
Permission.photos.request();
Permission.storage.request();
Permission.camera.request();
uttx8gqw

uttx8gqw1#

由于webview_flutter 4.0.2你可以很容易地做到这一点,因为支持Android was just added .
为了实现这一点,你必须首先检查它运行的平台是否是Android,然后设置你的自定义监听器:

if (Platform.isAndroid) { // or: if (webViewController.platform is AndroidWebViewController)
    final myAndroidController = webViewController.platform as AndroidWebViewController;

    myAndroidController.setOnShowFileSelector( (params) async {
        // Control and show your picker
        // and return a list of Uris.

        return []; // Uris
    });
}
qvtsj1bj

qvtsj1bj2#

webview_flutter插件还没有支持文件上传。您可以跟踪与此问题相关的当前打开的票证here。同时,您可以使用flutter_inappwebviewflutter_webview_plugin作为解决方法。

k5ifujac

k5ifujac3#

我使用webview_flutter_pro插件成功地实现了这个功能。我已经在这里发布了有关如何让它工作的详细信息:
如何在webview_flutter中打开图库或相机android中的文件拾取器?

相关问题