You need to enable JavaScript to run this app. Flutter中如何使用Painter实现自定义底部导航栏并让中间图标溢出容器
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何使用Painter实现自定义底部导航栏并让中间图标溢出容器

实现方案

核心原理:利用Flutter的层叠布局能力,打破父容器裁剪限制即可实现中间图标溢出效果,有两种常用实现方式可选:

方案1:官方自带实现(适配带凹口的导航栏)

直接使用Scaffold自带的floatingActionButton和floatingActionButtonLocation属性,可快速实现带底部凹口的凸起按钮效果:

import 'package:flutter/material.dart';

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

  @override
  State<CustomBottomNav> createState() => _CustomBottomNavState();
}

class _CustomBottomNavState extends State<CustomBottomNav> {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: const Center(child: Text("页面内容区域")),
      // 中间凸起按钮
      floatingActionButton: FloatingActionButton(
        shape: const CircleBorder(),
        backgroundColor: Colors.blue,
        onPressed: () {
          // 中间按钮点击逻辑
        },
        child: const Icon(Icons.add, color: Colors.white, size: 30),
      ),
      // 设置按钮位置为底部导航中间悬浮
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      bottomNavigationBar: BottomAppBar(
        shape: const CircularNotchedRectangle(), // 匹配按钮的凹口形状
        notchMargin: 8, // 凹口和按钮的间距
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            IconButton(
              onPressed: () => setState(() => _currentIndex = 0),
              icon: Icon(Icons.home, color: _currentIndex == 0 ? Colors.blue : Colors.grey),
            ),
            IconButton(
              onPressed: () => setState(() => _currentIndex = 1),
              icon: Icon(Icons.search, color: _currentIndex == 1 ? Colors.blue : Colors.grey),
            ),
            const SizedBox(width: 40), // 给中间按钮预留位置
            IconButton(
              onPressed: () => setState(() => _currentIndex = 2),
              icon: Icon(Icons.favorite, color: _currentIndex == 2 ? Colors.blue : Colors.grey),
            ),
            IconButton(
              onPressed: () => setState(() => _currentIndex = 3),
              icon: Icon(Icons.person, color: _currentIndex == 3 ? Colors.blue : Colors.grey),
            ),
          ],
        ),
      ),
    );
  }
}

方案2:自定义层叠布局(适配任意样式的溢出按钮)

如果不需要凹口、或者按钮样式异形,用Stack层叠布局实现,灵活度最高:

int _currentIndex = 0;

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: const Center(child: Text("页面内容区域")),
    bottomNavigationBar: Stack(
      clipBehavior: Clip.none, // 关键:允许子组件超出Stack边界不被裁剪
      alignment: Alignment.topCenter,
      children: [
        // 底部导航栏本体
        BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: (index) => setState(() => _currentIndex = index),
          type: BottomNavigationBarType.fixed,
          items: const [
            BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
            BottomNavigationBarItem(icon: Icon(Icons.search), label: "搜索"),
            BottomNavigationBarItem(icon: SizedBox.shrink(), label: ""), // 中间位置占位
            BottomNavigationBarItem(icon: Icon(Icons.favorite), label: "收藏"),
            BottomNavigationBarItem(icon: Icon(Icons.person), label: "我的"),
          ],
        ),
        // 中间溢出的自定义按钮
        Positioned(
          top: -20, // 向上偏移20dp,实现溢出效果,可根据设计调整
          child: GestureDetector(
            onTap: () {
              // 中间按钮点击逻辑
            },
            child: Container(
              padding: const EdgeInsets.all(12),
              decoration: const BoxDecoration(
                color: Colors.blue,
                shape: BoxShape.circle,
              ),
              child: const Icon(Icons.add, color: Colors.white, size: 28),
            ),
          ),
        )
      ],
    ),
  );
}

注意事项:使用Stack方案时必须显式设置clipBehavior: Clip.none,否则超出Stack边界的按钮部分会被系统自动裁剪,看不到凸起效果。

内容的提问来源于stack exchange,提问作者Inder Pal Singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.18 01:24:03