小编Ath*_*han的帖子

如何在 Flutter 中随着主题的改变而改变文本颜色

我正在 flutter 中开发一个应用程序,它根据系统主题更改其主题数据,即如果用户为其设备启用了深色模式,则应用程序将自动更改为深色模式,反之亦然。在这里我想更改文本应用程序的颜色也是如此。

我创建了自定义文本主题,因为我不想更改默认的TextThemeData。相同的代码如下

文本主题.dart

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    extension CustomTextStyles on TextTheme {
    
      TextStyle get h1 {
        return TextStyle(
          fontSize: 24.0,
          fontWeight: FontWeight.bold,
        );
      }
       
      TextStyle get d1 {
        return TextStyle(
          fontSize: 16.0,
          fontWeight: FontWeight.bold,
          color: Brightness.dark == null ? Colors.blue:Colors.white,
        );
      }
      TextStyle get d2 {
        return TextStyle(fontSize: 16.0);
      }
    }
Run Code Online (Sandbox Code Playgroud)

问题是,每当我切换主题时,文本颜色都不会改变。我尝试过使用 color: ThemeData.dark() == null ? Colors.blue[800] : Colors.white,color: Brightness.dark() == null ? Colors.blue[800] : Colors.white, 但没有任何效果。

这是我在上述代码行之后的预期输出

预期的

这是我当前的输出

实际的

主dart

    import …
Run Code Online (Sandbox Code Playgroud)

flutter

6
推荐指数
1
解决办法
4607
查看次数

标签 统计

flutter ×1