How to download files in Flutter WebView?
Asked Answered
R

1

6

I am working on Flutter webview apps using Flutter Webview.

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child : const WebView(
            initialUrl: 'https://google.com',
            javascriptMode: JavascriptMode.unrestricted,
          ),
        )
      )
    );
  }
}

I try to use launchURL plugin but that will open predefined url in external browser window.

if (url.contains('.pdf')) {
    launchURL(url);
  }

What I want is to download the file in-app webview.

Riparian answered 27/1, 2021 at 12:35 Comment(2)
I done this but i used flutter_inapp_webview pluginVeinlet
this answer: #57938164Adina
K
-1

There is actually an existing package that you could use. Check the flutter_downloader package:

A plugin for creating and managing download tasks. Supports iOS and Android.

This plugin is using WorkManager on Android and NSURLSessionDownloadTask on iOS to run download tasks in background.

Here is an example that you can try:

import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:flutter_downloader_example/home_page.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await FlutterDownloader.initialize(debug: true, ignoreSsl: true);

  runApp(const MyApp());
}

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

  static const _title = 'flutter_downloader demo';

  @override
  Widget build(BuildContext context) {
    final platform = Theme.of(context).platform;

    return MaterialApp(
      title: _title,
      theme: ThemeData.light(),
      darkTheme: ThemeData.dark(),
      home: MyHomePage(
        title: _title,
        platform: platform,
      ),
    );
  }
}
Koran answered 12/9, 2022 at 20:57 Comment(1)
The question is about how to download using webview. The answer provided here does not take into account the context of webview when downloading. Does not answer the questionPreachy

© 2022 - 2024 — McMap. All rights reserved.