Enforce Light or Dark theme in Flutter WebView - flutter

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

Related

I'm getting this error when I add theme data in flutter

import 'package:flutter/material.dart';
import 'package:my_application_1/pages/home_page.dart';
void main() {
runApp(my_application_1());
}
class my_application_1 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: home_page()
theme: ThemeData(
primarySwatch: Colors.brown
),
);
}
}
ERROR SHOWING IN THEME - Expected to find ','.dart(expected_token)
You forgot a ',' after home: homePage().
Just add it.
import 'package:flutter/material.dart';
import 'package:my_application_1/pages/home_page.dart';
void main() {
runApp(my_application_1());
}
class my_application_1 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: home_page(),
theme: ThemeData(
primarySwatch: Colors.brown
),
);
}
}

How to display HTML text in Flutter in desktop

I want to display a paragraph in HTML that has various bold text and links within the tag using flutter on a desktop platform. I've come across "flutter_html", but the only supported platforms are for iOS and Android. Is there any other library that could display HTML text in a desktop environment? If there are none, are there any other ways to make this possible?
You can use flutter_html in desktop app.
Try my code below:
import 'package:html/dom.dart' as dom;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:html/parser.dart' show parse;
import 'package:flutter_html/flutter_html.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.yellow,
),
home: const HomePage(title: 'This Is Desktop'),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
HomePageState createState() => HomePageState();
}
class HomePageState extends State<HomePage> {
#override
void initState() {
parseHtml();
super.initState();
}
Future<dom.Document> parseHtml() async {
return parse(await rootBundle.loadString('static/index.html'));
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: FutureBuilder<dom.Document>(
future: parseHtml(),
builder:
(BuildContext context, AsyncSnapshot<dom.Document> snapshot) {
if (snapshot.hasData) {
return Html(data: snapshot.data?.outerHtml);
} else {
return const Center(
child: Text("Loading"),
);
}
}),
);
}
}
However, some tag has no effect. For example img tag not showing image, a tag only have link style but won't open a browser to a link when you click on it.
And some of css styles are not support, like flex.

Flutter ProviderScope application open error

Flutter when use riverpod in release mode application crash and when remove provider scope it is working. Widgets do not appear when the application is opened.
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:grock/grock.dart';
import 'package:turesta/constant/constant.dart';
import 'package:turesta/view/normal/splash/splash.dart';
void main() {
runApp(const ProviderScope(child: MyApp()));
}
class MyApp extends ConsumerWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context, WidgetRef ref) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Turesta',
navigatorKey: Grock.navigationKey,
scaffoldMessengerKey: Grock.snackbarMessengerKey,
theme: ThemeData(
scaffoldBackgroundColor: Constant.scaffoldBgColor,
),
home: Splash(),
);
}
}

How to download files in Flutter WebView?

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

Flutter Dark mode statusbar color

How do you change the status bar text color to black when its in dark mode? I can't find an answer for dark mode only in flutter.
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fry',
theme: ThemeData(),
darkTheme: ThemeData(
),
home: Loader(),
routes: {
MainMenu.id : (context) => MainMenu(),
},
debugShowCheckedModeBanner: false,
);
}
}
for iOS open Info.plist, it is under ios/Runner and add that;
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
and import services library to your main.dart file;
import 'package:flutter/services.dart';
and use this to change the status bar color;
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(statusBarBrightness: Brightness.light)
);