獲取狀態欄高度:
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), ), ), ); }