2

我正在使用webview_flutter插件,当 webview 在暗模式设备上的任何小米上打开时,它会强制暗模式,我不知道如何避免它

这是我的代码:

import 'dart:async';
import 'dart:io';

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

class CdsWebView extends StatefulWidget {
  const CdsWebView({
    Key key,
    @required this.url,
  }) : super(key: key);

  final String url;

  @override
  _CdsWebViewState createState() => _CdsWebViewState();
}

class _CdsWebViewState extends State<CdsWebView> {
  final Completer<WebViewController> _controller =
  Completer<WebViewController>();

  WebViewController _webViewController;
  bool isLoading = true;

  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Builder(builder: (BuildContext context) {
          return Stack(children: <Widget>[
            WebView(
              initialUrl: widget.url,
              javascriptMode: JavascriptMode.unrestricted,
              onWebViewCreated: (WebViewController webViewController) {
                _controller.complete(webViewController);
                _webViewController = webViewController;
              },
              onPageFinished: (finish) {
                this.setState(() {
                  isLoading = false;
                });
              },
            ),
            isLoading
                ? Container(
              child: Center(
                child: CircularProgressIndicator(
                  valueColor:
                  AlwaysStoppedAnimation<Color>(Colors.red),
                ),
              ),
            )
                : Container()
          ]);
        }),
      ),
    );
  }
}

如果在颤动中无法防止,有没有办法在 webview 页面中防止?

它会像这样更改背景和文本颜色:

普通的

小米暗模式设备

WebView 测试页代码:

<html>
  <head>
  </head>
  <body style="
    display: flex;
    justify-content: center;
    align-items: center;">
    <h1 style="font-family: Arial;">
      My WebView Page
    </h1>
  </body>
</html>
4

1 回答 1

5

Force dark 的实现是为了让开发者更容易轻松地切换到深色主题,给用户一个统一的体验。

它分析浅色主题应用程序的每个视图,并在将其绘制到屏幕之前自动应用深色主题。

在基于 Android 10 或更高版本的 MIUI 中,应用程序被迫使用其深色主题,这可能会导致外观难看的强制深色网页视图和 pwa 应用程序,例如“LinkedIn Go”。

要关闭此功能,请转到您的 android 文件夹,然后转到该\app\src\main\res\values\themes.xml文件。将 forceDarkAllowed 设置为 false <style name="Theme.YOUR_APP_NAME" parent="Theme.MaterialComponents.DayNight"> ,就像这样

    <item name="android:forceDarkAllowed">false</item>

如果这不起作用,请在values-night/themes.xml. 这应该可以解决问题。

如果你仍然感到困惑,把它放在哪里,你的最终代码应该是这样的——

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.YOUR_APP_NAME" parent="Theme.MaterialComponents.DayNight">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/blue_grey_500</item>
        <item name="colorPrimaryVariant">@color/blue_grey_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/grey_200</item>
        <item name="colorSecondaryVariant">@color/grey_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Customize your theme here. -->
        <item name="android:forceDarkAllowed">false</item>
    </style>

</resources>
于 2021-05-18T15:55:24.320 回答