Flutter - How to set status bar color when AppBar not present
How to set status bar color when AppBar not present.
I have tried this but not working.
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
return new Scaffold(
body: new Container(
color: UniQueryColors.colorBackground,
child: new ListView.builder(
itemCount: 7,
itemBuilder: (BuildContext context, int index){
if (index == 0){
return addTopInfoSection();
}
},
),
),
);
}
Output look like this:
First, import services
package:
import 'package:flutter/services.dart';
Next, simply put this in the build function of your App:
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.blue, //or set color with: Color(0xFF0000FF)
));
Additionally, you can set useful properties like: statusBarIconBrightness
, systemNavigationBarColor
or systemNavigationBarDividerColor
If you prefer a more flutter/widget way of doing the same thing, consider using the AnnotatedRegion<SystemUiOverlayStyle>
widget.
The value:
property can be set to a SystemUiOverlayStyle()
object containing the same properties as shown above.
For more infos, head over to the API Docs
If you take a look at the source code of AppBar, you can see they use an AnnotatedRegion widget. AnnotedRegion widget gives you more control on System overlay style. This is a more fluttery way to configure the system styles when an app bar is not used.
From my understanding, this widget automatically sets the statusbar/navigationbar color when the widget wrapped in it gets overlaid by the statusbar/navigationbar.
You can wrap your widget like this:
import 'package:flutter/services.dart';
...
Widget build(BuildContext context) {
return Scaffold(
body: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle.light,
child: ...,
),
);
}
For more information about AnnotatedRegion widget head over to the API Docs
As the solution is already mentioned, I am implementing it in a different approach. The approach followed is removing AppBar and changing the color of the status bar using Container widget.
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Test',
home: Scaffold(
primary: true,
appBar: EmptyAppBar(),
body: MyScaffold(),
),
),
);
}
class MyScaffold extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Text(
'Test',
),
);
}
}
class EmptyAppBar extends StatelessWidget implements PreferredSizeWidget {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.black,
);
}
@override
Size get preferredSize => Size(0.0, 0.0);
}
- Here I am using EmptyAppBar class for removing the AppBar which is by default present in Scaffold
- In EmptyAppBar class we can choose the required color in the container widget.
- After that, you have your own custom MyScaffold class for creating your widgets. In my code, I've created a text.
Reference: GitHub Issue
On Android, add the following to onCreate in MainActivity.java, after the call to super.onCreate(savedInstanceState);
getWindow().setStatusBarColor(0x00000000);
or you can use the the flutter_statusbarcolor plugin
changeStatusColor(Color color) async {
try {
await FlutterStatusbarcolor.setStatusBarColor(color);
} on PlatformException catch (e) {
print(e);
}
}
Sample project