Flutter底部导航点插件dot_navbar的使用

Flutter底部导航点插件dot_navbar的使用

屏幕截图

样式1

视频

样式1

示例项目

示例Flutter项目

简介

dot_navbar 包为Flutter开发者提供了一个可定制且视觉效果良好的点状底部/顶部导航栏。该包允许您轻松地将底部导航栏集成到您的Flutter应用中,并实现平滑的屏幕切换。

特性

  • 可定制图标:可以轻松自定义活动和非活动状态下的图标,并支持每个状态的不同图标。
  • 灵活的样式:调整选中和未选中项目的字体大小,并设置导航栏的自定义背景颜色。
  • 无缝集成:简单地集成到Flutter应用程序中,兼容各种屏幕尺寸和分辨率。
  • 响应式设计:自动调整以适应屏幕尺寸,确保响应式设计。
  • 直观的交互:响应式的点击处理,通过 onTap 回调捕获用户交互。

安装

在您的 pubspec.yaml 文件中添加以下内容:

dependencies:
  dot_navbar: ^0.0.1
  riverpod: 
  flutter_riverpod: 
  hooks_riverpod: 

然后运行:

flutter pub get

在您的Dart代码中导入该包:

import 'package:dot_navbar/dot_navbar.dart';

使用方法

第一步

此包使用 riverpod 库。

void main() {
  runApp(
    const ProviderScope(
      child: MyApp(),
    ),
  );
}

点导航栏

一个可定制的导航栏。

  • 选中图标颜色:selectedItemColor
  • 未选中图标颜色:unselectedItemColor
  • 是底部还是顶部:isBottom
DotNavBar(
  dotMenuItems: SharedList.itemList,
  navbarBackgroundColor: Colors.white,
  selectedItemColor: Colors.black,
  unselectedItemColor: Colors.grey,
  isBottom: true,
  isTitleVisible: true,
  dotMenuItems:[
    DotMenuItemModel(
      keyValue: 'home',
      page: Container(
        color: Colors.red,
      ),
      icon: Icons.home,
    ),
    DotMenuItemModel(
      keyValue: 'search',
      page: Container(
        color: Colors.blue,
      ),
      icon: Icons.search,
    ),
    DotMenuItemModel(
      keyValue: 'add',
      page: Container(
        color: Colors.green,
      ),
      icon: Icons.add,
    ),
    DotMenuItemModel(
      keyValue: 'notifications',
      page: Container(
        color: Colors.yellow,
      ),
      icon: Icons.notifications,
    ),
    DotMenuItemModel(
      keyValue: 'profile',
      page: Container(
        color: Colors.purple,
      ),
      icon: Icons.person,
    ),
  ]
),

点菜单项模型

表示导航栏中一个项目的数据类。

DotMenuItemModel(
  keyValue: "Object Key Value",
  page: widgets(),
  icon: Icons.home,
)

完整实现

import 'package:dot_navbar/dot_navbar.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge, overlays: []);
  runApp(
    const ProviderScope(
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: SharedConstants.appName,
      home: DotNavBar(
        navbarBackgroundColor: Colors.black,
        selectedItemColor: Colors.white,
        unselectedItemColor: Colors.grey,
        isBottom: true,
        isTitleVisible: false,
        dotMenuItems:[
          DotMenuItemModel(
            keyValue: 'home',
            page: Container(
              color: Colors.red,
            ),
            icon: Icons.home,
          ),
          DotMenuItemModel(
            keyValue: 'search',
            page: Container(
              color: Colors.blue,
            ),
            icon: Icons.search,
          ),
          DotMenuItemModel(
            keyValue: 'add',
            page: Container(
              color: Colors.green,
            ),
            icon: Icons.add,
          ),
          DotMenuItemModel(
            keyValue: 'notifications',
            page: Container(
              color: Colors.yellow,
            ),
            icon: Icons.notifications,
          ),
          DotMenuItemModel(
            keyValue: 'profile',
            page: Container(
              color: Colors.purple,
            ),
            icon: Icons.person,
          ),
        ]
      ),
    );
  }
}

问题

如果您遇到任何问题或有改进建议,请访问 GitHub仓库 并提交问题或拉取请求。

开发者

Ali Birkan BAYRAM - LinkedIn


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

1 回复

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


dot_navbar 是一个用于 Flutter 的底部导航栏插件,它提供了一个带有圆点指示器的底部导航栏,可以轻松地在不同的页面之间切换。它具有良好的自定义性,允许你设置颜色、图标、标签等。

以下是如何在 Flutter 项目中使用 dot_navbar 插件的步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 dot_navbar 依赖:

dependencies:
  flutter:
    sdk: flutter
  dot_navbar: ^1.1.0+1  # 使用最新版本

然后运行 flutter pub get 以安装依赖。

2. 使用 DotNavBar 组件

在你的 Flutter 应用中,可以使用 DotNavBar 组件来创建底部导航栏。以下是一个简单的示例:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int _currentIndex = 0;

  // 页面列表
  final List<Widget> _pages = [
    Page1(),
    Page2(),
    Page3(),
    Page4(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pages[_currentIndex],
      bottomNavigationBar: DotNavBar(
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          DotNavBarItem(icon: Icons.home, selectedColor: Colors.blue),
          DotNavBarItem(icon: Icons.search, selectedColor: Colors.red),
          DotNavBarItem(icon: Icons.notifications, selectedColor: Colors.green),
          DotNavBarItem(icon: Icons.person, selectedColor: Colors.purple),
        ],
      ),
    );
  }
}

// 示例页面
class Page1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Page 1'));
  }
}

class Page2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Page 2'));
  }
}

class Page3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Page 3'));
  }
}

class Page4 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('Page 4'));
  }
}

3. 自定义 DotNavBar

DotNavBar 提供了多个可自定义的属性,包括:

  • currentIndex:当前选中的索引。
  • onTap:点击导航项时的回调函数。
  • items:导航项列表,每个导航项由 DotNavBarItem 定义。
  • dotColor:圆点指示器的颜色。
  • backgroundColor:导航栏的背景颜色。
  • unselectedItemColor:未选中项的颜色。
  • margin:导航栏的外边距。
  • curve:切换页面时的动画曲线。
  • duration:切换页面时的动画时长。

例如,你可以这样自定义导航栏:

DotNavBar(
  currentIndex: _currentIndex,
  onTap: (index) {
    setState(() {
      _currentIndex = index;
    });
  },
  items: [
    DotNavBarItem(icon: Icons.home, selectedColor: Colors.blue),
    DotNavBarItem(icon: Icons.search, selectedColor: Colors.red),
    DotNavBarItem(icon: Icons.notifications, selectedColor: Colors.green),
    DotNavBarItem(icon: Icons.person, selectedColor: Colors.purple),
  ],
  dotColor: Colors.orange,
  backgroundColor: Colors.white,
  unselectedItemColor: Colors.grey,
  margin: EdgeInsets.all(10),
  curve: Curves.easeInOut,
  duration: Duration(milliseconds: 300),
);
回到顶部