I recently switched my code basis to Flutter 2.
Now I face certain problems with theming (colors):
The color of the device's status bar icons are black
The color of the TextField's context menu items are black
Previously they were white, so something seems to be changed in the new Flutter version.
I am using ThemaData.dark() and some specific colors. Especially I am setting cardColor to a dark grey to color the background of the TextField's context menu:
ThemeData buildTheme() {
final ThemeColors colors = themeColors();
final ThemeData base = ThemeData.dark();
return ThemeData(
scaffoldBackgroundColor: colors.primaryBackground(), // == dark grey
primaryColor: colors.primaryBackground(), // == dark grey
accentColor: colors.accent(), // == orange
textTheme: base.textTheme,
buttonTheme: base.buttonTheme.copyWith(
buttonColor: colors.accent(), // == orange
textTheme: ButtonTextTheme.primary
canvasColor: colors.inputBackground(), // == dark grey
inputDecorationTheme: base.inputDecorationTheme.copyWith(
hintStyle: TextStyle(color: colors.textFieldHintText()), // == grey
fillColor: colors.inputBackground(), // == dark grey
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: colors.focused())), // == green
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: colors.accent())), // == orange
),
unselectedWidgetColor: colors.accent(), // == orange
cardColor: colors.messageBackground()); // == dark grey
}
Do you know why the described colors don't turn into white when using the new Flutter version?
Do you know what I can do to fix that? Maybe change another color field of ThemeData?
My only idea is to remove the cardColor change, which yields a white context menu. But this does not fix the text color problem of the status bar.
For the statusBar only, you can set the brightness of the icons when defining the AppBar:
AppBar(
backwardsCompatibility: false,
backgroundColor: Colors.grey.shade800,
systemOverlayStyle: SystemUiOverlayStyle(
statusBarColor: Colors.grey.shade800,
statusBarIconBrightness: Brightness.light,
),
),
But with this settings your appbar wouldn't get the color from your theme.
You'll have to set background color and statusBarColor from there.
I did set Colors.grey.shade800 since I didn't have acces to your colors.
For the sistem UI, like the copy/paste, you can solve your problem by adding brightness: Brightness.dark inside your ThemeData.
I wouldn't modify the status bar colour in AppBar because then when you use a different app bar in a different part of the app, you might change it there.
I find it nicer to change the theme brightness, which might fix the issue with the text color theming too:
...
theme: ThemeData(
brightness: Brightness.dark,
...
)
...
or alternatively:
...
theme: ThemeData.dark().copyWith(
...
)
...
Otherwise you might just be using the wrong text theme.
I think your are just missing a tiny part.
You create a dark theme with ThemeData.dark() but then you create a light theme with return ThemeData(...)
Instead create the ThemeData directly with the intended brightness, all the values
that you do not specify will be defaulted depending on the brightness value in
the same way ThemeData.dark() does create them.
ThemeData buildTheme() {
final ThemeColors colors = themeColors();
return ThemeData(
brightness: Brightness.dark,
scaffoldBackgroundColor: colors.primaryBackground(), // == dark grey
primaryColor: colors.primaryBackground(), // == dark grey
accentColor: colors.accent(), // == orange
buttonTheme: ButtonThemeData(
buttonColor: colors.accent(), // == orange
textTheme: ButtonTextTheme.primary,
),
canvasColor: colors.inputBackground(), // == dark grey
inputDecorationTheme: InputDecorationTheme(
hintStyle: TextStyle(color: colors.textFieldHintText()), // == grey
fillColor: colors.inputBackground(), // == dark grey
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: colors.focused())), // == green
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: colors.accent())), // == orange
),
unselectedWidgetColor: colors.accent(), // == orange
cardColor: colors.messageBackground(), // == dark grey
);
}
If you look in the implementation of ThemeData here:
You find this: factory ThemeData.dark() => ThemeData(brightness: Brightness.dark);
And if you look here you can see that the default brightness is Brightness.light and in the following lines you can find out which defaults are created depending on the brightness value.
Be careful about using ThemeData.copyWith as there are no defaults set when calling this.
Related
With Material 3, colorSchemeSeed can be used to set the color for the entire app. This works for colors like Red, Blue, and Green. If left as default the color will be purple. When trying to use black color as a seed, the color defaults to purple.
What would be the best way to have the Material 3 app, but have the color of the app Theme be plain black and white (in light and dark mode)?
Easiest way would be to setup your ThemeData colorScheme manually.
This is for light theme, you can just set Brightness.dark and swap around black and white for dark theme
ThemeData(
useMaterial3: true,
colorScheme: ColorScheme(
brightness: Brightness.light,
primary: Colors.black,
onPrimary: Colors.white,
secondary: Colors.white,
onSecondary: Colors.black,
error: Colors.red,
onError: Colors.white,
background: Colors.white,
onBackground: Colors.black,
surface: Colors.white,
onSurface: Colors.black,
),
textTheme: textTheme,
)
I have read that primaryColor is a Color, primarySwatch is MaterialColor, which includes various set of a Color.
first, according to my flutter class, I set Theme like this
primarySwatch: Colors.pink,
accentColor: Colors.amber,
and when I set splashColor of InkWell, it worked.
splashColor: Theme.of(context).primaryColor,
but the VsCode suggest me a better way, and automatically migrated Theme like this
colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.pink)
.copyWith(secondary: Colors.amber),
But the new way does not changes primaryColor of splashColor, I checked it Through By print the Color, but the Color was 0xff2196f3, which is default I think(close to blue).
so Why the new way does not set primaryColor?
in new way, you should use it like this:
splashColor: Theme.of(context).colorScheme.primary,
This is my theme:
final ThemeData theme = ThemeData();
return MaterialApp(
title: 'Notes',
home: SignInPage(),
debugShowCheckedModeBanner: false,
theme: theme.copyWith(
primaryColor: Colors.green[800],
colorScheme: theme.colorScheme
.copyWith(secondary: Colors.green, secondaryVariant: Colors.green),
floatingActionButtonTheme: FloatingActionButtonThemeData(
backgroundColor: Colors.blue[900],
),
inputDecorationTheme: InputDecorationTheme(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
);
In the old days of flutter if you'd put in the primary color to green, the border of the focused text fields would turn green too. Now, I want all of my text fields, when in focus, to have a green border, green prefix icon and green label text, all from the root Theme. But this is the result I get with the code above:
I want the lock, the "password" label, and border to be all green when focused, and grey when not focused. How can I do this from the root Theme of the app. I have the primaryColor set up to green and even the colorScheme secondary color set up to green, but still everything is blue, instead of green.
Add this worked for me
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Color.fromRGBO(126, 132, 138, 1),),
),
The actual way now to change the whole text field colors is by changing the colorScheme.
colorScheme: theme.colorScheme.copyWith(
primary: Colors.green,
),
In my MaterialApp widget, I'm using the theme property to set the universal theme for my app. I want to set a blue color for the text in the AppBar, and red color for text in the Scaffold body. But using textTheme in ThemeData, only the Scaffold's text has the intended color, not the AppBar text.
This is the code for the theme I'm using in MaterialApp:
theme: ThemeData(
primaryColor: Color(0xFF0A0E21), //a navy bluish color
scaffoldBackgroundColor: Color(0xFF0A0E21),
accentColor: Colors.purple,
textTheme: TextTheme(
headline6: TextStyle(
color: Colors.blue
),
bodyText2: TextStyle(
color: Colors.red
)
)
The AppBar text remains white, but text in the body of the app (in the Scaffold I'm using) changes to red. I'm not setting different colors anywhere else.
you have to use appBarTheme on themeData
ThemeData(
scaffoldBackgroundColor: Color(0xFF0A0E21),,
appBarTheme: AppBarTheme(
color: Colors.blue,
),
When the user select a text from inside a TextField, the default highlight color is blue. How to change it to green for example ?
2021 answer
Wrap with Theme and use copyWith to preserve other theme data.
Theme(data: Theme.of(context).copyWith(
textSelectionTheme: TextSelectionThemeData(
selectionColor: Colors.green)),
child: TextFormField()
)
Wrap your text widget with theme and assign the color to the textSelectionColor property inside ThemeData
refer below code for same:- I have changed the text selection color to green
Theme(
data: ThemeData(textSelectionColor: Colors.green),
child: TextField(
controller: _inputController,
decoration: InputDecoration(hintText: "Input"),
),
),
change the value of textSelectionColor of your ThemeData and it will give you the result you are looking for.
please use this code.
Widget build(BuildContex contex){
return MaterialApp{
debugShowCheckedModeBanner: false,
theme: ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.green,
iconTheme: IconThemeData(
color: kGreenColor,
),
hoverColor: kPrimaryColor,
indicatorColor: kPrimaryColor,
),
}
}