简体   繁体   English

Flutter Android和IOS透明状态和导航栏

[英]Transparent status and navigation bar in Flutter for both Android and IOS

I am making my first Flutter app and I have a question.我正在制作我的第一个 Flutter 应用程序,我有一个问题。 I want to make the status bar and navigation bar transparent on both Android and IOS. This is what I have now: https://i.stack.imgur.com/h3DJ3.png我想让Android和IOS的状态栏和导航栏都透明。这就是我现在拥有的: https://i.stack.imgur.com/h3DJ3.png

And this is what I want: https://i.stack.imgur.com/kKrec.png这就是我想要的: https://i.stack.imgur.com/kKrec.png

I am a beginner in Flutter so if you have a solution, please make it simple.我是Flutter的初学者所以如果你有解决方案,请简单点。 Thanks in advance!提前致谢!

In iOS you can not change color of the status bar according to their guidelines, though it is already transparent in iOS. For android, use it in the build method of your root widget:在 iOS 中,您不能根据他们的指南更改状态栏的颜色,尽管它在 iOS 中已经是透明的。对于 android,请在根小部件的构建方法中使用它:

if (Theme.of(context).platform == TargetPlatform.android) {
      SystemChrome.setSystemUIOverlayStyle(
          SystemUiOverlayStyle(statusBarColor: Colors.transparent));
    }

More details:更多细节:

 import 'package:flutter/foundation.dart' show defaultTargetPlatform;
 Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Permission.camera.request();

  runApp(MyApp());
}


    class MyApp extends StatefulWidget {
      @override
      State<MyApp> createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      @override
      void initState() {
        SchedulerBinding.instance?.addPostFrameCallback((timeStamp) {
          if (defaultTargetPlatform == TargetPlatform.android) {
            SystemChrome.setSystemUIOverlayStyle(
                SystemUiOverlayStyle(statusBarColor: Colors.transparent));
          }
        });
        super.initState();
      }
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: MyHomePage(),
        );
      }
    }

You can set color or backgroundColor parameter to Colors.transparent as well.您也可以将colorbackgroundColor颜色参数设置为Colors.transparent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM