Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get theme color inside the widget in Flutter

Tags:

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. How can I access this color inside the MainWidget class?

I want to set a background the same as the AppBar color.

like image 773
Mahdi-Malv Avatar asked Aug 14 '19 11:08

Mahdi-Malv


People also ask

How do you get the theme color in Flutter?

Creating an app theme If no theme is provided, Flutter creates a default theme for you. MaterialApp( title: appName, theme: ThemeData( // Define the default brightness and colors. brightness: Brightness. dark, primaryColor: Colors.

How do you get the current theme in Flutter?

You can use ThemeMode inside MaterialApp . ThemeMode. system will get the active theme in the OS and then either use the theme or darkTheme .

What is the use of ThemeData in Flutter?

ThemeData class Null safety. Defines the configuration of the overall visual Theme for a MaterialApp or a widget subtree within the app. The MaterialApp theme property can be used to configure the appearance of the entire app.


1 Answers

I'm not sure if there exist a way to use primarySwatch inside widget like that but if you are looking for AppBar color, it is actually the primaryColor and you can get it using

Color color = Theme.of(context).primaryColor; 
like image 88
CopsOnRoad Avatar answered Sep 26 '22 08:09

CopsOnRoad