简体   繁体   English

如何在 flutter 中更改 ThemeData 的“原色”?

[英]How to change the `primary color` for ThemeData dark in flutter?

I am using flutter 2.8.0 to create a simple app.我正在使用 flutter 2.8.0创建一个简单的应用程序。 But when I am trying to change the primary color for the App it does not work with me.但是当我尝试更改应用程序的原色时,它不适用于我。

This is the code I am working on:这是我正在处理的代码:

class BMICalculator extends StatelessWidget {
  const BMICalculator({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        primaryColor: const Color(0xFF0A0E21),
        scaffoldBackgroundColor: const Color(0xFF0A0E21),
      ),
      home: const InputPage(),
    );
  }
}

Use primarySwatch使用primarySwatch

theme: ThemeData(
primarySwatch: Colors.red,
),

@Mrinal already mentioned you to use primarySwatch. @Mrinal 已经提到您使用primarySwatch。 Flutter team intended to build clean up the Theme system and make everything more consistent and based off of the ColorScheme colors. Flutter 团队打算建立清理主题系统并使一切更加一致,并基于 ColorScheme colors。 They recommend using color scheme instead of primary color他们建议使用配色方案而不是原色

 theme: ThemeData(
     primaryColor: ColorsX.primary,
     colorScheme: ColorScheme.light().copyWith(primary: ColorsX.primary),
  );

For dark theme对于深色主题

theme: ThemeData.dark().copyWith(
        colorScheme: ColorScheme.light().copyWith(primary: Colors.tealAccent),
        ),

output: output:

在此处输入图像描述

For more read this article article有关更多信息,请阅读这篇文章

I found a solution for me..我为我找到了解决方案..

MaterialColor _primartSwatch = Colors.purple;

 @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MyApp',
      themeMode: _themeMode,
      theme: ThemeData(
        primarySwatch: _primartSwatch,
      ),
      darkTheme: ThemeData(
        brightness: Brightness.dark,
        primarySwatch: _primartSwatch,
      ),
      home: SplashScreen(),
    );
  }

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

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