Default color scheme Flutter - flutter

In this tutorial author show how to prepare new ColorScheme:
const ColorScheme _customColorScheme = ColorScheme(
primary: customMagenta50,
primaryVariant: customMagenta600,
secondary: Colors.amber,
secondaryVariant: customMagenta400,
surface: Colors.purpleAccent,
background: customSurfaceWhite,
error: customMagenta900,
onPrimary: Colors.red,
onSecondary: Colors.deepOrange,
onSurface: customMagenta300,
onBackground: customMagenta100,
onError: Colors.redAccent,
brightness: Brightness.light,
);
A lot of parameters are required in this constructor. Where I can find default colours used in default light theme? I want use code as above and change only one or two values (another values I change at next phase of my project).

You can apply changes to the default light ColorScheme like so:
final ColorScheme _colorScheme = ColorScheme.light().copyWith(->your changes<-);

You can check the source-code on GitHub. For default light colorSchema
You can use copyWith constructor.
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
colorScheme: Theme.of(context).colorScheme.copyWith(
//..here
)
),
An easy way to find attributes by pressing ctrl+space while you are inside the copyWith(..here.) method
You can find more details on flutter.dev > ColorScheme.

Related

how could I change the modes of flutter app with the mode of the mobile itself?

how could I change the modes of flutter app with the mode of the mobile itself?
is there any available way to make this ?
Okay since you want to change your apps theme according to the user's current theme you have to:
Create a class that defines both the themes:
Here is a link of one of my recent projects where I am using both dark and light themes :
class CustomTheme {
static final lightTheme = ThemeData(
appBarTheme: AppBarTheme(
backgroundColor: CustomColors.coral,
foregroundColor: Colors.grey.shade700),
colorScheme: ColorScheme(
brightness: Brightness.light,
primary: CustomColors.coral,
onPrimary: CustomColors.coral,
secondary: CustomColors.babyPink,
),
)),
static final darkTheme = ThemeData(
colorScheme: ColorScheme(
brightness: Brightness.dark,
primary: CustomColors.onBackground,
onPrimary: CustomColors.onBackground,
secondary: CustomColors.background,
onSecondary: CustomColors.background,
error: CustomColors.errorColor,
onError: CustomColors.onErrorColor,
background: CustomColors.background,
onBackground: CustomColors.onBackground,
surface: CustomColors.coral,
onSurface: CustomColors.onBackground)),
Then specify your theme in the main.dart file
MaterialApp(
themeMode: ThemeMode.system,
theme: CustomTheme.lightTheme,
darkTheme: CustomTheme.darkTheme,
home: const SplashScreen(),
debugShowCheckedModeBanner: false,
);
Then use these themes wherever you want, so if you have a container then you can say
color: Theme.of(context).colorScheme.secondary,
Checkout one of my apps where I am using both themes
https://github.com/MayurSMahajan/FrontendSIH/tree/main/daemon/lib
If by mode you mean Orientation, for instance Portrait mode to Landscape mode, then checkout this answer here, it might help you:
How to detect orientation change in layout in Flutter?
Or directly checkout flutter docs :
https://flutter.io/cookbook/design/orientation/

migrating accent color in flutter v2.5

after the flutter 2.5 update my theme data kinda broke and doesn't accept accentColor anymore. I took a look at the documantation and saw that is "renamed" to colorScheme.secondary. But no matter what I try, I can't get it to work for me.
This is my current code:
class Themes {
static final lightTheme = ThemeData(
accentColor: Palette.orange,
colorScheme: ColorScheme.light(),
floatingActionButtonTheme: FloatingActionButtonThemeData(
backgroundColor: Palette.orange,
foregroundColor: Colors.white,
),
scaffoldBackgroundColor: Colors.white,
);
static final darkTheme = ThemeData(
accentColor: Palette.orange,
colorScheme: ColorScheme.dark(),
floatingActionButtonTheme: FloatingActionButtonThemeData(
backgroundColor: Palette.orange,
foregroundColor: Colors.white,
),
scaffoldBackgroundColor: Colors.grey[900],
);
}
So many changes are in flutter 2.5
Try to use Below code hope it's helpful to you
theme: ThemeData(
colorScheme: Theme.of(context).colorScheme.copyWith(secondary: Color(accentColor))
),
for more information check official documentation here
final ThemeData theme = ThemeData();
MaterialApp(
theme: theme.copyWith(
colorScheme: theme.colorScheme.copyWith(secondary: myColor),
),
//...
)
Code before migration:
Color myColor = Theme.of(context).accentColor;
Code after migration:
Color myColor = Theme.of(context).colorScheme.secondary;

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

