//获取屏幕宽高
// 保存屏幕的基础尺寸(不受键盘影响)
late Size _screenSize;
void didChangeDependencies() {
super.didChangeDependencies();
// 获取屏幕的基础尺寸,这个尺寸不会因键盘弹出而改变
_screenSize = MediaQuery.of(context).size;
}
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
// 1. 背景图片层 - 固定尺寸,不受键盘影响
Positioned(
left: 0,
top: 0,
width: _screenSize.width,
height: _screenSize.height,
child: Image.asset(
'assets/images/chat_bg.jpg',
fit: BoxFit.cover,
width: _screenSize.width,
height: _screenSize.height,
),
),
// 2. 聊天内容层 - 响应键盘变化
SizedBox(
width: double.infinity,
height: double.infinity,
child: Column(
children: [
// 可滚动的消息列表(会跟随键盘上移)
Expanded(
child: ListView.builder(
reverse: true, // 新消息在底部
padding: const EdgeInsets.all(16),
itemCount: _messages.length,
itemBuilder: (context, index) {
return Container(
margin: const EdgeInsets.only(bottom: 8),
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.8),
borderRadius: BorderRadius.circular(8),
),
child: Text(_messages[index]),
);
},
),
),
// 输入框区域
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
child: Row(
children: [
Expanded(
child: TextField(
controller: _controller,
decoration: InputDecoration(
hintText: '请输入消息...',
filled: true,
fillColor: Colors.white.withOpacity(0.8),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(24),
borderSide: BorderSide.none,
),
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 12,
),
),
),
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
padding: const EdgeInsets.all(12),
),
child: const Icon(Icons.send),
),
],
),
),
],
),
),
],
),
);
}
【flutter 列表界面当软键盘弹出时挤压背景图】
最新推荐文章于 2026-06-23 23:00:29 发布

553

被折叠的 条评论
为什么被折叠?



