How to download files in Flutter WebView? - flutter

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.

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,
),
);
}
}

Related

Flutter app crash with appbunddle but works with apk

Everything's working fine with the apk but when deploy on the playstore I had to create an appbundle (.aab), I can download the app from the playstore but it crash when I try to open it
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: WebViewLoad()
)
);
}
}
class WebViewLoad extends StatefulWidget {
WebViewLoadUI createState() => WebViewLoadUI();
}
class WebViewLoadUI extends State<WebViewLoad>{
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
toolbarHeight: 0,
),
body: WebView(
initialUrl: 'https://www.google.fr',
javascriptMode: JavascriptMode.unrestricted,
)
);
}
}
I ran this command to create the appbuild
flutter build appbuild --release --no-sound-null-safety
Any idea of what it doesn't work?

my flutter app isnt running the homepage i want it to

I don't know what's happening but the app is running on my root page and not on the login page
import 'package:flutter/material.dart';
import 'package:loyality/Application/Pages/Login/login_page.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Libra',
home: LoginPage(),
);
}
}

Enforce Light or Dark theme in Flutter WebView

I'm using webview_flutter to display a site. The problem is, the site is opening in dark mode. I tried setting MaterialApp theme and darkTheme properties to ThemeData.light(), but the webview is not respecting it and taking Device's default theme. Switching device theme reflects in the webview.
How to enforce light/dark theme to the WebView widget explicitly?
Here a main.dart snippet
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
//please explain this one as well
// SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
// statusBarBrightness: Brightness.dark) // Or Brightness.dark
// );
return MaterialApp(
themeMode: ThemeMode.light,
theme: ThemeData.light(),
darkTheme: ThemeData.light(),
debugShowCheckedModeBanner: false,
title: 'Flutter site',
home: SafeArea(
child: WebViewExample(),
),
);
}
}
class WebViewExample extends StatefulWidget {
#override
WebViewExampleState createState() => WebViewExampleState();
}
class WebViewExampleState extends State<WebViewExample> {
#override
void initState() {
super.initState();
// Enable hybrid composition.
if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
// if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) { }
}
#override
Widget build(BuildContext context) {
return WebView(
javascriptMode: JavascriptMode.unrestricted,
initialUrl: 'https://flutter.dev.',
);
}
}

Load html from assets folder and add query parameters with webview_flutter package

I want to send query parameters to my HTML in the assets folder with the webview_flutter package.
I added the HTML file to my pubspec.yaml file.
This is my code.
//Dart
import 'dart:convert';
//flutter
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
//plugins
import 'package:webview_flutter/webview_flutter.dart';
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: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
#override
HomePageState createState() {
return HomePageState();
}
}
class HomePageState extends State<HomePage> {
WebViewController _controller;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Help')),
body: WebView(
initialUrl: 'about:blank',
onWebViewCreated: (WebViewController webViewController) {
_controller = webViewController;
_loadHtmlFromAssets();
},
),
);
}
_loadHtmlFromAssets() async {
String fileText = await rootBundle.loadString('assets/web/index.html');
_controller.loadUrl(Uri.dataFromString(fileText,
mimeType: 'text/html', encoding: Encoding.getByName('utf-8'))
.toString());
}
}
I tried to add parameters like this. but it can't load HTML.
_loadHtmlFromAssets() async {
String fileText = await rootBundle
.loadString('assets/web/index.html?url=https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4');
_controller.loadUrl(Uri.dataFromString(fileText,
mimeType: 'text/html', encoding: Encoding.getByName('utf-8'))
.toString());
}
I also thoughts about the JavaScript the javascriptChannels it can send data from java script to the dart file. but I have no idea how to send data from dart file to html file with the web view?

Error -32601 received from application: Method not found

I'm getting error in this code. Any help?
import 'package:flutter/material.dart';
import './homepage.dart';
void main() => (MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: "Chat",
home: HomePage(),
);
}
}
In HomePage.dart file
import 'package:flutter/material.dart';
class HomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Chat"
),
),
);
}
}
In Debug Console it shows:
Restarted application in 2,271ms.
Error -32601 received from application: Method not found
Reloaded 0 of 496 libraries in 237ms
The issue is with your code. Your syntax for the main method doesn't make sense for running a flutter app. You likely intended to call runApp on the widget within main.
Changed main:
import 'package:flutter/material.dart';
import './homepage.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: "Chat",
home: HomePage(),
);
}
}
`