Related
I am new to flutter and want to set my apple status bar to a dark theme (That the text is white). In my main I import a separate file with the themes. This is what I got now in themes.dart:
AppThemeData _theme = AppThemeData(
theme: ThemeData(
brightness: Brightness.dark, //This gives the error "Unhandled Exception: Bad state: No element"
cardTheme: CardTheme(
elevation: 1,
),
primarySwatch: Colors.green,
backgroundColor: Colors.grey[400],
visualDensity: VisualDensity.adaptivePlatformDensity,
),
mainScreenBackgroundGradientColors: [Colors.green[700], Colors.green[300]],
chartThemeData: ChartThemeData(
lineColor: Colors.yellow[600],
lineAreaColor: Colors.yellow[100],
loadingColor: Colors.yellow,
),
deviceChipColor: Colors.green[300],
infoIconColor: Colors.grey[600]);
I came across this Comment and tried that, but that didn't work because context did not exists.
All help is appreciated.
This is what worked for me in the end:
ThemeData(
appBarTheme: AppBarTheme(brightness: Brightness.dark),
),
I tried several ways to make status bar icons dark, but after home press and returning to app status bar icons are white!
it seems that its flutter bug.
but in iOS it works fine.
i tried these ways :
android app style:
<item name="android:windowLightStatusBar">false</item>
AppBar brightness:
brightness: Brightness.dark
Flutter API:
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark.copyWith(
statusBarIconBrightness: Brightness.dark
));
flutter_statusbarcolor package :
import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';
FlutterStatusbarcolor.setStatusBarWhiteForeground(false);
Add following into your widget tree:
AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle.dark,
child: ...
)
As of Flutter 2.4.*, AppBar brightness is deprecated. To achieve status bar brightness, add a systemOverlayStyle to your AppBar as I have highlighted below.
appBar: AppBar(
systemOverlayStyle: SystemUiOverlayStyle(
statusBarBrightness: Brightness.dark
),
)
If you wish to apply dark status bar icons on the entire app, add an appBarTheme to your MaterialApp theme as shown in the following code. Your MaterialApp widget is on the main.dart file;
MaterialApp(
...
theme: ThemeData(
appBarTheme: AppBarTheme(
systemOverlayStyle: SystemUiOverlayStyle(
statusBarBrightness: Brightness.dark
),
),
)
)
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.white
));
This is only working when there is no appbar. If there is an app bar, this will not work. So you will have to change the brightness of appbar first.
appBar: new AppBar(
brightness: Brightness.light, // Add this line
),
If you want to change the theme for the app as a whole, I would recommend this answer to a similar problem: https://stackoverflow.com/a/62607827/15605135
If you want to change a single AppBar, you could try to use the systemOverlayStyle property:
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
systemOverlayStyle: SystemUiOverlayStyle.dark,
),
);
}
Wrap your widget with SafeArea and include brightness in your appBar and it will work.
void main()
{
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(statusBarColor: Colors.deepPurple,
statusBarBrightness: Brightness.dark,
));
runApp(MyApp());
}
return SafeArea(child: Scaffold(
appBar: AppBar(
toolbarHeight: 50,
centerTitle: true,
brightness: Brightness.dark,
backgroundColor: Colors.deepPurple,
child: Text("Your Text"),),
Use:-
systemOverlayStyle: SystemUiOverlayStyle.dark,
instead of:
brightness: Brightness.dark,
Apply for all appBar use
return MaterialApp(
theme: ThemeData(
appBarTheme: Theme.of(context)
.appBarTheme
.copyWith(systemOverlayStyle: SystemUiOverlayStyle.dark)),
debugShowCheckedModeBanner: false,
home: widget());
I hope it works.
I am trying to set a common theme for app so I need to change appbar color as a color that indicates hex code #0f0a1a
const MaterialColor toolbarColor = const MaterialColor(
0xFF151026, const <int, Color>{0: const Color(0xFF151026)});
I try this piece of code to make a custom color but fails.
How can I do this from themeData?
Declare your Color:
const primaryColor = Color(0xFF151026);
In the MaterialApp level (will change the AppBar Color in the whole app ) change primaryColor
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: primaryColor,
),
home: MyApp(),
);
and if you want to change it on the Widget level modify the backgroundColor
appBar: AppBar(
backgroundColor: primaryColor,
),
If you don't want to change the whole PrimaryColor you can also define AppBarTheme in your ThemeData:
MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
appBarTheme: AppBarTheme(
color: const Color(0xFF151026),
)),
home: myApp(),
)
In the current version of Flutter, to comply with the new "Material You" design, we should try to use ColorScheme whenever possible. The app bar color is controlled by:
If theme brightness is light, use primary color.
If theme brightness is dark, use surface color.
For examples:
Light Mode
Set brightness to light, then set primary and onPrimary to yellow and black, and set all other colors to grey to showcase that they are not relevant to AppBar:
MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme(
brightness: Brightness.light,
primary: Colors.yellow,
onPrimary: Colors.black,
// Colors that are not relevant to AppBar in LIGHT mode:
primaryVariant: Colors.grey,
secondary: Colors.grey,
secondaryVariant: Colors.grey,
onSecondary: Colors.grey,
background: Colors.grey,
onBackground: Colors.grey,
surface: Colors.grey,
onSurface: Colors.grey,
error: Colors.grey,
onError: Colors.grey,
),
),
home: Scaffold(
appBar: AppBar(title: Text("Light Mode Demo")),
),
)
Dark Mode
Set brightness to dark, then set surface and onSurface to yellow and black, all others to grey to showcase that they are not relevant to AppBar.
MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme(
brightness: Brightness.dark,
surface: Colors.yellow,
onSurface: Colors.black,
// Colors that are not relevant to AppBar in DARK mode:
primary: Colors.grey,
onPrimary: Colors.grey,
primaryVariant: Colors.grey,
secondary: Colors.grey,
secondaryVariant: Colors.grey,
onSecondary: Colors.grey,
background: Colors.grey,
onBackground: Colors.grey,
error: Colors.grey,
onError: Colors.grey,
),
),
home: Scaffold(
appBar: AppBar(title: Text("Dark Mode Demo")),
),
)
include backgroundColor: to appbar
appBar: AppBar(
title: const Text('Example'),
backgroundColor: Colors.black,
),
With the new Material 3 and Flutter 3 updates, background color for AppBar can be changed using surfaceTintColor.
Either inside the AppBar like this:
return AppBar(
...
surfaceTintColor: backgroundColor ?? CommonColors.lightColor,
);
Or inside the ThemeData class like this:
ThemeData.light().copyWith(
...
appBarTheme: AppBarTheme(
backgroundColor: CommonColors.lightColor,
surfaceTintColor: CommonColors.lightColor,
actionsIconTheme: const IconThemeData(color: Colors.white),
),
),
According to AppBar description On Flutter 2.5, it uses ColorScheme.primary by default.
The default app bar [backgroundColor] is the overall theme's
[ColorScheme.primary] if the overall theme's brightness is [Brightness.light]. Unfortunately this is the same as the default
[ButtonStyle.foregroundColor] for [TextButton] for light themes.
In this case a preferable text button foreground color is
[ColorScheme.onPrimary], a color that contrasts nicely with
[ColorScheme.primary]. to remedy the problem, override
[TextButton.style]:
try using colorScheme
MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSwatch(
primarySwatch: const Color(0xFF151026),
),
),
home: MyApp(),
),
And to use somewhere else
Theme.of(context).colorScheme.primary,
Or you can just call backgroundColor on Appbar.
For more, visit ThemeData-class
To change Appbar backgroundColor in the whole app:
MaterialApp(theme: ThemeData(appBarTheme: AppBarTheme(backgroundColor: Colors.blueGrey),),);
You can use Flutter ThemeData,if you want to set a theme for your entire application:
class HomePage extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'MyApp',),
);
}
}....
Then if you want to change certain elements of your primary and secondary colors,you can achieve this using the colorScheme from Swatch.
Learn More Here
Here is an example using colorScheme :
class HomePage extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
theme: ThemeData(
colorScheme: ColorScheme.fromSwatch(
primarySwatch: Colors.blue,//the color of your Appbar will be blue
).copyWith(
secondary: Colors.green,
//your accent color-floating action will appear green
),
),
home: MyHomePage(title: 'MyApp',),
);
Since flutter 2.5+, the working solution will be simply use ColorScheme in ThemeData:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
#override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: const AppBarWidget(),
theme: ThemeData.light().copyWith(
colorScheme: ColorScheme.fromSwatch().copyWith(
// change the appbar color
primary: Colors.green[800],
),
),
);
}
}
class AppBarWidget extends StatelessWidget {
const AppBarWidget({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('AppBar'),
),
);
}
}
If you are using material 3. you have to take care of tint and forground color also.
Scaffold(
appBar: AppBar(
backgroundColor: Colors.white //your color,
surfaceTintColor: Colors.white // for material 3 you have to make it transparent,
)
For the background color, you can use backgroundColor property, for the text color you can apply a style.
Example:
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Jorgesys', style: TextStyle(color: Colors.cyanAccent),),
backgroundColor: Colors.green, //App Bar background color.
),
body: Column(
...
...
I'm new to Flutter and was trying to change the child icon color of FloatingActionButton. Child icon color is white by default.
How can i change that??
Given below is the code that i have worked on.
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new Icon(Icons.add),
backgroundColor: Colors.yellow,
), // This trailing comma makes auto-formatting nicer for build methods.
);
Thank you.
You can wrap in an IconTheme
child: new IconTheme(
data: new IconThemeData(
color: Colors.yellow),
child: new Icon(Icons.add),
),
or a Theme where iconTheme is set the way you want it
(not tested)
To change the color of child Icon, You have to set the color in the Icon() widget.
Here I am sharing the code snippet where I have set the red color.
floatingActionButton: FloatingActionButton(
tooltip: 'Increment',
child: new Icon(Icons.add, color: Colors.red,),
backgroundColor: Colors.yellow,
),
Flutter v17.4
This worked for me, if you just want to change to colour of icon.
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add,
color: Colors.black, //The color which you want set.
),
onPressed: () => {},
),
MaterialApp theme: ThemeData
If using ThemeData in your MaterialApp to color your app like:
return MaterialApp(
title: 'Flutter Demo',
theme: lightThemeData, // this guy
darkTheme: darkThemeData, // and perhaps this guy
home: MyHomePage()
);
... you have 2 ways to color your FloatingActionButton (FAB) Icon color in ThemeData:
floatingActionButtonTheme: const FloatingActionButtonThemeData(foregroundColor: myColor)
onSecondary: myColor (used only if ↑↑↑ is missing)
final lightThemeData = ThemeData.from(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
brightness: Brightness.light,
primary: blue,
secondary: blue,
onSecondary: white, // FAB Icon color if FABTheme not provided like below
),
).copyWith(
floatingActionButtonTheme: const FloatingActionButtonThemeData(foregroundColor: Colors.green), // this guy works
iconTheme: const IconThemeData(color: white), // DOES NOT affect FAB Icon
accentIconTheme: const IconThemeData(color: purpleRain), // argumentFormerlyKnownAsFabIconColor - deprecated, DON'T USE THIS
);
In the above themeData example, the FAB Icon would be colored green.
Because onSecondary is used to color the FAB icon only if floatingActionButtonTheme is missing.
If neither of the above theme colors are provided, the FAB Icon will fallback to black color.
iconTheme and accentIconTheme both have no effect on FAB icon color.
FAB accentIconTheme change happened in Flutter 1.17.
When the user scrolls to the edge of a list or TabView, an animated blue circle appears on the ending edge.
What is this called, and how do I change the colour of it?
This is the android scroll physics (ClampingScrollPhysics).
From the source code and docs:
glow color is specified to use [ThemeData.accentColor].
That been said, when you create your App, the best practice is to specify a custom theme, istead of appling colors manually.
Example:
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
theme: ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.grey,
primaryColor: Colors.grey[50],
primaryColorBrightness: Brightness.light,
//this is what you want
accentColor: Colors.orangeAccent[400],
accentColorBrightness: Brightness.light,
),
home: Home(),
);
}
}
I like to use this tool to define the primary and secondary (called accent color in flutter) and having a preview of the widgets.
Note: On IOs the physics is different, letting the user scroll beyond the bounds of the content, but then bounce the content back to the edge of those bounds (BouncingScrollPhysics).
Since Flutter v2.3.0-0.1.pre, themes changed
https://api.flutter.dev/flutter/material/ThemeData/accentColor.html
For setting the accent color, you must do something like
class Themes {
static final light = ThemeData(
// Colors
backgroundColor: Colors.white,
primaryColor: MyColors.blue,
splashColor: MyColors.blue,
cardColor: MyColors.blue,
hoverColor: MyColors.blue,
highlightColor: MyColors.blue,
colorScheme: ColorScheme.light(primary: MyColors.blue),
// Fonts
fontFamily: 'Inter');
static final lightV2 = light.copyWith(
colorScheme: light.colorScheme.copyWith(secondary: MyColors.blue));
}
And assign ligthV2 in your MaterialApp
MaterialApp(
...
theme: Themes.lightV2
...
)