需要实现的效果:
但是当键盘弹出时
解决方法:将页面内容放在一个sizebox中,给盒子设置高度为页面高度,当软键盘弹起时,SingleChildScrollView的高度等于设备高度减去键盘的高度,但是sizebox还是之前的设备工作区高度,所以SingleChildScrollView可以滚动显示下方的内容。
double availableHeight = MediaQuery.of(context).size.height -
MediaQuery.of(context).padding.top -
kToolbarHeight;//盒子高度=页面高度-标题高度-状态栏高度
return Scaffold(
resizeToAvoidBottomInset: false,//一定要设置这个,Scaffold就不会自动调整样式将按钮顶上去
backgroundColor: Colors.white, // 设置页面背景颜色
body: Container(
padding: EdgeInsets.all(24.w),
alignment: Alignment.center,
height: availableHeight,
child: SingleChildScrollView(
child: SizedBox(
height: availableHeight - 48.w,//这里再减去Container的上下边距
child: Column(
children: [
//内容
Expanded(child: Container()),//占据页面剩余宽度
//按钮
SizedBox(//按钮位于页面三分之二的位置,所以要在页面下方放一个高度盒子
height: availableHeight / 3,
)
],
),
),
)),
);
封装:(innerScrollBox可以嵌套调用)
double contentHeight = MediaQuery.of(context).size.height -
appBarWidget.preferredSize.height -
MediaQuery.of(context).padding.top -
MediaQuery.of(context).padding.bottom;
Widget innerScrollBox(
{required Widget child,
required double contentHeight,
required Color color}) {
return Container(
alignment: Alignment.topLeft,
height: contentHeight,
margin: EdgeInsets.only(top: 10),
color: color,
child: SingleChildScrollView(child: child));
}