自定义导航栏组件

核心代码实现

1. 自定义导航栏组件

首先,我们创建一个自定义的底部导航栏组件 CustomBottomNavBar。这个组件完全由我们自己绘制,可以灵活控制每个细节。

dart 复制代码
class CustomBottomNavBar extends StatelessWidget {
  final int currentIndex;
  final Function(int) onTap;

  const CustomBottomNavBar({
    super.key,
    required this.currentIndex,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.1),
            blurRadius: 20,
            offset: const Offset(0, -5),
          ),
        ],
      ),
      child: SafeArea(
        child: Container(
          height: 65,
          padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              _NavBarItem(
                icon: Icons.home_outlined,
                selectedIcon: Icons.home,
                label: '首页',
                isSelected: currentIndex == 0,
                onTap: () => onTap(0),
              ),
              _NavBarItem(
                icon: Icons.explore_outlined,
                selectedIcon: Icons.explore,
                label: '发现',
                isSelected: currentIndex == 1,
                onTap: () => onTap(1),
              ),
              _NavBarItem(
                icon: Icons.message_outlined,
                selectedIcon: Icons.message,
                label: '消息',
                isSelected: currentIndex == 2,
                onTap: () => onTap(2),
              ),
              _NavBarItem(
                icon: Icons.person_outlined,
                selectedIcon: Icons.person,
                label: '我的',
                isSelected: currentIndex == 3,
                onTap: () => onTap(3),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
相关推荐
nashane1 天前
HarmonyOS 6学习:AI攻略长截图“防抖”与像素级拼接术
学习·华为·harmonyos
想你依然心痛2 天前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“代码哨兵“——AI智能体代码安全审计平台
人工智能·安全·harmonyos·智能体
轻口味2 天前
HarmonyOS 6.1 全栈实战录 - 09 极光底座:ArkWeb 6.1 性能、安全与视觉插帧全特性深度实战
pytorch·安全·harmonyos
Ww.xh2 天前
鸿蒙Web组件中Hash路由传登录态方案
前端·哈希算法·harmonyos
nashane2 天前
HarmonyOS 6学习:Canvas性能优化与长截图流畅实现实战
学习·性能优化·harmonyos
轻口味2 天前
HarmonyOS 6.1 全栈实战录 - 13 流量增长新引擎:全场景归因与 App Linking 链接深度开发实战
pytorch·深度学习·harmonyos
轻口味2 天前
HarmonyOS 6.1 全栈实战录 - 12 性能底座与包管理演进深度开发实战
华为·harmonyos
云水一下2 天前
华为防火墙安全区域与NAT实战:基于eNSP的企业边界网完整部署
安全·华为·nat·下一代防火墙
小雨青年2 天前
鸿蒙 HarmonyOS 6 | Pura X Max 鸿蒙原生适配 06:GridRow 做卡片自适应布局
华为·harmonyos
前端不太难2 天前
一套鸿蒙 App,如何跑在手机 / 平板 / TV?
智能手机·电脑·harmonyos