migrating accent color in flutter v2.5 - flutter

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;

Related

Default color scheme 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.

Shared ThemeData between light and dark themes

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

Flutter textTheme property is not working, How to replace or fix it?

I am new to flutter. I just created a theme file to define light and dark theme.When I defined light theme, the textTheme: property which is deprecated is not changing the text title color of app bar into black.
If I have to create or define textTheme or replace it ?? How should I do it ?
class MyTheme {
static ThemeData lightTheme(BuildContext context) => ThemeData(
primarySwatch: Colors.deepPurple,
fontFamily: GoogleFonts.lato().fontFamily,
appBarTheme: AppBarTheme(
color: Colors.white,
elevation: 0.0,
iconTheme: IconThemeData(color: Colors.black),
//------
textTheme: Theme.of(context).textTheme, // Problem is here
//------
));
static ThemeData darkTheme(BuildContext context) =>
ThemeData(brightness: Brightness.dark);
}
Change it to this
textTheme: Theme.of(context).appBarTheme.textTheme,
PS: It's deprecated. try to migrate.
Use toolbarTextStyle and titleTextStyle instead of using textTheme inside appBarTheme.
toolbarTextStyle:
Theme.of(context).appBarTheme.toolbarTextStyle?.copyWith(
color: Colors.amber,
///your config
),
titleTextStyle:
Theme.of(context).appBarTheme.titleTextStyle?.copyWith(
color: Colors.amber, ///your config
)
More about using theme.

how to add font family into in main.dart flutter

I'm new to flutter and struggling to add font family in my code..already updated my pubspec.yamal with OpenSans fonts.. now how to add font family into this main. dart material app widget??
fontFamily: "OpenSans",
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: theme.copyWith(
colorScheme: theme.colorScheme.copyWith(primary: Color(0xff075e54),secondary: Color(0xff128C7E),
),
),
home: Homescreen(key: null),
);
}
}
You can try this
theme: theme.copyWith(
colorScheme: theme.colorScheme.copyWith(primary: Color(0xff075e54),secondary: Color(0xff128C7E)),
textTheme: theme.textTheme.apply(fontFamily: "OpenSans"),
),
Create a simple file for the theme like the one I created theme_util.dart and use it as follows.
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: appTheme,
home: Homescreen(key: null),
);
theme_util.dart
ThemeData appTheme = ThemeData(
backgroundColor: appColor.white,
scaffoldBackgroundColor: appColor.white,
appBarTheme: AppBarTheme(
color: appColor.white,
brightness: Brightness.light,
textTheme: TextTheme().copyWith(headline6: titleStyle),
iconTheme: IconThemeData(color: appColor.primaryColor)),
fontFamily: 'Opensans',
primaryColor: appColor.primaryColor,
primaryColorDark: appColor.primaryDarkColor,
primaryColorLight: appColor.primaryLight,
accentColor: appColor.accentColor,
brightness: Brightness.light,
//primarySwatch: appColor.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
buttonTheme: ButtonThemeData(buttonColor: appColor.primaryColor),
);
You can create a style file in your dart folder and use the font where ever you want like this way-text_styles.dart
class TextStyles {
static TextStyle overline(
{required BuildContext context, required Color color}) {
return GoogleFonts.montserrat(
textStyle: Theme.of(context).textTheme.overline?.copyWith(color: color),
);
}
}
and use it as-
TextStyles.overline(context: context, color: theme.textColor).copyWith(fontWeight: FontWeight.w900),
Note:if you use custom font you have to install the package from your pubspec.yaml.Example-
google_fonts:

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