繁体   English   中英

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

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

我有这个:

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

所以我将primarySwatch颜色设置为blueGrey 如何在MainWidget class 中访问此颜色?

我想设置与AppBar颜色相同的背景。

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

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

您可以使用:

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

或者

您可以通过以下方式更改主主题类中的primaryswatch颜色,而不是在您的类中更改-

单击 ctrl + primarySwatch,您将进入 theme_data 页面,在那里您可以根据自己的喜好更改主题颜色。

请创建 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.

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