简体   繁体   English

Flutter 主题(深色&浅色)

[英]Flutter Theme (dark & light)

I am working on an application that contains two modes, Dark and Light Theme, I used extension to check between them as shown unfortunately.我正在开发一个包含两种模式的应用程序,深色和浅色主题,不幸的是,我使用扩展来检查它们之间的关系。

Extension to check theme mode检查主题模式的扩展

import 'package:flutter/material.dart';

extension DarkMode on BuildContext {
  bool get isDarkMode {
    final brightness = MediaQuery.of(this).platformBrightness;
    return brightness == Brightness.dark;
  }
}

I also have a separate class that contains both Dark Theme and Light Theme我还有一个单独的 class,其中包含 Dark Theme 和 Light Theme

App theme class应用主题 class

class AppTheme {
  
  static final lightTheme = ThemeData(
    // My light theme
  );
  static final darkTheme = ThemeData(
    // My dark theme
  );

}

In the MaterialApp I put these codes在 MaterialApp 我把这些代码

MaterialApp(
        theme: AppTheme.lightTheme,
        darkTheme: AppTheme.darkTheme,
        themeMode: ThemeMode.system,
        // ...
      ),

Now things work correctly when you change the mod from the phone system, the theme changes perfectly as shown below:现在,当您从电话系统更改 mod 时一切正常,主题更改如下所示:

在此处输入图像描述

The problem now is when you select the theme, or rather when you put the Light theme, the theme does not work properly.现在的problem是当您使用 select 主题时,或者更确切地说,当您使用 Light 主题时,主题无法正常工作。 I put an image:我放一张图:

Everything is supposed to change to a light theme, as in the previous video, but it does not happen一切都应该变成浅色主题,就像之前的视频一样,但它并没有发生

MaterialApp(
        theme: AppTheme.lightTheme,
        darkTheme: AppTheme.darkTheme,
        //themeMode: ThemeMode.system,
        themeMode: ThemeMode.light,
        // ...
      ),

在此处输入图像描述

I want to know what is wrong and how can I solve that problem so that everything works correctly when switching between each theme.我想知道出了什么问题以及如何解决该问题,以便在每个主题之间切换时一切正常。

Try to check on Theme.of(context).brightness instead of MediaQuery.of(this).platformBrightness In your DarkMode extension尝试在DarkMode扩展中检查Theme.of(context).brightness而不是MediaQuery.of(this).platformBrightness

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM