I use accentColor property, i get that this attribute is deprecated. how can i create an specific accent color using the new way?
the code is:
theme: ThemeData(
accentColor: const Color(accentColor),
),
use this
theme: ThemeData(
colorScheme: Theme.of(context).colorScheme.copyWith(secondary: Color(accentColor))
),
for more info check their doc
https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties
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;
Related
In my Flutter project app_theme.dart file I have this code:
class AppTheme {
static const primaryColor = Color.fromARGB(255, 106, 49, 185);
static ThemeData lightTheme() {
return ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: primaryColor,
brightness: Brightness.light,
),
);
}
static ThemeData darkTheme() {
return ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: primaryColor,
brightness: Brightness.dark,
),
);
}
Then in main.dart, my Material App widget implements this like so:
theme: AppTheme.lightTheme(),
darkTheme: AppTheme.darkTheme(),
This works correctly, so that in the Android emulator, when I switch between dark and light mode (within settings), the app automatically changes.
But what I wanted to do was declare the ColorScheme separately (so I can override certain colors etc) and then just use copyWith to declare the brightness in the ThemeData:
class AppTheme {
static const primaryColor = Color.fromARGB(255, 106, 49, 185);
static ColorScheme colorScheme = ColorScheme.fromSeed(
seedColor: primaryColor,
// Customisation goes here
);
static ThemeData lightTheme() {
return ThemeData(
useMaterial3: true,
colorScheme: colorScheme.copyWith(
brightness: Brightness.light,
),
);
}
static ThemeData darkTheme() {
return ThemeData(
useMaterial3: true,
colorScheme: colorScheme.copyWith(
brightness: Brightness.dark,
),
);
}
But this does not work. It just stays in light mode all the time, although no errors are thrown.
Any ideas what I'm doing wrong?
ColorScheme.fromSeed constructor generates a ColorScheme derived from the given seedColor. Copying the generated object obviously does not generate colours again. Something like this would work:
class AppTheme {
static const primaryColor = Color.fromARGB(255, 106, 49, 185);
static ColorScheme fromBrightness({required Brightness brightness}) {
return ColorScheme.fromSeed(
brightness: brightness,
seedColor: primaryColor,
// Customisation goes here
);
}
static ThemeData lightTheme() {
return ThemeData(
useMaterial3: true,
colorScheme: AppTheme.fromBrightness(
brightness: Brightness.light,
),
);
}
static ThemeData darkTheme() {
return ThemeData(
useMaterial3: true,
colorScheme: AppTheme.fromBrightness(
brightness: Brightness.dark,
),
);
}
}
class BMICalculator extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Colors.red,
),
home: InputPage(),
);
I am taking this course: https://www.udemy.com/course/flutter-bootcamp-with-dart/
and with the section on themes she uses this exact code to turn her appBar red. My code does not display any errors, however my appBar remains the default theme.
The description of primary color here: https://api.flutter.dev/flutter/material/ThemeData-class.html
It doesn't say it is depreciated, or indicate any recent changes.
My question is not "how can I make my app bar red", but rather, "why does this code not perform as expected?"
PrimaryColor won't work in themeData directly you have to declare it in colorScheme
theme: ThemeData(colorScheme: ColorScheme.light(primary: Colors.red)),
You can either use primarySwatch
theme: ThemeData(primarySwatch: Colors.red),
or you can use appBarTheme
appBarTheme: AppBarTheme(
backgroundColor: Colors.red
),
primarySwatch is not a Color. It's MaterialColor. Which means it's a the different shades of a color a material app will use.
primaryColor is one of those shades. To be exact, primaryColor is normally equal to primarySwatch[500]
ThemeData is one holding all of your theme settings, and the one controlling how the app will look, but ColorScheme is just a set of colors that you create to easily maintain the app's colors. Notice that ThemeData class has a parameter colorScheme, so you can create your own colorScheme and add it to the ThemeData object.
all of widgets styling inherits from colors or themes from ThemeData (defined in MaterialApp) not ColorScheme, colorScheme is just extra colors that you define to use whether in ThemeData or anywhere across the app.
AppBar background color comes from appBarTheme(color:..).
I prefer extending parent theme,
return MaterialApp(
primaryColor: Colors.green,// no effect on AppBar
theme: Theme.of(context).copyWith(
appBarTheme: Theme.of(context).appBarTheme.copyWith(
color: Colors.red,
),
),
More about theme.
MaterialApp(
theme: ThemeData.dark().copyWith(
primaryColor: Color(0xFF090C22),
backgroundColor: Color(0xFF090C22),
scaffoldBackgroundColor: Color(0xFF090C22),
appBarTheme: AppBarTheme(
backgroundColor: Color(0xFF090C22),
),
),
);
I am using flutter 2.8.0 to create a simple app.
But when I am trying to change the primary color for the App it does not work with me.
This is the code I am working on:
class BMICalculator extends StatelessWidget {
const BMICalculator({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
primaryColor: const Color(0xFF0A0E21),
scaffoldBackgroundColor: const Color(0xFF0A0E21),
),
home: const InputPage(),
);
}
}
#Mrinal already mentioned you to use primarySwatch. Flutter team intended to build clean up the Theme system and make everything more consistent and based off of the ColorScheme colors. They recommend using color scheme instead of primary color
theme: ThemeData(
primaryColor: ColorsX.primary,
colorScheme: ColorScheme.light().copyWith(primary: ColorsX.primary),
);
For dark theme
theme: ThemeData.dark().copyWith(
colorScheme: ColorScheme.light().copyWith(primary: Colors.tealAccent),
),
output:
For more read this article article
Use primarySwatch
theme: ThemeData(
primarySwatch: Colors.red,
),
I found a solution for me..
MaterialColor _primartSwatch = Colors.purple;
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
themeMode: _themeMode,
theme: ThemeData(
primarySwatch: _primartSwatch,
),
darkTheme: ThemeData(
brightness: Brightness.dark,
primarySwatch: _primartSwatch,
),
home: SplashScreen(),
);
}
I am working with flutter projects
and I want to change the themecolor using an onpressed event
I tried some code but not working.
main.dart file
import 'package:flutter/material.dart';
import 'Screens/HomePage.dart';
void main()=> runApp(MyApp());
ThemeData _lightTheme = ThemeData(
brightness: Brightness.light,
primaryColor: Colors.white,
);
ThemeData _darkTheme = ThemeData(
brightness: Brightness.light,
primaryColor: Colors.white,
);
bool _ktheme = true;
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Wallbay',
theme: _ktheme ? _lightTheme : _darkTheme,
home: MyHomePage('Alpha Papers'),
);
}
}
HomePage.dart file
........
IconButton(
icon: Icon(Icons.brightness_medium_outlined),
onPressed: () {
setState(() {
_ktheme = !ktheme;
});
},
)
......
Your theme objects _lightTheme & _darkTheme are the same objects with different names.
They both have brightness set to Brightness.light & primaryColor as Colors.white.
ThemeData _lightTheme = ThemeData(
brightness: Brightness.light, // <----
primaryColor: Colors.white, // <----
);
ThemeData _darkTheme = ThemeData(
brightness: Brightness.light, // <--- Dark mode having light brightness
primaryColor: Colors.white, // <---- Dark mode having white color
);
Change these to the following:
ThemeData _lightTheme = ThemeData(
brightness: Brightness.light, // light mode
primaryColor: Colors.white,
);
ThemeData _darkTheme = ThemeData(
brightness: Brightness.dark, // dark mode
primaryColor: Colors.black,
);
Also, your onPressed code looks wrong.
setState(() {
_ktheme = !_ktheme; // Notice: I renamed ktheme to _ktheme
});
I want to display dark or light mode based on the settings of the device.
I need to get the brightness of the device when building my ThemeData, something like this:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final materialLightTheme = ThemeData(
primaryColor: Colors.blueAccent,
brightness: Brightness.light,
scaffoldBackgroundColor: MyColors.backgroundColor0(Brightness.light),
appBarTheme: AppBarTheme(
color: MyColors.backgroundColor1(Brightness.light)
),
);
final materialDarkTheme = ThemeData(
primaryColor: Colors.blueAccent,
brightness: Brightness.dark,
scaffoldBackgroundColor: MyColors.backgroundColor0(Brightness.dark),
appBarTheme: AppBarTheme(
color: MyColors.backgroundColor1(Brightness.dark)
),
);
Brightness brightness = MediaQuery.of(context).platformBrightness;
return MaterialApp(
themeMode: brightness == Brightness.light
? ThemeMode.light
: ThemeMode.dark,
theme: materialLightTheme,
darkTheme: materialDarkTheme,
home: MyHomePage(),
);
}
}
But it seems, MediaQuery is not available in the build Method outside of an Material App. I get the Error
MediaQuery.of() called with a context that does not contain a
MediaQuery.
How can I achieve this behaviour?
First for your use case, if you just want to apply dark and light theme based on system's dark mode, then it is automatically handled by the Flutter, just by providing the darkTheme property in your MaterialApp widget, whenever the system's dark mode is activated, the theme supplied in the darkTheme parameter will automatically activate.
However if you do want to access MediaQuery before you apply themes and other stuff to your app via MaterialApp, from what I know you need either MaterialApp or WidgetsApp before in the widget tree in order to get the MediaQuery.of(context). However, we can workaround that by using, two MaterialApp widgets one for just getting the MediaQuery in the second you apply the theming and all other stuff as below
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final materialLightTheme = ThemeData(
primaryColor: Colors.blueAccent,
brightness: Brightness.light,
scaffoldBackgroundColor: MyColors.backgroundColor0(Brightness.light),
appBarTheme: AppBarTheme(
color: MyColors.backgroundColor1(Brightness.light)
),
);
final materialDarkTheme = ThemeData(
primaryColor: Colors.blueAccent,
brightness: Brightness.dark,
scaffoldBackgroundColor: MyColors.backgroundColor0(Brightness.dark),
appBarTheme: AppBarTheme(
color: MyColors.backgroundColor1(Brightness.dark)
),
);
Brightness brightness = MediaQuery.of(context).platformBrightness;
return MaterialApp(
themeMode: brightness == Brightness.light
? ThemeMode.light
: ThemeMode.dark,
theme: materialLightTheme,
darkTheme: materialDarkTheme,
home: MyHomePage(),
);
}
}