简体   繁体   English

如何在 Flutter 中获取小部件内部的主题颜色

[英]How to get theme color inside the widget in Flutter

I have this:我有这个:

Widget build(BuildContext context) {
    return MaterialApp(
      title: 'AnApp',
      theme: ThemeData(
        primarySwatch: Colors.blueGrey,
      ),
      home: MainWidget()
    );
  }
}

So I have set primarySwatch color to blueGrey .所以我将primarySwatch颜色设置为blueGrey How can I access this color inside the MainWidget class?如何在MainWidget class 中访问此颜色?

I want to set a background the same as the AppBar color.我想设置与AppBar颜色相同的背景。

我不确定是否有一种方法可以在像这样的小部件内使用primarySwatch但如果您正在寻找AppBar颜色,它实际上是primaryColor并且您可以使用它

Color color = Theme.of(context).primaryColor;

You can use:您可以使用:

color: Theme.of(context).primarySwatch;颜色:Theme.of(context).primarySwatch;

OR或者

you can change the primaryswatch color in main theme class instead of changing in your class by -您可以通过以下方式更改主主题类中的primaryswatch颜色,而不是在您的类中更改-

Clicking on ctrl + primarySwatch, You will be Headed to theme_data page and there you can change your theme color according to your convience.单击 ctrl + primarySwatch,您将进入 theme_data 页面,在那里您可以根据自己的喜好更改主题颜色。

Please create the AppTheme class and setup dark and light theme请创建 AppTheme class 并设置深色和浅色主题

 Widget build(BuildContext context) {
return MaterialApp(
  debugShowCheckedModeBanner: false,
   theme: AppTheme.lightTheme,
   darkTheme: AppTheme.darkTheme,
   themeMode: theme.isDarkMode ? ThemeMode.dark : ThemeMode.light,

  home: SplashScreen(),
);

} }

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

相关问题 在应用程序中将颜色设置为文本小部件在颤动中普遍适用,而无需每次都提及小部件内部的主题 - Set Color to Text Widget in App universally in flutter without mentioning the theme inside the Widget everytime 如何为 Flutter 中的主窗口小部件内的窗口小部件设置背景颜色? - How to set a background color for a widget inside a main widget in Flutter? 如何在 flutter 树部件中获取父部件颜色? - How to get parent widget color in flutter tree widget? 如何在返回小部件的函数中获取颜色? - How to get color inside a function that returns a widget? 如何在 GridView flutter 中获取小部件的偏移量 - How to get offset of a widget inside a GridView flutter Flutter - 如何更改放置在自定义小部件中的 Color 类的值? - Flutter - How to change the Color class' value that placed inside a custom widget? flutter - 如何从父小部件获取原色? - flutter - how get primary color from parent widget? Flutter 子小部件正在使用错误的 Theme.of(context).color 重建 - Flutter child widget is rebuilding with wrong Theme.of(context).color 如何在 Cupertino 小部件中将颜色更改为相反的主题? - How to change color to opposite theme in cupertino widget? Flutter中如何改变按钮主题的文字颜色 - How to change the text color of the button theme in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM