I have created my own theme in Flutter but when I try to use these colours they are much lighter on the emulator. I'm new to Flutter so I'm not sure of the best way to create a theme but the colours do appear. What is the best way to set your own theme or why would the colours be lighter?
import 'package:flutter/material.dart';
extension CustomPrimaryColorScheme on ColorScheme {
Color get primaryColor => const Color(0xfffa4659);
Color get accentColor => const Color(0xfff0fff3);
Color get watermelonLight => const Color(0xffFFF0F1);
}
extension CustomColorScheme on ColorScheme {
Color get aqua =>const Color(0xff11cbd7);
Color get paleAqua => const Color(0xffc6f1e7);
Color get ice => const Color(0xfff0fff3);
Color get watermelon => const Color(0xfffa4659);
Color get brownGrey => const Color(0xff979797);
Color get charcoal => const Color(0xff313939);
}
backgroundColor: Theme.of(context).colorScheme.watermelon;
Related
Can Anybody tell me how the change the color of this statusbar into other colors. As you can see in the following image it's a dark color and the time, battery, network details also shown in dark. so that's making them invisible.
I either want to change the color dynamically like when the background color becomes dark the text color of the system widgets ( time , battery, network) becomes white and when color is light those becomes dark.
or If I have to hardcode the color each time. where should I do it? Any solution will be appreciable.
this package: https://pub.dev/packages/flutter_statusbarcolor
import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
FlutterStatusbarcolor.setStatusBarColor(Colors.white);
return MaterialApp(
title: "Flutter",
theme: ThemeData(
primarySwatch: Colors.grey,
),
home: HomeScreen(),
);
}
}
I found how to achieve this in flutter built in functions.
in MaterialApp go inside theme
create AppBarTheme and give the property brightenss: Brightness.dark or Brightness.light based on your requirement. If your status bar color is dark then give Brightness.dark it will make icons and text in white color and vice versa for light background.
I'm trying to make custom dark mode for my app.
Is there a way I set the buttons color, primary color and other themes colors,
then make a button to swap between light and dark mode?
`static get Black => true ? black_1D26 : black1_dark_0x161D;
static get lightviolet => true ? lightviolet_879B :lightviolet_dark_0x9CB1;
static get blue => true ? blue_0x2348FF : blue_dark_0x4B78`;
You could use a ThemeProvider that holds the selected theme and you can implement getter functions inside of the provider
One way is to:
Create a class that contains a ThemeData object.
select items in that object you want to change depends on the theme you selected.
something like this, for primaryColor,
bool lightTheme = true;
primaryColor : lightTheme ? Colors.blue : Colors.red,
find all the properties for ThemeData class below.
ThemeData(
{Brightness? brightness,
VisualDensity? visualDensity,
MaterialColor? primarySwatch,
Color? primaryColor,
Brightness? primaryColorBrightness,
Color? primaryColorLight,
Color? primaryColorDark,
Color? accentColor,
Brightness? accentColorBrightness,
Color? canvasColor,
Color? shadowColor,
Color? scaffoldBackgroundColor,
Color? bottomAppBarColor,
Color? cardColor,
Color? dividerColor,
Color? focusColor,
Color? hoverColor,
Color? highlightColor,
Color? splashColor,
InteractiveInkFeatureFactory? splashFactory,
Color? selectedRowColor,
Color? unselectedWidgetColor,
Color? disabledColor,
Color? buttonColor,
ButtonThemeData? buttonTheme,
ToggleButtonsThemeData? toggleButtonsTheme,
Color? secondaryHeaderColor,
#Deprecated('Use TextSelectionThemeData.selectionColor instead. ' 'This feature was deprecated after v1.23.0-4.0.pre.') Color? textSelectionColor,
#Deprecated('Use TextSelectionThemeData.cursorColor instead. ' 'This feature was deprecated after v1.23.0-4.0.pre.') Color? cursorColor,
#Deprecated('Use TextSelectionThemeData.selectionHandleColor instead. ' 'This feature was deprecated after v1.23.0-4.0.pre.') Color? textSelectionHandleColor,
Color? backgroundColor,
Color? dialogBackgroundColor,
Color? indicatorColor,
Color? hintColor,
Color? errorColor,
Color? toggleableActiveColor,
String? fontFamily,
TextTheme? textTheme,
TextTheme? primaryTextTheme,
TextTheme? accentTextTheme,
InputDecorationTheme? inputDecorationTheme,
IconThemeData? iconTheme,
IconThemeData? primaryIconTheme,
IconThemeData? accentIconTheme,
SliderThemeData? sliderTheme,
TabBarTheme? tabBarTheme,
TooltipThemeData? tooltipTheme,
CardTheme? cardTheme,
ChipThemeData? chipTheme,
TargetPlatform? platform,
MaterialTapTargetSize? materialTapTargetSize,
bool? applyElevationOverlayColor,
PageTransitionsTheme? pageTransitionsTheme,
AppBarTheme? appBarTheme,
ScrollbarThemeData? scrollbarTheme,
BottomAppBarTheme? bottomAppBarTheme,
ColorScheme? colorScheme,
DialogTheme? dialogTheme,
FloatingActionButtonThemeData? floatingActionButtonTheme,
NavigationRailThemeData? navigationRailTheme,
Typography? typography,
NoDefaultCupertinoThemeData? cupertinoOverrideTheme,
SnackBarThemeData? snackBarTheme,
BottomSheetThemeData? bottomSheetTheme,
PopupMenuThemeData? popupMenuTheme,
MaterialBannerThemeData? bannerTheme,
DividerThemeData? dividerTheme,
ButtonBarThemeData? buttonBarTheme,
BottomNavigationBarThemeData? bottomNavigationBarTheme,
TimePickerThemeData? timePickerTheme,
TextButtonThemeData? textButtonTheme,
ElevatedButtonThemeData? elevatedButtonTheme,
OutlinedButtonThemeData? outlinedButtonTheme,
TextSelectionThemeData? textSelectionTheme,
DataTableThemeData? dataTableTheme,
CheckboxThemeData? checkboxTheme,
RadioThemeData? radioTheme,
SwitchThemeData? switchTheme,
bool? fixTextFieldOutlineLabel,
#Deprecated('No longer used by the framework, please remove any reference to it. ' 'This feature was deprecated after v1.23.0-4.0.pre.') bool? useTextSelectionTheme}
)
I have a class where I specify ThemeData for my app.
I use this class to set the appropriate theme in either the MaterialApp or CupertinoApp.
return CupertinoApp(
//...
theme: AppicationTheme.iosTheme()
//...
);
My IOS theme is provided as follows
static CupertinoThemeData iosTheme(){
return CupertinoThemeData(primaryColor: myPrimaryColor);
}
However when trying to set the color on an Icon, the primary color is still default blue as if never set to my color.
You are probably using Theme.of(context).primaryColor,
switch Theme to CupertinoTheme.
example:
Icon(Icons.access_alarm,<br>
color: CupertinoTheme.of(context).primaryColor,
),
I'm trying to integrate material-ui to my project and I have some issues with custom theme settings
I created a custom theme this way
App.js
const theme = createMuiTheme({
palette: {
primary: green,
secondary: red,
},
});
class App extends Component {
render() {
return (
<MuiThemeProvider theme={theme}>
<BrowserRouter>
<Switch>
...}}
Then in a component in the substructure I create some specific css.
Now my issue is that I'm obliged to define style appBar with a backgroud color and apply it explicitely on the AppBar component. If I don't do one of these two operations, the bg of the appBar remains light gray
What is weired is that I get the correct green from theme.palette.primary["500"], which means the theme is correctly configured
Header.js
const styles = theme => ({
root: {
flexGrow: 1,
},
grow: {
flexGrow: 1,
},
appBar: {
backgroundColor: theme.palette.primary["500"]
},//...)}
class Header extends Component {
constructor(props) {
super(props)
this.classes = props.classes
}
render() {
return (<I18n>
{(tsl, {i18n, t, ready}) => {
return (
<div className={this.classes.root}>
<AppBar position="static" color="default" className={this.classes.appBar}>
<Toolbar>...(irrelevant code)
I followed the examples in https://material-ui.com/demos/app-bar/ where the first example has the light gray color, then all the other examples have a blue bg, but there's nothing in the source code that was added to apply the blue color (in my opinion)
Any help please? thanks folks
Use <AppBar position="static" color="primary">.
By default AppBar is using the colors from the grey palette.
We are considering defaulting to primary for the color prop since it's not following the spec and having grey as a default for something as prominent as the app bar is a bad idea anyway.
In Flutter, one can apply a theme to the app using ThemeData class. But there two propeties of this class that confuses me: primaryColor and primarySwatch. What's the difference between these two properties and when to use one or the other? Thanks.
primarySwatch is not a Color. It's MaterialColor.
Which means it's 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].
It is usually better to define a primarySwatch instead of primaryColor. Because some material components may use a different shade of the primaryColor for things such as shadow, border, ...
The following is taking from my perusal of theme_data.dart:
primarySwatch defaults to Colors.blue and sets the following fields (including primaryColor) to various shades of the MaterialColor input depending on whether the theme brightness is light or dark (default is light):
Light Themes
// The default shade for the color is used
primaryColor = primarySwatch; // [500] for normal colors and [200] for accent colors
primaryColorLight = primarySwatch[100];
primaryColorDark = primarySwatch[700];
// This can be overridden by setting accentColor (below) manually
toggleableActiveColor = primarySwatch[600];
accentColor = primarySwatch[500];
secondaryHeaderColor = primarySwatch[50];
textSelectionColor = primarySwatch[200];
textSelectionHandleColor = primarySwatch[300]
backgroundColor = primarySwatch[200];
*buttonColor is set to its default (grey[300])
Dark Themes
buttonColor = primarySwatch[600];
*The remaining fields listed above for light themes are set to their dark defaults (various shades of tealAccent, grey or black)
All Themes (light or dark)
// Brightness.dark/light is estimated based on the default shade for the color
// This also sets the bool primaryIsDark
primaryColorBrightness = estimateBrightnessForColor(primarySwatch);
// This generates the modern simplified set of theme colors flutter recommends
// using when theming Widgets based on the theme. Set it manually if you need
// more control over individual colors
colorScheme = ColorScheme.fromSwatch(
primarySwatch: primarySwatch, // as above
primaryColorDark: primaryColorDark, // as above
accentColor: accentColor, // as above
cardColor: cardColor, // default based on theme brightness, can be set manually
backgroundColor: backgroundColor, // as above
errorColor: errorColor, // default (Colors.red[700]), can be set manually
brightness: brightness, // default (Brightness.light), can be set manually
);
As mentioned in the accepted answer, only setting primaryColor could leave Widgets open to selecting some other default color (various shades of blue) based on one of the other fields above if they are not also set individually, so primarySwatch is a convenient shorthand for simple themes.
In general, however, the colorScheme field is most important as per modern conventions you should be using Theme.of(context).colorScheme.<Color> (though it may not work everywhere yet depending on when you read this).
So, if you need more control over individual theme colors, you could either make do with setting the fields used in ColorScheme.fromSwatch, or just set the primarySwatch (for backwards compatibility of Flutter Widgets that have not yet been migrated), and then set the colorScheme manually for extra control. See also this document for more information…
Swatch is a category. Color is a range in that category although not limited to it. Based on swatch colour you specify flutter can picks a background and a foreground colour it feels suitable for a component.
tldr;
Its important to understand the difference b/w a swatch and a color. A swatch is a category of colour. Its of type MaterialColor Material has below listed swatches plus white. (ignore 50)
.
Each swatch has various ranges. An individual in a swatch/range is a colour although you aren't restricted by it. You can specify any valid colour code even though it's not inside a swatch range.
.
Based on swatch colour you specify flutter can picks a background and a foreground colour it feels suitable for a component.
.
Here is a list of all swatches and their colours. Screenshot taken from https://material.io/design/color/the-color-system.html#tools-for-picking-colors
primarySwatch is MaterialColor.
/// Defines a single color as well a color swatch with ten shades of the color.
///
/// The color's shades are referred to by index. The greater the index, the
/// darker the color. There are 10 valid indices: 50, 100, 200, ..., 900.
/// The value of this color should the same the value of index 500 and [shade500].
/// hex_value1 = 0xFFAAD401; hex_value2 = 0xFFAAD403; ...
MaterialColor myGreen = const MaterialColor(0xFFAAD400,
const {
50 : const Color(hex_value1),
100 : const Color(hex_value2),
200 : const Color(hex_value3),
300 : const Color(hex_value4),
400 : const Color(hex_value5),
500 : const Color(hex_value6),
600 : const Color(hex_value7),
700 : const Color(hex_value8),
800 : const Color(hex_value9),
900 : const Color(hex_value10)});
// use MaterialColor: myGreen.shade100,myGreen.shade500 ...
myGreen.shade50 // Color === 0xFFAAD401