flutter 软键盘弹出导致固定在底部的按钮被顶上来

需要实现的效果:

但是当键盘弹出时

解决方法:将页面内容放在一个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));
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值