Flutter底部导航栏插件border_bottom_navigation_bar的使用

Flutter底部导航栏插件border_bottom_navigation_bar的使用

border_bottom_navigation_bar 是一个完全可自定义的 Flutter 底部导航栏插件。它允许开发者根据自己的需求定制底部导航栏的样式和功能。

使用方法

以下是一个完整的示例,展示了如何在 Flutter 应用中使用 border_bottom_navigation_bar 插件。

import 'package:border_bottom_navigation_bar/border_bottom_navigation_bar.dart';
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomeView(),
    );
  }
}

// 忽略: 使用构造函数中的键
class HomeView extends StatefulWidget {
  [@override](/user/override)
  State<HomeView> createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  int _currentIndex = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      extendBody: true,
      appBar: AppBar(
        backgroundColor: Colors.blue[900],
      ),
      body: ListView.builder(
        itemCount: 100,
        itemBuilder: (context, index) {
          return Container(
            height: 300,
            margin: const EdgeInsets.only(bottom: 10),
            decoration: BoxDecoration(
              color: Colors.grey[350],
              image: DecorationImage(
                image: NetworkImage(
                  'https://picsum.photos/id/$index/500',
                ),
                fit: BoxFit.cover,
              ),
            ),
          );
        },
      ),
      bottomNavigationBar: BorderBottomNavigationBar(
        height: 53, // 设置底部导航栏的高度
        currentIndex: _currentIndex, // 当前选中的索引
        borderRadiusValue: 25, // 圆角半径
        onTap: (index) { // 点击事件处理器
          setState(() {
            _currentIndex = index;
          });
          print(index);
        },
        selectedLabelColor: Colors.white, // 选中项标签颜色
        unselectedLabelColor: Colors.grey, // 未选中项标签颜色
        selectedBackgroundColor: Colors.blue[900]!, // 选中项背景颜色
        unselectedBackgroundColor: Colors.grey[200]!, // 未选中项背景颜色
        unselectedIconColor: Colors.grey[600]!, // 未选中项图标颜色
        selectedIconColor: Colors.white, // 选中项图标颜色
        customBottomNavItems: [ // 自定义底部导航栏项
          BorderBottomNavigationItems(
            icon: Icons.home,
          ),
          BorderBottomNavigationItems(
            icon: Icons.event_note_outlined,
          ),
          BorderBottomNavigationItems(
            icon: Icons.add_circle_outline_outlined,
          ),
          BorderBottomNavigationItems(
            icon: Icons.rss_feed_outlined,
          ),
          BorderBottomNavigationItems(
            icon: Icons.more_horiz,
          ),
        ],
      ),
    );
  }
}

更多关于Flutter底部导航栏插件border_bottom_navigation_bar的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter底部导航栏插件border_bottom_navigation_bar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


border_bottom_navigation_bar 是一个 Flutter 插件,它提供了一个带有底部边框的导航栏。这个插件可以帮助你创建一个具有现代风格的底部导航栏,通常用于切换应用中的不同页面。

安装插件

首先,你需要在 pubspec.yaml 文件中添加 border_bottom_navigation_bar 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  border_bottom_navigation_bar: ^0.0.3  # 请确保使用最新版本

然后运行 flutter pub get 来安装插件。

使用 BorderBottomNavigationBar

以下是一个简单的例子,展示了如何使用 BorderBottomNavigationBar

import 'package:flutter/material.dart';
import 'package:border_bottom_navigation_bar/border_bottom_navigation_bar.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Border Bottom Navigation Bar Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentIndex = 0;

  final List<Widget> _pages = [
    Center(child: Text('Home Page')),
    Center(child: Text('Search Page')),
    Center(child: Text('Profile Page')),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Border Bottom Navigation Bar'),
      ),
      body: _pages[_currentIndex],
      bottomNavigationBar: BorderBottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (int index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          BorderBottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BorderBottomNavigationBarItem(
            icon: Icon(Icons.search),
            label: 'Search',
          ),
          BorderBottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: 'Profile',
          ),
        ],
      ),
    );
  }
}

参数说明

  • currentIndex: 当前选中的导航项索引。
  • onTap: 当用户点击导航项时调用的回调函数。
  • items: 导航项的列表,每个 BorderBottomNavigationBarItem 包含一个图标和标签。

自定义样式

你可以通过设置 BorderBottomNavigationBar 的其他属性来自定义导航栏的外观,例如:

  • selectedColor: 选中的导航项颜色。
  • unselectedColor: 未选中的导航项颜色。
  • borderWidth: 底部边框的宽度。
  • borderColor: 底部边框的颜色。
  • elevation: 导航栏的阴影高度。
bottomNavigationBar: BorderBottomNavigationBar(
  currentIndex: _currentIndex,
  onTap: (int index) {
    setState(() {
      _currentIndex = index;
    });
  },
  items: [
    BorderBottomNavigationBarItem(
      icon: Icon(Icons.home),
      label: 'Home',
    ),
    BorderBottomNavigationBarItem(
      icon: Icon(Icons.search),
      label: 'Search',
    ),
    BorderBottomNavigationBarItem(
      icon: Icon(Icons.person),
      label: 'Profile',
    ),
  ],
  selectedColor: Colors.blue,
  unselectedColor: Colors.grey,
  borderWidth: 2.0,
  borderColor: Colors.blue,
  elevation: 8.0,
),
回到顶部