Flutter - How to set status bar color when AppBar not present

后端 未结 17 740
孤城傲影
孤城傲影 2020-12-07 23:56

How to set status bar color when AppBar not present.

I have tried this but not working.

Widget build(BuildContext context) {
    SystemChrome.setSys         


        
17条回答
  •  失恋的感觉
    2020-12-08 00:48

    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

提交回复
热议问题