首页 > 解决方案 > 如何在颤动的 webview 中禁用暗模式?

问题描述

我在这里有一个简单的代码,它只是应该使用 webview_flutter 显示一个网页(https://pub.dev/packages/webview_flutter

但是,它会自动启用暗模式并显示网站的暗版本。我怎样才能防止这种情况?

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text("Mobile App"),
            backgroundColor: Colors.blueGrey,
          ),
          body: WebView(
            initialUrl: 'https://flutter.dev',
            javascriptMode: JavascriptMode.unrestricted,
          )),
      debugShowCheckedModeBanner: false,
    );
  }
}

标签: flutterwebviewuiwebviewandroid-webviewdarkmode

解决方案


设置您的 MaterialApp 主题。

MaterialApp(
      theme: ThemeData.light(), //<- specify here light()
      home: Scaffold(
          appBar: AppBar(
            title: Text("Mobile App"),
            backgroundColor: Colors.blueGrey,
          ),
          body: WebView(
            initialUrl: 'https://flutter.dev',
            javascriptMode: JavascriptMode.unrestricted,
          )),
      debugShowCheckedModeBanner: false,
    );

推荐阅读