Changing one property of ThemeData.dark() - flutter

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)

Related

flutter: custom color for App Bar with ThemeData

How can I make a custom color for App Bar with ThemeData?
accentColor is deprecated while colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.indigo)) expects type MaterialColor.
I know that I can make it this way
Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFF0A0E21),
title: Text('CAPTION'),
),
But I want to specify it in ThemeData
Rather than overriding everything, it often makes sense to extend the parent theme. You can handle this by using the copyWith() method.
More about extending-the-parent-theme.
return MaterialApp(
home: const T1(),
theme: Theme.of(context).copyWith(
appBarTheme: Theme.of(context).appBarTheme.copyWith(
backgroundColor: const Color(0xFF0A0E21),
),
),
As you mentioned, the first way is to set it with backroundColor property.
If this property is null, then the AppBarTheme.backgroundColor is used. AppBarTheme can be set using appBarTheme property of ThemeData.
If AppBarTheme.backgroundColor is also null, then AppBar uses the overall theme's ColorScheme.primary if the overall theme's brightness is Brightness.light, and ColorScheme.surface if the overall theme's brightness is Brightness.dark.

Theme responsible for CircularProgressIndicator

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.

How to globally override default Colors.blue color for the ThemeData?

In flutter even when I set
primaryColor: Colors.orange,
then in some part of the UI the default Colors.blue is still present.
Is there a way how to globally override it for the ThemeData?
In order for me to override Colors.blue, I had to state primarySwatch in the ThemeData and also the colorScheme, as in
theme: ThemeData(
primarySwatch: Colors.orange,
colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.orange),
),
Try:
primarySwatch: Colors.orange

What's the difference between accessing the text theme via .of(context) method instead of statically?

In Flutter we can write our ThemeData in two ways,
1: As variable
2: As a method
final lightThemeData = ThemeData(
brightness: Brightness.light,
primaryColor: primaryColor,
textTheme: GoogleFonts.poppinsTextTheme(
ThemeData.light().textTheme,
),
elevatedButtonTheme: elevatedButtonThemeData,
colorScheme: ThemeData.light()
.colorScheme
.copyWith(secondary: secondaryColorLightTheme),
);
ThemeData buildThemeData(BuildContext context) {
return ThemeData(
brightness: Brightness.light,
primaryColor: primaryColor,
textTheme: GoogleFonts.poppinsTextTheme(
Theme.of(context).textTheme,
),
elevatedButtonTheme: elevatedButtonThemeData,
colorScheme: Theme.of(context)
.colorScheme
.copyWith(secondary: secondaryColorLightTheme),
);
}
Check the image for more clear explanation
There is no better way here since the two snippets have different behaviors:
In the first one, the theme is independent from where it's used.
In the second snippet, the theme inherits from the ambient theme because of the Theme.of(context).
But the context variant is significantly better
Because with the "create a new theme" variant, there's no single source of truth for the theme. Theming is all over the place

Theme copyWith not changing brightness

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,
),
),