簡體   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