Flutter导航栏动画插件the_brioflashy_navbar的使用

Flutter导航栏动画插件the_brioflashy_navbar的使用

flashly_tab_bar

一个漂亮的带有动画效果的导航栏(灵感来源于Cuberto)。

Animation


开始使用

pubspec.yaml 文件中添加依赖:

dependencies:
  ...
  thebrioflashynavbar: ^0.0.1

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


基本用法

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

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

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

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

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

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flashy Tab Bar 示例'),
      ),
      body: Center(
        child: Text(
          '当前选中的页面是: ${_selectedIndex + 1}',
          style: TextStyle(fontSize: 20),
        ),
      ),
      bottomNavigationBar: Thebrioflashynavbar(
        selectedIndex: _selectedIndex,
        showElevation: true,
        onItemSelected: (index) => setState(() {
          _selectedIndex = index;
        }),
        items: [
          ThebrioflashynavbarItem(
            icon: Icon(Icons.event),
            title: Text('事件'),
          ),
          ThebrioflashynavbarItem(
            icon: Icon(Icons.search),
            title: Text('搜索'),
          ),
          ThebrioflashynavbarItem(
            icon: Icon(Icons.highlight),
            title: Text('高亮'),
          ),
          ThebrioflashynavbarItem(
            icon: Icon(Icons.settings),
            title: Text('设置'),
          ),
          ThebrioflashynavbarItem(
            icon: Icon(Icons.settings),
            title: Text('韩语'),
          ),
        ],
      ),
    );
  }
}

代码解释

  1. 导入包

    import 'package:thebrioflashynavbar/thebrioflashynavbar.dart';
    

    导入 thebrioflashynavbar 包以使用其功能。

  2. 初始化状态

    int _selectedIndex = 0;
    

    定义一个变量 _selectedIndex 来跟踪当前选中的导航项。

  3. 构建导航栏

    bottomNavigationBar: Thebrioflashynavbar(
      selectedIndex: _selectedIndex,
      showElevation: true,
      onItemSelected: (index) => setState(() {
        _selectedIndex = index;
      }),
      items: [
        ThebrioflashynavbarItem(
          icon: Icon(Icons.event),
          title: Text('事件'),
        ),
        ThebrioflashynavbarItem(
          icon: Icon(Icons.search),
          title: Text('搜索'),
        ),
        ThebrioflashynavbarItem(
          icon: Icon(Icons.highlight),
          title: Text('高亮'),
        ),
        ThebrioflashynavbarItem(
          icon: Icon(Icons.settings),
          title: Text('设置'),
        ),
        ThebrioflashynavbarItem(
          icon: Icon(Icons.settings),
          title: Text('韩语'),
        ),
      ],
    ),
    
    • selectedIndex: 当前选中的导航项索引。
    • showElevation: 是否显示阴影效果。
    • onItemSelected: 当用户点击导航项时触发的回调函数。
    • items: 导航栏中的每个项目,包含图标和文字。
  4. 更新状态

    onItemSelected: (index) => setState(() {
      _selectedIndex = index;
    }),
    

    当用户点击导航项时,通过 setState 更新 _selectedIndex


许可证

MIT 许可证

版权所有 © 2022 TheBrio

许可协议如下:

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

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

1 回复

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


the_brioflashy_navbar 是一个 Flutter 插件,用于创建带有动画效果的导航栏。它可以帮助你为应用程序的底部导航栏添加一些炫酷的动画效果,提升用户体验。

安装

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

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

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

基本用法

以下是一个简单的示例,展示如何使用 the_brioflashy_navbar 插件创建一个带有动画效果的底部导航栏。

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Brioflashy Navbar Example',
      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('Brioflashy Navbar Example'),
      ),
      body: _pages[_currentIndex],
      bottomNavigationBar: BrioflashyNavBar(
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          BrioflashyNavBarItem(
            icon: Icons.home,
            title: 'Home',
            activeColor: Colors.blue,
          ),
          BrioflashyNavBarItem(
            icon: Icons.search,
            title: 'Search',
            activeColor: Colors.green,
          ),
          BrioflashyNavBarItem(
            icon: Icons.person,
            title: 'Profile',
            activeColor: Colors.purple,
          ),
        ],
      ),
    );
  }
}

参数说明

  • currentIndex: 当前选中的导航栏项的索引。
  • onTap: 当用户点击导航栏项时触发的回调函数。
  • items: 导航栏项的列表,每个项都是一个 BrioflashyNavBarItem 对象。
    • icon: 导航栏项的图标。
    • title: 导航栏项的标题。
    • activeColor: 导航栏项被选中时的颜色。

自定义动画效果

the_brioflashy_navbar 插件允许你自定义导航栏的动画效果。你可以通过调整 BrioflashyNavBar 的参数来实现不同的动画效果。

例如,你可以通过 animationDuration 参数来调整动画的持续时间:

BrioflashyNavBar(
  currentIndex: _currentIndex,
  onTap: (index) {
    setState(() {
      _currentIndex = index;
    });
  },
  animationDuration: Duration(milliseconds: 500),  // 设置动画持续时间
  items: [
    BrioflashyNavBarItem(
      icon: Icons.home,
      title: 'Home',
      activeColor: Colors.blue,
    ),
    BrioflashyNavBarItem(
      icon: Icons.search,
      title: 'Search',
      activeColor: Colors.green,
    ),
    BrioflashyNavBarItem(
      icon: Icons.person,
      title: 'Profile',
      activeColor: Colors.purple,
    ),
  ],
)
回到顶部