如何让Flutter的persistentFooterButtons在TextField选中时移至键盘上方
persistentFooterButtons的定位逻辑是固定在屏幕底部,不会自动响应键盘弹出事件,所以会被键盘挡住。你可以用以下两种方案解决:
方案一:替换为body内的底部按钮(推荐)
把按钮从persistentFooterButtons移到body里,用Column+Expanded+SingleChildScrollView组合,让body内容可滚动,键盘弹出时自动把按钮顶上去:
Scaffold( body: SingleChildScrollView( child: Column( children: [ // 你的页面内容,比如TextField等 const TextField(hintText: "输入内容"), // 用Expanded把按钮推到底部 Expanded(child: Container()), ElevatedButton( style: ElevatedButton.styleFrom( enableFeedback: true, minimumSize: const Size(double.infinity, 45)), onPressed: !userInteracts() ? null : () { _submit(); }, child: const Text('Continue') ), ], ), ), );
方案二:用MediaQuery动态调整按钮位置
如果一定要保留persistentFooterButtons,可以结合MediaQuery获取键盘高度,手动调整按钮的底部内边距:
Scaffold( body: // 你的页面内容, persistentFooterButtons: [ Container( padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom), width: double.infinity, child: ElevatedButton( style: ElevatedButton.styleFrom( enableFeedback: true, minimumSize: const Size(double.infinity, 45)), onPressed: !userInteracts() ? null : () { _submit(); }, child: const Text('Continue') ), ), ], );
注意:此方案需要确保Scaffold的resizeToAvoidBottomInset属性保持默认的true,否则无法获取正确的键盘高度。
内容的提问来源于stack exchange,提问作者Boss Nass
相关产品推荐
相关产品推荐

