How to change Status Bar and App Bar color in Flutter?

后端 未结 6 2058
后悔当初
后悔当初 2020-12-14 17:16

I\'m trying to change the color of system status bar to black. The configuration seems to be overridden by the AppBar class. I can achieve what I want by assigning theme: to

6条回答
  •  忘掉有多难
    2020-12-14 17:24

    I tried the method SystemChrome.setSystemUIOverlayStyle(), as far as I tested (Flutter SDK v1.9.1+hotfix.2, running on iOS 12.1) it works perfect for Android. But for iOS, e.g. if your first screen FirstScreen() doesn't have an AppBar, but the second SecondScreen() does, then at launch the method does set the color in FirstScreen(). However, after navigating back to FirstScreen() from SecondScreen(), the status bar color becomes transparent.

    I come up with a hacky workaround by setting an AppBar() with zero height, then status bar's color gets changed by the AppBar, but the AppBar itself is not visible. Hope it would be useful to someone.

    // FirstScreen that doesn't need an AppBar
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: PreferredSize(
            preferredSize: Size.fromHeight(0),
            child: AppBar( // Here we create one to set status bar color
              backgroundColor: Colors.black, // Set any color of status bar you want; or it defaults to your theme's primary color
            )
          )
      );
    }
    
    // SecondScreen that does have an AppBar
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar()
      }
    }
    

    Here is the screenshot of FirstScreen in iPhone Xs Max iOS 12.1:

提交回复
热议问题