Flutter適配安卓劉海、水滴屏顯示全屏

旺仔小小饅頭發表於2021-07-25
  • 配置Android

找到android/app/src/main/res/values目錄,開啟styles.xml 將shortEdges放到style標籤內。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Theme applied to the Android Window while the process is starting -->
    <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <!-- Show a splash screen on the activity. Automatically removed when
             Flutter draws its first frame -->
        <item name="android:windowBackground">@drawable/launch_background</item>
        /// 放這裡
        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    </style>
    <!-- Theme applied to the Android Window as soon as the process has started.
         This theme determines the color of the Android Window while your
         Flutter UI initializes, as well as behind your Flutter UI while its
         running.
         
         This Theme is only used starting with V2 of Flutter's Android embedding. -->
    <style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowBackground">@android:color/white</item>
        /// 放這裡
        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    </style>
</resources>
複製程式碼

LaunchTheme指的是啟動頁的主題,也就是我們常說的Splah頁面。如果需要啟動頁全屏就放在裡面。 NormalTheme代表正常頁面的主題。 根據需求,我們可以放在2個不同的地方。

  • Flutter中設定SystemChrome.setEnabledSystemUIOverlays([]),隱藏狀態列和底部導航欄
@override
initState() {
	  /// 初始化時隱藏
      SystemChrome.setEnabledSystemUIOverlays([]);
      super.initState();
}

@override
void dispose() { 
	/// 頁面關閉時恢復正常設定
  	SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
    super.dispose();
}
複製程式碼

相關文章