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

如何在Flutter的AppBar下方实现点击Home文本弹出下拉窗口?

实现AppBar中Home文本的下拉弹窗功能

要实现点击Home文本弹出下拉窗口,你可以用Flutter的PopupMenuButton组件改造你的AppBar标题区域,具体修改如下:

修改后的完整代码

appBar: AppBar(
  toolbarHeight: 100,
  automaticallyImplyLeading: false,
  elevation: 10,
  backgroundColor: Colors.white,
  title: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      const SizedBox(height:30),
      // 用PopupMenuButton包裹原有的Row,实现点击触发下拉
      PopupMenuButton<String>(
        // 自定义下拉按钮的样式,保留原来的Home图标+文本+下拉箭头
        child: Row(
          mainAxisSize: MainAxisSize.min, // 避免Row占满整个宽度
          children: [
            Icon(Icons.home_rounded, color: HexColor('#24591C')),
            const SizedBox(width: 8), // 增加图标和文本的间距,优化视觉
            const Text(
              'Home',
              style: TextStyle(
                  color: Colors.black,
                  fontSize: 16,
                  fontWeight: FontWeight.w700
              ),
            ),
            Icon(Icons.arrow_drop_down_rounded, size: 30, color: HexColor('#444444')),
          ],
        ),
        // 定义下拉菜单的选项内容
        itemBuilder: (context) => [
          const PopupMenuItem(
            value: 'option1',
            child: Text('选项1'),
          ),
          const PopupMenuItem(
            value: 'option2',
            child: Text('选项2'),
          ),
          const PopupMenuItem(
            value: 'option3',
            child: Text('选项3'),
          ),
        ],
        // 处理菜单选中后的逻辑(可选)
        onSelected: (value) {
          switch(value) {
            case 'option1':
              // 执行选项1对应的操作
              break;
            case 'option2':
              // 执行选项2对应的操作
              break;
            case 'option3':
              // 执行选项3对应的操作
              break;
          }
        },
      ),
      Container(
        padding: const EdgeInsets.only(left: 2),
        child: Text(
          'B Block, NCC Aster Park...',
          style: TextStyle(
              color: HexColor('#999999'),
              fontSize: 11
          ),
        ),
      )
    ],
  ),
),

关键修改点说明

  • 用PopupMenuButton包裹原有的Home图标+文本+下拉箭头区域,点击整个区域(包括Home文本)都会触发下拉弹窗
  • 设置mainAxisSize: MainAxisSize.min让Row只占用自身内容宽度,避免占满AppBar横向空间
  • 通过itemBuilder自定义下拉菜单的选项,可根据需求调整选项数量和内容
  • onSelected回调用于处理用户选择菜单后的业务逻辑,不需要的话可以直接删除

内容的提问来源于stack exchange,提问作者Rishab Srikanth

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 01:01:19