Flutter中ReactiveRawAutocomplete被键盘遮挡问题求助
解决方案
1. 恢复Scaffold的自动布局调整
首先将RecommandeScaffold的resizeToAvoidBottomInset设为true,这是解决键盘遮挡的核心前提:
return RecommandeScaffold( resizeToAvoidBottomInset: true, // 改为true // 其余参数保持不变 ... );
该属性会让Scaffold在键盘弹出时自动调整body区域布局,避免内容被遮挡。你之前设置为false,相当于关闭了系统自带的键盘避让逻辑,这是问题的主要诱因。
2. 优化ReactiveRawAutocomplete的选项弹窗布局
若恢复上述设置后仍存在遮挡,可修改optionsViewBuilder的布局逻辑,让弹窗主动避开键盘:
optionsViewBuilder: (BuildContext newContext, AutocompleteOnSelected<String> onSelected, Iterable<String> options) { // 获取键盘弹出时的底部留白高度 final bottomInset = MediaQuery.of(newContext).viewInsets.bottom; return Positioned( left: 20, // 与输入框左padding对齐 right: 20, // 与输入框右padding对齐 // 计算弹窗顶部位置,确保不会被键盘遮挡 top: MediaQuery.of(newContext).size.height - bottomInset - 200, child: Material( elevation: 4.0, child: SizedBox( height: 200.0, child: ListView.builder( padding: const EdgeInsets.all(8.0), itemCount: options.length, itemBuilder: (BuildContext listContext, int index) { final option = options.elementAt(index); return GestureDetector( onTap: () { onSelected(option); }, child: ListTile( title: Text(option), ), ); }, ), ), ), ); },
这种方式通过Positioned结合键盘高度动态调整弹窗位置,比原有的Align布局更适配键盘弹出场景。
3. 确保SingleChildScrollView正常工作
确认SingleChildScrollView的父容器没有固定高度限制,恢复resizeToAvoidBottomInset: true后,它会随键盘上移带动表单滚动,让输入框始终处于可见区域。无需额外设置ClampingScrollPhysics(),默认的滚动物理效果已能满足需求。
内容的提问来源于stack exchange,提问作者Brahim CHEBBI
相关产品推荐
相关产品推荐

