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

Flutter模态底部弹窗带键盘时底部文本上移问题求助

问题描述

开发了一个Flutter页面:顶部AppBar下方有一段文本,屏幕底部有两段文本。进入该页面时,会自动弹出包含TextField的模态底部弹窗,同时系统键盘也会唤起。目前的问题是,弹窗和键盘弹出后,屏幕底部的文本会被遮挡,希望实现底部文本随之上移以保持可见,但未能成功,求解决方案。

解决方案

问题根源在于两个关键设置:一是Scaffold的resizeToAvoidBottomInset被设为false,阻止了页面布局随键盘高度调整;二是模态底部弹窗没有适配键盘高度。以下是具体修改步骤:

  • 移除resizeToAvoidBottomInset: false:该属性会让Scaffold忽略键盘弹出的高度变化,删除后Scaffold会自动调整body布局,让底部内容避开键盘。
  • 给模态弹窗添加键盘高度padding:在弹窗的builder中,通过MediaQuery.of(context).viewInsets.bottom获取键盘高度,将其添加到底部padding中,避免弹窗内容被键盘遮挡。
  • 优化底部文本布局:用Spacer替代Expanded+Align,简化布局逻辑,确保底部文本始终固定在页面底部,同时能随键盘上移。
修改后的完整代码
import 'package:flutter/material.dart';
import 'package:prototype_app/modules/home/domain/entity/coin.dart';

class BuySellDetailsScreen extends StatefulWidget {
  
  const BuySellDetailsScreen({super.key,});

  @override
  _BuySellDetailsScreenState createState() => _BuySellDetailsScreenState();
}

class _BuySellDetailsScreenState extends State<BuySellDetailsScreen> {
  bool _isBottomSheetOpen = false;
  final _textController = TextEditingController();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      displaySheet();
    });
  }

  void displaySheet() {
    showModalBottomSheet<void>(
      context: context,
      isScrollControlled: true,
      builder: (BuildContext context) {
        final keyboardHeight = MediaQuery.of(context).viewInsets.bottom;
        return Padding(
          padding: EdgeInsets.only(bottom: keyboardHeight + 8),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextField(
                controller: _textController,
                decoration: const InputDecoration(
                  hintText: 'Enter text',
                ),
              ),
              ElevatedButton(
                onPressed: () {
                  setState(() {
                    _isBottomSheetOpen = false;
                  });
                  Navigator.pop(context);
                },
                child: const Text('Close'),
              ),
            ],
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    final topText = Container(
      alignment: Alignment.topCenter,
      child: const Text(
        'This is the top text',
        style: TextStyle(fontSize: 20),
      ),
    );

    final bottomTexts = Container(
      padding: const EdgeInsets.symmetric(vertical: 16),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: const [
          Text('Left bottom text'),
          Text('Right bottom text'),
        ],
      ),
    );

    return Scaffold(
      appBar: AppBar(
        title: const Text('My Screen'),
      ),
      body: Column(
        children: [
          topText,
          const Spacer(),
          bottomTexts,
        ],
      ),
    );
  }
}

内容的提问来源于stack exchange,提问作者Inan Mahmud

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 21:21:48