How to change status and navigation bar color in Flutter?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 09:18:28

You can use SystemChrome class to change Status bar and Navigation bar color. First import

import 'package:flutter/services.dart';

After this, you need to add following lines (better place to put these lines is in your main() method)

void main() {
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
    systemNavigationBarColor: Colors.blue,
    statusBarColor: Colors.pink,
  ));
}

If you don't want AppBar at all, then you can just call setSystemUIOverlayStyle in the main function:

void main() async {
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);

  runApp(new MaterialApp(
    home: new Scaffold(),
  ));
}

It's more tricky if you have an app bar in one scaffold, and none in another. In that case I had to call setSystemUIOverlayStyle after pushing new route with a scaffold that does not have an appbar:

@override
Widget build(BuildContext context) {
  final page = ModalRoute.of(context);
  page.didPush().then((x) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
  });

  return new Scaffold();
}

I'm quite new to StackOverflow & I've never used Flutter however I have found this package that seems to make things relatively easy.

Method 1: Using the package

Once this is imported all you need to do is add this code fragment:

try {
await FlutterStatusbarcolor.setStatusBarColor(Colors.black);
} on PlatformException catch (e) {
print(e);
}

Replacing the parameter for the setStatusBarColor() should give you the desired result, a full list of colours can be found here.

Method 2: Using default functions

If this doesn't work / you don't want to add extra packages or libraries then perhaps this StackOverflow answer may help.

It involves using a similar function to the above method: getWindow().setStatusBarColor() or getActivity().getWindow().setStatusBarColor()

Replacing the parameter with the desired hex code from the same list as earlier may also result in a solution.

Hope it works/helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!