Changing text color for dark mode in Flutter(with Dynamic Theme)?

Text turn to white when I got select dark mode but I want to make all texts white70 or something(including buttons and regular texts). How can I definde the default text color for dark mode?
My Theme data like this right now:
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return DynamicTheme(
defaultBrightness: Brightness.light,
data: (brightness) => ThemeData(
primarySwatch: Colors.blueGrey,
brightness: brightness,
),
You can do something similar to this (Feel free to change things as you'd like):
At first go to ios/Runner folder. Next open info.plist and add the following lines into the Dict section.
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
Next. Make sure you have these lines in Theme settings of your MaterialApp:
MaterialApp(
themeMode: ThemeMode.light, // Change it as you want
theme: ThemeData(
primaryColor: Colors.white,
primaryColorBrightness: Brightness.light,
brightness: Brightness.light,
primaryColorDark: Colors.black,
canvasColor: Colors.white,
// next line is important!
appBarTheme: AppBarTheme(brightness: Brightness.light)),
darkTheme: ThemeData(
primaryColor: Colors.black,
primaryColorBrightness: Brightness.dark,
primaryColorLight: Colors.black,
brightness: Brightness.dark,
primaryColorDark: Colors.black,
indicatorColor: Colors.white,
canvasColor: Colors.black,
// next line is important!
appBarTheme: AppBarTheme(brightness: Brightness.dark)),

Change color of RaisedButton from theme not working

I tried changing the color of all my RaisedButtons from the themeData but it refused to work. All other properties, such as fontSize and fontWeight changed successfully. The color of the text only changes from black to white when the brightness property of themeData is changed to Brightness.dark.
Is there a way I can solve this issue? What could I be doing wrong?
Here is my sample code:
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primaryColor: Color(0XFF212845),
scaffoldBackgroundColor: Color(0XFF212845),
primarySwatch: Colors.yellow,
buttonColor: Color(0XFFF8D320),
textTheme: TextTheme(
button: TextStyle(
color: Colors.green, // This is not working.
fontSize: 30.0,
fontWeight: FontWeight.bold
)
)
),
home:MenuPage(),
);
For other people coming to this question, one reason that a button may not change colors is that it is disabled, which happens when you don't have the onPressed method set.
RaisedButton(
color: Theme.of(context).accentColor,
onPressed: () {}, // <-- need to add this
child: Text(...),
),
if you are giving a color to the color property and it doesn't show ,then probably you haven't implemented the onPressed property, because in this state the button will show it's disabled color , which is no color at all.
set it like this:
onPressed: () {},
giving it an anonymous function like that while not implementing anythig ( or something if you wish) will give it color
Add buttonTheme and accentColor to your ThemeData , like this:
ThemeData(
primaryColor: Color(0XFF212845),
scaffoldBackgroundColor: Color(0XFF212845),
primarySwatch: Colors.yellow,
buttonColor: Color(0XFFF8D320),
buttonTheme: ButtonThemeData(textTheme: ButtonTextTheme.accent),
accentColor: Colors.green,
Even though primarySwatch might have been added, you still need to add buttonColor to add the color to the buttons, like this:
child: MaterialApp(
home: Wrapper(),
theme: ThemeData(
primarySwatch: Colors.blue,
buttonColor: Colors.blue // this is needed
),
),
primarySwatch - used to configure default values for several fields,
including: primaryColor, primaryColorBrightness, primaryColorLight,
primaryColorDark, toggleableActiveColor, accentColor, colorScheme,
secondaryHeaderColor, textSelectionColor, backgroundColor, and
buttonColor.
Also, make sure onPressed in your RaisedButton is set:
onPressed: () {},
Make sure you haven't customize the RaisedButton() itself, else it will override the ThemeData. If you have customized color property in RaisedButton it will override the properties set in ThemeData.
I believe the correct way is to declare a buttonColor property in your ThemeData Widget.
MaterialApp(
theme: ThemeData(
fontFamily: 'Pirata',
primaryColor: Color.fromRGBO(71, 86, 87, 1),
accentColor: Color.fromRGBO(71, 86, 87, 1),
buttonColor: Color.fromRGBO(238, 238, 238, 1),
),
home: App()))