Flutter导航栏高亮插件highlight_nav_bar的使用

发布于 1周前 作者 yibo5220 来自 Flutter

Flutter导航栏高亮插件highlight_nav_bar的使用

安装

  1. 在你的pubspec.yaml文件中添加依赖。
dependencies:
  highlight_nav_bar: <latest version>
  1. 导入highlight_nav_bar包。
import 'package:highlight_nav_bar/spotlight_nav_bar.dart';

使用

你可以通过以下方式使用highlight_nav_bar来创建一个带有高亮效果的底部导航栏。

MaterialApp(
  title: 'SpotlightNavBar Example',
  theme: ThemeData.light(),
  darkTheme: ThemeData.dark(),
  themeMode: ThemeMode.system,
  home: SpotlightNavBar(
    items: [
      SpotlightItem(
        title: 'Home',
        screen: const Center(
          child: Text('Home'),
        ),
      ),
      SpotlightItem(
        title: 'Search',
        screen: const Center(
          child: Text('Search'),
        ),
        icon: Icons.search,
        spotlightColor: Colors.green,
      ),
      SpotlightItem(
        title: 'Profile',
        screen: const Center(
          child: Text('Profile'),
        ),
        icon: Icons.person,
        lampColor: Colors.red,
        spotlightColor: Colors.blue,
      ),
    ],
  ),
);

示例

下面是完整的示例代码:

import 'package:flutter/material.dart';
import 'package:highlight_nav_bar/spotlight_item.dart';
import 'package:highlight_nav_bar/spotlight_nav_bar.dart';

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'SpotlightNavBar Example',
        theme: ThemeData.light(),
        darkTheme: ThemeData.dark(),
        themeMode: ThemeMode.system,
        home: SpotlightNavBar(
          items: [
            SpotlightItem(
              title: 'Home',
              screen: const Center(
                child: Text('Home'),
              ),
            ),
            SpotlightItem(
              title: 'Search',
              screen: const Center(
                child: Text('Search'),
              ),
              icon: Icons.search,
              spotlightColor: Colors.green,
            ),
            SpotlightItem(
              title: 'Profile',
              screen: const Center(
                child: Text('Profile'),
              ),
              icon: Icons.person,
              lampColor: Colors.red,
              spotlightColor: Colors.blue,
            ),
          ],
        ),
    );
  }
}

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

1 回复

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


当然,下面是一个关于如何在Flutter中使用highlight_nav_bar插件来实现导航栏高亮效果的代码案例。

首先,确保你已经在pubspec.yaml文件中添加了highlight_nav_bar依赖:

dependencies:
  flutter:
    sdk: flutter
  highlight_nav_bar: ^x.y.z  # 请将x.y.z替换为当前最新版本号

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

接下来,是一个简单的示例代码,展示了如何使用highlight_nav_bar来创建一个带有高亮效果的导航栏。

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

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

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

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

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

  final List<Widget> _widgetOptions = <Widget>[
    Text('Home'),
    Text('Search'),
    Text('Profile'),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Highlight Nav Bar Demo'),
        bottom: PreferredSize(
          preferredSize: Size.fromHeight(50.0),
          child: HighlightNavBar(
            items: [
              HighlightNavBarItem(
                icon: Icon(Icons.home),
                title: Text('Home'),
              ),
              HighlightNavBarItem(
                icon: Icon(Icons.search),
                title: Text('Search'),
              ),
              HighlightNavBarItem(
                icon: Icon(Icons.person),
                title: Text('Profile'),
              ),
            ],
            currentIndex: _selectedIndex,
            onItemSelected: (index) {
              setState(() {
                _selectedIndex = index;
              });
            },
          ),
        ),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

代码解释

  1. 依赖导入

    import 'package:highlight_nav_bar/highlight_nav_bar.dart';
    
  2. 创建导航栏项: 使用HighlightNavBarItem来定义每个导航项,每个项包含一个图标和一个标题。

  3. HighlightNavBar: 使用HighlightNavBar来创建导航栏,传递导航项列表、当前选中的索引以及一个选择项时的回调函数。

  4. 更新状态: 当导航项被选中时,通过setState方法来更新当前选中的索引,从而刷新UI。

  5. 显示选中内容: 根据当前选中的索引,在body中显示相应的内容。

这个示例展示了如何使用highlight_nav_bar插件来创建一个简单的带有高亮效果的导航栏。你可以根据实际需求进一步定制导航栏的样式和功能。

回到顶部