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));
  }

在层级组件Stack中,当键盘TextField的输入框获取焦点时,由于软键盘弹出,可能会导致内容被顶上去的情况。解决这个问题的方法很简单,只需要在Stack组件中添加一个属性resizeToAvoidBottomInset并设置为false即可。这样做可以防止键盘弹出时内容被顶上去。例如: ```dart Stack( resizeToAvoidBottomInset: false, children: \[ // 其他组件 TextField(), // 其他组件 \], ) ``` 通过设置resizeToAvoidBottomInset为false,可以确保键盘弹出时不会影响到Stack中的其他组件的位置。这样就解决了软键盘弹出时提高ListView的问题。\[1\] #### 引用[.reference_title] - *1* *3* [【Flutter 问题系列第 16 篇】如何防止软键盘弹出后顶掉内容](https://blog.csdn.net/qq_42351033/article/details/116091208)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [正确使用Flutter的Listview](https://blog.csdn.net/qq_41628942/article/details/129127387)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值