可以使用 MediaQuery.of(context).padding.top 来获取当前屏幕的顶部内边距大小,再根据 AppBar 的高度来计算出需要设置的内边距大小。
示例代码:
class MyPage extends StatefulWidget {
@override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State {
double appBarHeight = 80;
EdgeInsetsGeometry contentPadding = EdgeInsets.zero;
@override
Widget build(BuildContext context) {
contentPadding = EdgeInsets.only(top: MediaQuery.of(context).padding.top + appBarHeight);
return Scaffold(
appBar: AppBar(
title: Text('My Page'),
centerTitle: true,
elevation: 0,
backgroundColor: Colors.blueAccent,
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {},
),
actions: [
IconButton(
icon: Icon(Icons.search),
onPressed: () {},
),
IconButton(
icon: Icon(Icons.menu),
onPressed: () {},
),
],
),
body: Container(
padding: contentPadding,
child: SingleChildScrollView(
child: Column(
children: [
SizedBox(
height: 100,
child: Placeholder(),
),
SizedBox(height: 20),
SizedBox(
height: 100,
child: Placeholder(),
),
SizedBox(height: 20),
SizedBox(
height: 100,
child: Placeholder(),
),
SizedBox(height: 20),
SizedBox(
height: 100,
child: Placeholder(),
),
SizedBox(height: 20),
SizedBox(
height: 100,
child: Placeholder(),
),
SizedBox(height: 20),
SizedBox(
height: 100,
child: Placeholder(),
),
SizedBox(height: 20),
SizedBox(
height: 100,
child: Placeholder(),
),
],
),
),
),
);
}
}