I'd like to change the background color of the status bar and app bar in Android, but I don't know how to do it.
Below is my sample code and screen capture.
Code:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarColor: Colors.black,
),
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
),
body: Center(),
),
);
}
}
Update
I try to using SystemChrome.setSystemUIOverlayStyle();
First page can change the background color of the status bar and app bar, but second page does not change the background color of status bar.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
routes: <String, WidgetBuilder>{
'Page 1': (BuildContext context) => MyHomePage(),
'Page 2': (BuildContext context) => MyHomePage2(),
},
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.black,
));
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
),
body: Center(
child: MaterialButton(
onPressed: () {
Navigator.pushNamed(context, 'Page 2');
},
child: Text('Next Page'),
),
),
);
}
}
class MyHomePage2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.white,
));
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.yellow,
),
body: Center(),
);
}
}