flutter好用的轮子推荐八-flutter一个超酷动画的底部tab栏

9,036 阅读2分钟

前言

Flutter是谷歌的移动UI框架,可以快速在iOS和Android上构建高质量的原生用户界面。

IT界著名的尼古拉斯·高尔包曾说:轮子是IT进步的阶梯!热门的框架千篇一律,好用轮子万里挑一!Flutter作为这两年开始崛起的跨平台开发框架,其第三方生态相比其他成熟框架还略有不足,但轮子的数量也已经很多了。本系列文章挑选日常app开发常用的轮子分享出来,给大家提高搬砖效率,同时也希望flutter的生态越来越完善,轮子越来越多。

本系列文章准备了超过50个轮子推荐,工作原因,尽量每1-2天出一篇文章。

tip:本系列文章合适已有部分flutter基础的开发者,入门请戳:flutter官网

正文

轮子

  • 轮子名称:curved_navigation_bar
  • 轮子概述:flutter一个超酷动画的底部tab栏.
  • 轮子作者:rafbednarczuk@gmail.com
  • 推荐指数:★★★★
  • 常用指数:★★★★
  • 效果预览:
    原文效果gif
    效果图

安装

dependencies:
  curved_navigation_bar: ^0.3.1
import 'package:curved_navigation_bar/curved_navigation_bar.dart';

使用

  • items:按钮小部件列表
  • index:NavigationBar的索引,可用于更改当前索引或设置初始索引
  • color:NavigationBar的颜色,默认值为Colors.white
  • buttonBackgroundColor:浮动按钮的背景色
  • backgroundColor: NavigationBar动画镂空时的背景,默认的Colors.blueAccent
  • onTap:按钮点击事件(index)
  • animationCurve:动画曲线,默认的Curves.easeOutCubic
  • animationDuration:按钮更改动画的持续时间,默认的Duration(毫秒:600)
  • height:NavigationBar的高度,最小值0.0,最高75.0

默认示例:

Scaffold(
  bottomNavigationBar: CurvedNavigationBar(
    backgroundColor: Colors.blueAccent,
    items: <Widget>[
      Icon(Icons.add, size: 30),
      Icon(Icons.list, size: 30),
      Icon(Icons.compare_arrows, size: 30),
    ],
    onTap: (index) {
      //Handle button tap
    },
  ),
  body: Container(color: Colors.blueAccent),
)

与TabBarView一起联动使用

class CurvedNavigationBarDemo extends StatefulWidget {
    CurvedNavigationBarDemo({Key key}) : super(key: key);

    @override
    _CurvedNavigationBarDemoState createState() => _CurvedNavigationBarDemoState();
}

class _CurvedNavigationBarDemoState extends State<CurvedNavigationBarDemo> with SingleTickerProviderStateMixin{

    TabController tabController;

    List colors=[Colors.blue,Colors.pink,Colors.orange];

    int currentIndex=0;

    @override
    void initState() {
        // TODO: implement initState
        super.initState();
        tabController = TabController(vsync: this, length: 3)..addListener((){
            setState(() {
                currentIndex=tabController.index;
            });
        });
    }

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            bottomNavigationBar: CurvedNavigationBar(
                backgroundColor: colors[currentIndex],
                index: currentIndex,
                items: <Widget>[
                    Icon(Icons.home, size: 30),
                    Icon(Icons.fiber_new, size: 30),
                    Icon(Icons.person, size: 30),
                ],
                onTap: (index) {
                    //Handle button tap
                    setState(() {
                        currentIndex=index;
                    });
                    tabController.animateTo(index,duration: Duration(milliseconds: 300), curve: Curves.ease);
                },
            ),
            body: TabBarView(
                controller: tabController,
                children: <Widget>[
                    Container(
                        color: colors[0],
                    ),
                    Container(
                        color: colors[1],
                    ),
                    Container(
                        color: colors[2],
                    )
                ],
            )
        );
    }
}

结尾