flutter如何实现convex_bottom_bar导航栏

在Flutter中如何实现convex_bottom_bar导航栏?我按照官方文档添加了依赖并配置了基本样式,但运行时出现异常提示"Bad state: No element"。请问正确的集成步骤是什么?是否需要额外配置?能否提供一个完整的实现示例,包括如何定义Tab页面和点击切换逻辑?

2 回复

使用ConvexAppBar包实现凸起底部导航栏。

  1. 在pubspec.yaml添加依赖:convex_bottom_bar: ^latest_version
  2. 导入包:import 'package:convex_bottom_bar/convex_bottom_bar.dart';
  3. 在Scaffold的bottomNavigationBar中使用ConvexAppBar,配置items和onTap即可。

更多关于flutter如何实现convex_bottom_bar导航栏的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在 Flutter 中,使用 convex_bottom_bar 包可以轻松实现凸起式底部导航栏。以下是实现步骤:

  1. 添加依赖
    pubspec.yaml 文件中添加依赖:

    dependencies:
      convex_bottom_bar: ^latest_version
    

    运行 flutter pub get 安装包。

  2. 基本使用
    在页面中使用 ConvexAppBar 组件:

    import 'package:convex_bottom_bar/convex_bottom_bar.dart';
    import 'package:flutter/material.dart';
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _selectedIndex = 0;
    
      final List<Widget> _pages = [
        Page1(),
        Page2(),
        Page3(),
      ];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: _pages[_selectedIndex],
          bottomNavigationBar: ConvexAppBar(
            items: [
              TabItem(icon: Icons.home, title: '首页'),
              TabItem(icon: Icons.map, title: '发现'),
              TabItem(icon: Icons.add, title: '添加'), // 中间凸起按钮
              TabItem(icon: Icons.message, title: '消息'),
              TabItem(icon: Icons.people, title: '我的'),
            ],
            initialActiveIndex: _selectedIndex,
            onTap: (int index) {
              setState(() {
                _selectedIndex = index;
              });
            },
          ),
        );
      }
    }
    
  3. 自定义样式

    • 样式类型:通过 style 参数设置样式,如 TabStyle.fixedCircle(固定圆形凸起)、TabStyle.react(响应式)等。
    • 颜色:使用 backgroundColoractiveColorcolor 调整背景、激活和未激活颜色。
    • 示例
      ConvexAppBar(
        style: TabStyle.fixedCircle,
        backgroundColor: Colors.blue,
        items: [...],
        onTap: (index) {...},
      );
      
  4. 凸起按钮自定义
    中间按钮可通过 TabItemicontitle 自定义,或使用 ConvexAppBar.builder 构造更复杂的项。

注意事项

以上代码即可实现一个带凸起效果的底部导航栏,支持点击切换页面。

回到顶部