You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter输入时如何让BottomSheet显示在键盘上方

解决BottomSheet被键盘覆盖的问题

要让BottomSheet在弹出键盘时自动上移并支持滚动,只需做两处修改:

1. 开启BottomSheet的滚动控制

调用showModalBottomSheet时,添加isScrollControlled: true参数,让BottomSheet能根据键盘高度自适应调整尺寸:

floatingActionButton: FloatingActionButton(
  onPressed: (){
    showModalBottomSheet<void>(
      context: context,
      isScrollControlled: true, // 新增该参数
      builder: (context){
        return EntryForm();
      });
  },
  child: Icon(Icons.add),
),

2. 给滚动视图添加键盘内边距

在EntryForm的SingleChildScrollView中,通过MediaQuery获取键盘高度并设置底部内边距,确保输入框不被遮挡,同时支持内容滚动:

class _EntryFormState extends State<EntryForm> {
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      padding: EdgeInsets.only(
        bottom: MediaQuery.of(context).viewInsets.bottom, // 新增底部内边距
      ),
      child: Column(
        children: [
          TextField(),
          TextField(),
          ElevatedButton(onPressed: (){}, child: Text('Save Record'))
        ],
      ),
    );
  }
}

修改完成后,点击TextField唤起键盘时,BottomSheet会自动上移适配键盘高度,内容也能通过滚动查看完整区域。

内容的提问来源于stack exchange,提问作者Irfan Ganatra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 05:50:34