繁体   English   中英

如何在 Flutter 中通过更改主题来更改文本颜色

[英]How to change the Text color with change of theme in Flutter

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

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

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

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

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

预期的

这是我目前的输出

实际的

Main.dart

    import 'package:firebase_auth/firebase_auth.dart';
    import 'package:flutter/material.dart';
    import 'package:kjssc/models/user_data.dart';
    import 'package:kjssc/screens/edit_profile_screen.dart';
    import 'package:kjssc/screens/home_screen.dart';
    import 'package:kjssc/screens/sign_up_screen.dart';
    import 'package:provider/provider.dart';
    import 'screens/login_screen.dart';
    import 'screens/sign_up_in_screen.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      static SharedPreferences mainSharedPreferences;
    
      Widget _getScreenID() {
        return StreamBuilder<FirebaseUser>(
          stream: FirebaseAuth.instance.onAuthStateChanged,
          builder: (BuildContext context, snapshot) {
            if (snapshot.hasData) {
              Provider.of<UserData>(context).currentUserID = snapshot.data.uid;
              return HomeScreen();
            } else {
              return SignUpInScreen();
            }
          },
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return ChangeNotifierProvider(
          create: (context) => UserData(),
          child: MaterialApp(
            title: 'My KJSSC',
            debugShowCheckedModeBanner: false,
            home: _getScreenID(),
            // theme: state.themeData,
            theme: ThemeData(
              brightness: Brightness.light,
              indicatorColor: Colors.white,
              primaryColor: Colors.lightBlue[800],
              primaryIconTheme: IconThemeData(
                color: Colors.white,
              ),
              tabBarTheme: TabBarTheme(
                labelColor: Colors.white,
                unselectedLabelColor: Colors.white70,
                indicatorSize: TabBarIndicatorSize.tab,
                labelStyle: TextStyle(fontWeight: FontWeight.bold),
              ),
            ),
            darkTheme: ThemeData(
              brightness: Brightness.dark,
              indicatorColor: Colors.white,
              primaryIconTheme: IconThemeData(
                color: Colors.white,
              ),
              tabBarTheme: TabBarTheme(
                labelColor: Colors.white,
                unselectedLabelColor: Colors.white54,
                indicatorSize: TabBarIndicatorSize.tab,
                labelStyle: TextStyle(fontWeight: FontWeight.bold),
              ),
            ),
    
            routes: {
              LoginScreen.id: (context) => LoginScreen(),
              SignupScreen.id: (context) => SignupScreen(),
              EditProfileScreen.id: (context) => EditProfileScreen(),
            },
          ),
        );
      }
    }

homescreen.dart(代码片段)

    child: Row(
      children: <Widget>[
        Container(
          child: Text(
            'Email Id : ',
             style: Theme.of(context).textTheme.d1
          ),
        ),
        Container(
          child: Text(
            'user1@gmail.com',
             style: Theme.of(context).textTheme.d2,
             overflow: TextOverflow.ellipsis,
        ),
       ),
      ],
    ),

在 Flutter 上更改主题应该会触发 Widget 重建。 我在我的方法中所做的是检查当前主题并更新要在 Text 小部件上使用的样式。

TextStyle? _textStyle;

@override
Widget build(BuildContext context) {
  var brightness = MediaQuery.of(context).platformBrightness;
  bool isDarkMode = (brightness == Brightness.dark);
  
  /// Update TextStyle depending on the theme
  if (isDarkMode){
    _textStyle = CustomTextStyleDark();
  } else {
    _textStyle = CustomTextStyleDefault();
  }
  return ...
}

暂无
暂无

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

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