Flutter之BottomNavigationBar的封装

栏目: IOS · Android · 发布时间: 5年前

内容简介:BottomNavigationBarBottomNavigationBar 是属于 Scaffold 中的一个位于底部的控件。通常和 BottomNavigationBarItem 配合使用。

Flutter之BottomNavigationBar的封装

BottomNavigationBar

BottomNavigationBar 是属于 Scaffold 中的一个位于底部的控件。通常和 BottomNavigationBarItem 配合使用。

BottomNavigationBar构造方法:

BottomNavigationBar({
        Key key,
        @required this.items,
        this.onTap,
        this.currentIndex = 0,
        this.elevation = 8.0,
        BottomNavigationBarType type,
        Color fixedColor,
        this.backgroundColor,
        this.iconSize = 24.0,
        Color selectedItemColor,
        this.unselectedItemColor,
        this.selectedIconTheme = const IconThemeData(),
        this.unselectedIconTheme = const IconThemeData(),
        this.selectedFontSize = 14.0,
        this.unselectedFontSize = 12.0,
        this.selectedLabelStyle,
        this.unselectedLabelStyle,
        this.showSelectedLabels = true,
        bool showUnselectedLabels,
      })

BottomNavigationBarItem

底部导航栏要显示的Item,有图标和标题组成

BottomNavigationBarItem构造方法:

const BottomNavigationBarItem({
        @required this.icon,
        this.title,
        Widget activeIcon,
        this.backgroundColor,
      })

构建BottomNavigationBar

// 创建底部BottomNavigationBar
      BottomNavigationBar _bottomNavigationBar(List <String>titles, List <String>icons){
        return  BottomNavigationBar(
          items: [
            _bottomBarItem(titles[0], icons[0]),
            _bottomBarItem(titles[1], icons[1]),
            _bottomBarItem(titles[2], icons[2]),
            _bottomBarItem(titles[3], icons[3]),
          ],
          currentIndex: _currentIndex,
          type: BottomNavigationBarType.fixed,// 当items大于3时需要设置此类型
          onTap: _bottomBarItemClick,
          selectedFontSize: 12,
        );
      }
      // 创建item
      BottomNavigationBarItem _bottomBarItem(String title, String iconName,) {
        return BottomNavigationBarItem(
          icon: _image(iconName),
          title: Text(title),
          activeIcon: _image('${iconName}_select'),
          backgroundColor: Colors.white,
        );
      }
      //image
      Widget _image(String iconName) {
        return Image.asset(
          'assets/images/${iconName}@2x.png',// 在项目中添加图片文件夹
          width: 24,
          height: 24,
        );
      }
      //item点击事件
      _bottomBarItemClick(int index) {
        setState(() {
          _currentIndex = index;
        });
      }

调用构建方法

// 当前选中的索引
    int _currentIndex = 0;
    
      //item点击事件
      _bottomBarItemClick(int index) {
        setState(() {
          _currentIndex = index;
        });
      }
      // 数据
      List titles = <String>['首页', '收藏', '消息', '我的'];
      List icons = <String>['tabbar_discover', 'tabbar_favorite', 'tabbar_message', 'tabbar_me'];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          bottomNavigationBar: _bottomNavigationBar(titles, icons),
        );
      }

样式显示

Flutter之BottomNavigationBar的封装


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数字麦克卢汉

数字麦克卢汉

(美)保罗﹒莱文森(Paul Levinson) / 何道宽 / 社会科学文献出版社 / 2001年 / 20.0

本书是一本三合一的书。既是麦克卢汉评传,又是一部专著,而且是让网民“扫盲”和提高的指南。 《数字麦克卢汉》实际上有两个平行的主题和任务。一个是批评和张扬麦克卢汉。另一个是写作者自己的思想。它“不仅谋求提供进入数字时代的向导……而且谋求证明麦克卢汉思想隐而不显的准确性。为了完成这个双重任务,本书的每一章都试图阐明麦克卢汉的一种重要的洞见、原则或概念。与此同时,它试图揭示麦克卢汉告诉我们一些什么......一起来看看 《数字麦克卢汉》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具