I have two differents flavors with differents styles, and Im trying to init MaterialApp copying this theme and change the brightness to dark or light depending of the settings states, but seems thaht copyWith is not working properly, because the brightness does not changes.
Here is the code:
return MaterialApp(
theme: FlavorConfig.instance.theme.copyWith(
brightness:
Provider.of<SettingsViewModel>(context).darkModeEnabled
? Brightness.dark
: Brightness.light,
),
Any idea?
Try the cupertinoOverrideTheme attribute of the Theme white the copyWith
Something like:
return MaterialApp(
theme: FlavorConfig.instance.theme.copyWith(
brightness:
Provider.of<SettingsViewModel>(context).darkModeEnabled
? Brightness.dark
: Brightness.light,
cupertinoOverrideTheme: FlavorConfig.instance.theme.cupertinoThemeData.copyWith(
brightness: Provider.of<SettingsViewModel>(context).darkModeEnabled
? Brightness.dark
: Brightness.light,
),
),
Related
I'm trying to avoid repeating my code.
My app has light and dark theme modes, and I'm trying to change my app theme in both modes without repeating lines, like the following code.
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
// shared ThemeData between light and dark themes :(
appBarTheme: const AppBarTheme(
toolbarHeight: 100,
),
),
darkTheme: ThemeData(
brightness: Brightness.dark,
// shared ThemeData between light and dark themes :(
appBarTheme: const AppBarTheme(
toolbarHeight: 100,
),
),
themeMode: ThemeMode.dark,
);
As I explained in my code, that there are few lines repeated in both light and dark themedata
// shared ThemeData between light and dark themes :(
appBarTheme: const AppBarTheme(
toolbarHeight: 100,
),
Is there a good and TESTED way to avoid this mistake?
First define your ThemeData as a variable, for example:
final myTheme = ThemeData(
// brightness: Brightness.light,
primarySwatch: Colors.red,
primaryColor: Colors.blue,
cardColor: Color(0xCCF2F2F2),
);
You can then use your variable. If you want to modify a few items, you can use the copyWith constructor. For example:
MaterialApp(
// light theme: use the variable as is
theme: themeData,
// dark theme: need to modify a few things
darkTheme: themeData.copyWith(
brightness: Brightness.dark, // change brightness
),
home: MyHomePage(),
)
make a ThemeData variable and put all the shared properties then use copyWith when you want to change something like this:
ThemeData _themeData = ThemeData(
primarySwatch: Colors.indigo,
appBarTheme: AppBarTheme(
color : Colors.deepOrange,
),
);
then :
theme: _themeData.copyWith(
brightness: Brightness.light,
),
darkTheme: _themeData.copyWith(
brightness: Brightness.dark
),
themeMode: ThemeMode.light,
you can also use ThemeData.from()
I'm getting this error:
'package:flutter/src/material/theme_data.dart': Failed assertion: line 412 pos 12: 'colorScheme?.brightness == null || brightness == null || colorScheme!.brightness == brightness': is not true.
I've used this brightness: Brightness.dark parameter for my dark mode without any problems until a recent update. I updated several things at once, so I'm not sure what caused the change.
Do I need to be setting up my dark mode differently now?
Current dark theme:
darkTheme: ThemeData(
toggleableActiveColor: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
textTheme: _textTheme(),
colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.blue).copyWith(secondary: Colors.blueAccent),
brightness: Brightness.dark,
),
This is a consequence of tightening up the ThemeData constructor wrt the brightness parameter and the ColorScheme's brightness parameter in an update of Flutter. In your example the brightness of the ColorScheme is light (the default), but the ThemeData's brightness is dark.
To get your darkTheme working, you need to remove the brightness parameter and put that in the colorScheme, like so:
darkTheme: ThemeData(
toggleableActiveColor: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.blue)
.copyWith(
secondary: Colors.blueAccent, brightness: Brightness.dark),
),
Just Add the brightness property in fromSwatch Constructor
in Dark Theme
colorScheme: ColorScheme.fromSwatch().copyWith(secondary: Colors.blue,
brightness: Brightness.dark)
in Light Theme
brightness: Brightness.light
I want to give every CircularProgressIndicator a color in my app but what attribute is responsible for it in MaterialApp widget:
MaterialApp(
theme: ThemeData(
// what is the attribute for this widget.
)
)
I used both accentColor and colorScheme.secondary but still not change.
You can use accentColor to change the color of CircularProgressIndicator. It will look like this:
theme: ThemeData(
accentColor: Colors.red,
EDIT:
In the newest version of Flutter, you should use colorScheme instead of accentColor.
colorScheme: ColorScheme.fromSwatch().copyWith(
secondary: Colors.red,
),
In ThemeData there is progressIndicatorTheme attribute which you can assign ProgressIndicatorThemeData constructor to it, and there you can change the default color of ProgressIndicator.
Almost all the Q/A referred on Stackoverflow related to the same topic but didn't get proper solutions.
Main Question: My app having the primary color blue and I want to set Statusbar Text Color white.
What I have tried:
Using SystemChrome: (Using the following code, It's just changing the color of status bar text in the first screen only, other screens are having blue/black combination background/foreground.)
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: MaterialColor(0xFF084775, color), // status bar color
statusBarBrightness: Brightness.light,//status bar brightness
statusBarIconBrightness:Brightness.light , //status barIcon Brightness
));
Screenshots:
Splash Screen:
Dashboard Screen:
Using ThemeData : (This method gives the same result as the above screenshot).
theme: ThemeData(
brightness: Brightness.light, // ADDED THIS LINE..
primarySwatch: MaterialColor(0xFF084775, color),
accentColor: Color(0xffe46b10),
unselectedWidgetColor: Colors.grey,
fontFamily: 'SourceSansPro',
),
I have also checked github issue link but not worked for me.
I just need to change Statusbar Text Color to White. Any help?
To apply for all appBar use
return MaterialApp(
theme: ThemeData(
appBarTheme: Theme.of(context).appBarTheme.copyWith(brightness: Brightness.dark),
),
debugShowCheckedModeBanner: false,
// home: InvoiceList(),
home: widget());
I hope it will works.
Thank you.
As of Flutter 2
return MaterialApp(
theme: ThemeData(
appBarTheme: Theme.of(context)
.appBarTheme
.copyWith(systemOverlayStyle: SystemUiOverlayStyle.light)),
debugShowCheckedModeBanner: false,
home: widget());
I have a theme that can be changed. ThemeData.dark() and ThemeData() . I want to change the default accentColor 's value of the ThemeData.dark(). Is this possible? My codes in main are like this:
theme: snapshot.data
? ThemeData.dark()
: ThemeData(
accentColor: Colors.white,
canvasColor: Colors.blue[400],
appBarTheme: AppBarTheme(color: Colors.blue[400])),
Yes! ThemeData has a method called copyWith where you can copy the original and assign new values to only those you want to change. In your case, it would be something like this:
ThemeData.dark().copyWith(accentColor: Colors.black)