flutter獲取狀態欄高度及安全區域


獲取狀態欄高度:

final double statusBarHeight = MediaQuery.of(context).padding.top;

所謂安全區域,就是適配現在一些劉海屏之類的非常規顯示屏,在flutter中除了根據上面的方法獲取到狀態欄高度,給頁面加對應的狀態欄高度padding,還有一個專門的widget用來顯示安全區域內容:SafeArea

下面是對比圖:

 1 Widget build(BuildContext context) {
 2   return Material(
 3     color: Colors.blue,
 4     child: SafeArea(
 5       child: SizedBox.expand(
 6         child: Card(color: Colors.yellowAccent),
 7       ),
 8     ),
 9   );
10 }

該widget可以設置四個方向是否啟用安全區,例如不對底部設置安全區域,可以設置為:

Widget build(BuildContext context) {
  return Material(
    color: Colors.blue,
   bottom: false, child: SafeArea( child: SizedBox.expand( child: Card(color: Colors.yellowAccent), ), ), ); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM