Flutter垂直导航栏插件vertical_nav_bar的使用

Flutter垂直导航栏插件vertical_nav_bar的使用

通过使用此插件,您可以在Flutter中轻松创建一个漂亮的垂直导航栏,并在不同的操作系统上使用它。以下是关于如何使用该插件的文档。

如何使用?

首先,在Scaffold中定义所需的垂直导航栏:

VerticalNavBar(
  selectedIndex: currentRoute,
  height: MediaQuery.of(context).size.height,
  width: MediaQuery.of(context).size.width * 25 / 100,
  onItemSelected: (value) {
    setState(() {
      _navigateRoutes(value);
    });
  },
  items: const [
    VerticalNavBarItem(
      title: "H O M E",
    ),
    VerticalNavBarItem(
      title: "S E T T I N G S",
    ),
    VerticalNavBarItem(
      title: "P R O F I L E",
    ),
    VerticalNavBarItem(
      title: " S E A R C H",
    ),
  ],
)

参数说明

参数名称 类型 描述
backgroundColor Color 可选参数。容器的颜色
selectedIndex int 可选参数。选中页面的索引
height double 可选参数。经典底部导航栏的高度
width double 可选参数。经典底部导航栏的宽度
borderRadius double 可选参数。底部导航栏角的半径
items List<VerticalNavBarItem> 必须参数。底部导航栏中的项目列表
onItemSelected callbackFunction 必须参数。选择项目的回调函数

使用示例

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

import 'package:flutter/material.dart';

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    List<Widget> myRoutes = [
      SizedBox(
        width: MediaQuery.of(context).size.width * 75 / 100,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: const [
            Text(
              "PAGE 1",
              style: TextStyle(
                color: Colors.black,
                fontSize: 50,
                fontWeight: FontWeight.bold,
              ),
            ),
          ],
        ),
      ),
      SizedBox(
        width: MediaQuery.of(context).size.width * 75 / 100,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: const [
            Text(
              "PAGE 2",
              style: TextStyle(
                color: Colors.black,
                fontSize: 50,
                fontWeight: FontWeight.bold,
              ),
            ),
          ],
        ),
      ),
      SizedBox(
        width: MediaQuery.of(context).size.width * 75 / 100,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: const [
            Text(
              "PAGE 3",
              style: TextStyle(
                color: Colors.black,
                fontSize: 50,
                fontWeight: FontWeight.bold,
              ),
            ),
          ],
        ),
      ),
      SizedBox(
        width: MediaQuery.of(context).size.width * 75 / 100,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: const [
            Text(
              "PAGE 4",
              style: TextStyle(
                color: Colors.black,
                fontSize: 50,
                fontWeight: FontWeight.bold,
              ),
            ),
          ],
        ),
      ),
    ];

    return Scaffold(
      body: Center(
        child: Row(
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                VerticalNavBar(
                  selectedIndex: currentRoute,
                  height: MediaQuery.of(context).size.height,
                  width: MediaQuery.of(context).size.width * 25 / 100,
                  onItemSelected: (value) {
                    setState(() {
                      _navigateRoutes(value);
                    });
                  },
                  items: const [
                    VerticalNavBarItem(
                      title: "H O M E",
                    ),
                    VerticalNavBarItem(
                      title: "S E T T I N G S",
                    ),
                    VerticalNavBarItem(
                      title: "P R O F I L E",
                    ),
                    VerticalNavBarItem(
                      title: " S E A R C H",
                    ),
                  ],
                )
              ],
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [myRoutes[currentRoute]],
            ),
          ],
        ),
      ),
    );
  }

  void _navigateRoutes(int selectedIndex) {
    setState(() {
      currentRoute = selectedIndex;
    });
  }
}

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

1 回复

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


在 Flutter 中,如果你想创建一个垂直导航栏,可以使用 vertical_nav_bar 插件。这个插件提供了一个简单的方式来创建一个垂直布局的导航栏,类似于侧边栏的效果。

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  vertical_nav_bar: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来获取依赖。

2. 使用 VerticalNavBar

接下来,你可以在你的 Flutter 应用中使用 VerticalNavBar 组件。以下是一个简单的示例:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Row(
          children: [
            VerticalNavBar(
              items: [
                VerticalNavBarItem(
                  icon: Icons.home,
                  label: 'Home',
                ),
                VerticalNavBarItem(
                  icon: Icons.search,
                  label: 'Search',
                ),
                VerticalNavBarItem(
                  icon: Icons.settings,
                  label: 'Settings',
                ),
              ],
              onTap: (index) {
                print('Tapped on item $index');
              },
            ),
            Expanded(
              child: Center(
                child: Text('Content Area'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

3. 解释代码

  • VerticalNavBar: 这是主要的组件,用于创建垂直导航栏。
  • VerticalNavBarItem: 每个导航项由一个图标和一个标签组成。
  • onTap: 这是一个回调函数,当用户点击导航项时触发。它会返回被点击项的索引。

4. 自定义样式

你可以通过 VerticalNavBar 的一些属性来自定义导航栏的外观,例如:

  • backgroundColor: 导航栏的背景颜色。
  • selectedItemColor: 选中项的颜色。
  • unselectedItemColor: 未选中项的颜色。
  • iconSize: 图标的大小。
VerticalNavBar(
  backgroundColor: Colors.blue,
  selectedItemColor: Colors.white,
  unselectedItemColor: Colors.grey,
  iconSize: 24.0,
  items: [
    VerticalNavBarItem(
      icon: Icons.home,
      label: 'Home',
    ),
    VerticalNavBarItem(
      icon: Icons.search,
      label: 'Search',
    ),
    VerticalNavBarItem(
      icon: Icons.settings,
      label: 'Settings',
    ),
  ],
  onTap: (index) {
    print('Tapped on item $index');
  },
),

5. 处理导航

你可以根据 onTap 回调中的索引来处理导航逻辑。例如,你可以使用 PageViewIndexedStack 来切换不同的页面。

class MyApp extends StatefulWidget {
  [@override](/user/override)
  _MyAppState createState() => _MyAppState();
}

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Row(
          children: [
            VerticalNavBar(
              items: [
                VerticalNavBarItem(
                  icon: Icons.home,
                  label: 'Home',
                ),
                VerticalNavBarItem(
                  icon: Icons.search,
                  label: 'Search',
                ),
                VerticalNavBarItem(
                  icon: Icons.settings,
                  label: 'Settings',
                ),
              ],
              selectedIndex: _selectedIndex,
              onTap: (index) {
                setState(() {
                  _selectedIndex = index;
                });
              },
            ),
            Expanded(
              child: _pages[_selectedIndex],
            ),
          ],
        ),
      ),
    );
  }
}
回到顶部