Flutter侧边导航插件navigation_sidebar的使用

Flutter侧边导航插件navigation_sidebar的使用

特性

Navigation Sidebar 是一个完全可定制的导航小部件。

开始使用

  1. pubspec.yaml 文件中添加包。
  2. 导入包。
  3. 使用 NavigationSideBar 小部件,并在 items 中使用 NavigationSideBarItem

示例代码

import 'package:flutter/material.dart';
import 'package:navigation_sidebar.dart';
import 'package:navigation_sidebar_theme.dart';

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

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

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _selectedIndex = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Row(
          children: [
            NavigationSideBar(
              isExtended: true, // 是否扩展
              isIndicatorActive: true, // 是否显示指示器
              items: [
                NavigationSideBarItem(
                  selectedIcon: Icons.home_filled, // 选中状态的图标
                  unSelectedIcon: Icons.home_outlined, // 未选中状态的图标
                  text: '首页', // 文本标签
                ),
                NavigationSideBarItem(
                  selectedIcon: Icons.search_rounded, // 选中状态的图标
                  unSelectedIcon: Icons.search_outlined, // 未选中状态的图标
                  text: '搜索', // 文本标签
                ),
                NavigationSideBarItem(
                  selectedIcon: Icons.settings_rounded, // 选中状态的图标
                  unSelectedIcon: Icons.settings_outlined, // 未选中状态的图标
                  text: '设置', // 文本标签
                ),
              ],
              initialSelectedIndex: _selectedIndex, // 初始选中索引
              onItemSelected: (int value) {
                setState(() {
                  _selectedIndex = value; // 更新选中索引
                });
              },
            ),
            Expanded(
              child: Center(
                child: Text('当前页面: ${['首页', '搜索', '设置'][_selectedIndex]}'), // 动态显示当前页面
              ),
            ),
          ],
        ),
      ),
    );
  }
}

其他信息

import 'package:flutter/material.dart';
import 'package:navigation_sidebar.dart';
import 'package:navigation_sidebar_theme.dart';

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

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

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _selectedIndex = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: NavigationSideBar(
        items: [
          Page1(), // 第一页
          Page2(), // 第二页
          Page3(), // 第三页
        ],
        onItemSelected: (int index) {
          setState(() {
            _selectedIndex = index; // 更新选中索引
          });
        },
        initialSelectedIndex: _selectedIndex, // 初始选中索引
        isExtended: true, // 是否扩展
        isIndicatorActive: true, // 是否显示指示器
        animationDuration: Duration(milliseconds: 500), // 动画持续时间
        bottomWidget2: Container(), // 添加底部小部件
        bottomWidget: Container(), // 添加底部小部件
        topWidget2: Container(), // 添加顶部小部件
        topWidget: Container(), // 添加顶部小部件
        collapsedWidth: 60, // 收缩宽度
        extendedWidth: 200, // 扩展宽度
        showExtendedButton: true, // 是否显示扩展按钮

        theme: NavigationSideBarTheme(
          backgroundColor: Colors.white, // 背景颜色
          elevation: 10, // 阴影高度
          selectedIconColor: Colors.blue, // 选中图标的颜色
          selectedTextStyle: TextStyle(
            color: Colors.blue, // 选中文本的颜色
            fontSize: 20, // 字号
            fontWeight: FontWeight.bold, // 字体粗细
          ),
          unSelectedIconColor: Colors.black, // 未选中图标的颜色
          unSelectedTextStyle: TextStyle(
            color: Colors.black, // 未选中文本的颜色
            fontSize: 20, // 字号
            fontWeight: FontWeight.bold, // 字体粗细
          ),
        ),
      ),
    );
  }
}

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

1 回复

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


navigation_sidebar 是一个用于 Flutter 的侧边导航栏插件,它可以帮助你轻松地在应用中实现侧边导航功能。以下是如何使用 navigation_sidebar 插件的基本步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  navigation_sidebar: ^1.0.0  # 请检查最新版本

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

2. 导入包

在你的 Dart 文件中导入 navigation_sidebar 包:

import 'package:navigation_sidebar/navigation_sidebar.dart';

3. 使用 NavigationSidebar

NavigationSidebar 是一个小部件,你可以将它添加到你的应用中来创建侧边导航栏。以下是一个简单的示例:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Navigation Sidebar Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _selectedIndex = 0;

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Row(
        children: [
          NavigationSidebar(
            selectedIndex: _selectedIndex,
            onItemSelected: (index) {
              setState(() {
                _selectedIndex = index;
              });
            },
            items: const [
              NavigationSidebarItem(
                icon: Icons.home,
                label: 'Home',
              ),
              NavigationSidebarItem(
                icon: Icons.person,
                label: 'Profile',
              ),
              NavigationSidebarItem(
                icon: Icons.settings,
                label: 'Settings',
              ),
            ],
          ),
          Expanded(
            child: _pages[_selectedIndex],
          ),
        ],
      ),
    );
  }
}

4. 解释代码

  • NavigationSidebar 是一个侧边导航栏小部件,它接受一个 selectedIndex 参数来指示当前选中的项,以及一个 onItemSelected 回调函数来处理项的选择事件。
  • NavigationSidebarItem 是导航栏中的单个项,它包含一个图标和一个标签。
  • _pages 是一个包含不同页面的列表,根据 _selectedIndex 的值来显示相应的页面。

5. 自定义样式

你可以通过 NavigationSidebar 的其他参数来自定义侧边导航栏的样式,例如背景颜色、图标颜色、标签样式等。

NavigationSidebar(
  selectedIndex: _selectedIndex,
  onItemSelected: (index) {
    setState(() {
      _selectedIndex = index;
    });
  },
  items: const [
    NavigationSidebarItem(
      icon: Icons.home,
      label: 'Home',
    ),
    NavigationSidebarItem(
      icon: Icons.person,
      label: 'Profile',
    ),
    NavigationSidebarItem(
      icon: Icons.settings,
      label: 'Settings',
    ),
  ],
  backgroundColor: Colors.blueGrey,
  selectedItemColor: Colors.white,
  unselectedItemColor: Colors.white70,
  itemTextStyle: TextStyle(fontSize: 16),
  iconSize: 24,
);
回到顶部