[英]Set Color to Text Widget in App universally in flutter without mentioning the theme inside the Widget everytime
[英]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.