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.
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.
You can use ThemeMode inside MaterialApp . ThemeMode. system will get the active theme in the OS and then either use the theme or darkTheme .
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With