当前位置:   article > 正文

Flutter 安卓端启动页全屏+沉浸式状态栏_flutter全屏背景图 沉浸式

flutter全屏背景图 沉浸式

对酒当歌,人生几何。——曹操 

随着Flutter的兴起,移动端开发又简洁很多啦。不管怎么变,我们还是可以实现我们想要的效果

       开发中常见的就是欢迎页启动要全屏,首页需要显示为沉浸式状态栏。效果大致如下:

 有原生开发基础的同时,我们需要UI设计给我们设计低、中、高、超高、超超高分辨率的图片放置在创建工程文件的不同目录下;分别对应(drawable、drawable-hdpi、drawable-xhdpi、drawable-xxhdpi、drawable-xxxhdpi)

1、设置启动页样式,防止启动页黑屏:

  1. <activity
  2. android:name=".SplashActivity"
  3. android:hardwareAccelerated="true"
  4. android:launchMode="singleTop"
  5. android:theme="@style/LaunchTheme"
  6. android:windowSoftInputMode="adjustResize">
  7. <meta-data
  8. android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
  9. android:value="true" />
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>

2、设置MainActivity页样式,防止导航到该页面黑屏:

 

  1. <activity
  2. android:name=".MainActivity"
  3. android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
  4. android:hardwareAccelerated="true"
  5. android:launchMode="singleTop"
  6. android:theme="@style/LaunchThemeMain"
  7. android:windowSoftInputMode="adjustResize">
  8. <meta-data
  9. android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
  10. android:value="true" />
  11. </activity>

3、样式:

适配安卓5.0以下样式放置在values文件夹下

适配安卓5.0-9.0的样式放置在values-v21文件夹下

适配安卓9.0以上的样式放置在values-v28文件夹下

values文件夹下styles.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <!--SplashActivity样式-->
  4. <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
  5. <!-- Show a splash screen on the activity. Automatically removed when
  6. Flutter draws its first frame -->
  7. <item name="android:windowBackground">@drawable/launch_background</item>
  8. <item name="android:windowFullscreen">true</item>//全屏即无通知栏
  9. </style>
  10. <!--MainActivity样式-->
  11. <style name="LaunchThemeMain" parent="@android:style/Theme.Black.NoTitleBar">
  12. <!-- Show a splash screen on the activity. Automatically removed when
  13. Flutter draws its first frame -->
  14. <item name="android:windowBackground">@drawable/launch_background</item>
  15. <item name="android:windowFullscreen">false</item>//全屏即无通知栏
  16. </style>
  17. </resources>

values-v21文件夹styles.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <!--SplashActivity样式-->
  4. <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
  5. <!-- Show a splash screen on the activity. Automatically removed when
  6. Flutter draws its first frame -->
  7. <item name="android:windowBackground">@drawable/launch_background</item>
  8. <item name="android:windowDrawsSystemBarBackgrounds">false</item>
  9. <item name="android:windowFullscreen">true</item>//全屏即无通知栏
  10. </style>
  11. <!--MainActivity样式-->
  12. <style name="LaunchThemeMain" parent="@android:style/Theme.Black.NoTitleBar">
  13. <!-- Show a splash screen on the activity. Automatically removed when
  14. Flutter draws its first frame -->
  15. <item name="android:windowBackground">@drawable/launch_background</item>
  16. <item name="android:windowDrawsSystemBarBackgrounds">false</item>
  17. <item name="android:windowFullscreen">false</item>//全屏即无通知栏
  18. </style>
  19. </resources>

values-v28文件夹下styles.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <!--SplashActivity样式-->
  4. <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
  5. <!-- Show a splash screen on the activity. Automatically removed when
  6. Flutter draws its first frame -->
  7. <item name="android:windowBackground">@drawable/launch_background</item>
  8. <!--<item name="android:windowFullscreen">true</item>-->
  9. <!--不让windowBackground延申到navigation bar区域-->
  10. <item name="android:windowDrawsSystemBarBackgrounds">false</item>
  11. <!--适配Android P刘海屏-->
  12. <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
  13. <item name="android:windowFullscreen">true</item>//全屏即无通知栏
  14. </style>
  15. <!--MainActivity样式-->
  16. <style name="LaunchThemeMain" parent="@android:style/Theme.Black.NoTitleBar">
  17. <!-- Show a splash screen on the activity. Automatically removed when
  18. Flutter draws its first frame -->
  19. <item name="android:windowBackground">@drawable/launch_background</item>
  20. <!--<item name="android:windowFullscreen">true</item>-->
  21. <!--不让windowBackground延申到navigation bar区域-->
  22. <item name="android:windowDrawsSystemBarBackgrounds">false</item>
  23. <!--适配Android P刘海屏-->
  24. <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
  25. <item name="android:windowFullscreen">false</item>//全屏即无通知栏
  26. </style>
  27. </resources>

4、导航到Flutter MyApp后希望能看到沉浸式状态栏, MyApp继承自StatelessWidget(是不可变的,所有的值是最终的)。

所以选择沉浸式属性的初始化在StatelessWidget中build 函数下。

  1. if (Platform.isAndroid) {
  2. SystemUiOverlayStyle systemUiOverlayStyle =
  3. SystemUiOverlayStyle(statusBarColor: Colors.transparent);
  4. SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  5. }

下载资源

参考:

支持不同像素密度:https://developer.android.google.cn/training/multiscreen/screendensities#java

SystemChrome:https://www.jianshu.com/p/540dccbd5a51

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/126322?site
推荐阅读
相关标签
  

闽ICP备14008679号