如何在WebView Flutter中下载文件

问题描述 投票:0回答:1

我尝试通过 webview_flutter 下载文件,但文件无法下载。 我已经获得了读写磁盘的权限,但是找不到文件下载。

我希望该文件能够下载到android。在 webview_flutter 文档中找不到有关下载文件的任何内容。我尝试过替代包,但他们的解决方案很混乱或已被弃用。

Flutter 中从 WebView 下载文件的标准方式是什么?

main.dart:

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

void main() {

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  WebViewController controller = WebViewController()
  ..setJavaScriptMode(JavaScriptMode.unrestricted)
  ..setBackgroundColor(const Color(0x00000000))
  ..setNavigationDelegate(
    NavigationDelegate(
      onProgress: (int progress) {
        // Update loading bar.
      },
      onPageStarted: (String url) {},
      onPageFinished: (String url) {},
      onWebResourceError: (WebResourceError error) {}
    ),
  )
  ..loadRequest(Uri.parse('https://file-examples.com/index.php/sample-documents-download/sample-pdf-download/'));

  @override
  Widget build(BuildContext context) {
    Permission.storage.request();
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
    appBar: AppBar(title: const Text('Flutter Simple Example')),
    body: WebViewWidget(controller: controller),
  );
  }
}
android flutter dart mobile webview
1个回答
0
投票

我正在使用 url_launcher

然后,我将其添加到 NavigationDelegate 中。 你可以检查你的URL是否有字符串下载、.pdf等

    onNavigationRequest: (NavigationRequest request){
      final urlString = request.url.toUpperCase();
      final uriUL = Uri.parse(request.url);
      if(urlString.contains('DOWNLOAD')){
        print(request.url);
        _launchUrl(uriUL);
        return NavigationDecision.prevent;
      }
      return NavigationDecision.navigate;
    },

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.