flutter 自定义tabbar 给tabbar添加背景功能
flutter 自带的tabbar BottomNavigationBar有长按水波纹效果,不可以添加背景图片功能,如果有这方面的需求,就需要自定义tabbar了
自定义图片 我们使用BottomAppBar 设定宽高,内部的UI就可以完全自定义 测试了一下,基本能达到想要的效果

废话不多了,直接上代码了
import 'package:flutter/material.dart';
import 'home.dart';
import 'category.dart';
import 'activity.dart';
import 'mine.dart';
class TabbarPage extends StatefulWidget {
@override
_TabbarPageState createState() => _TabbarPageState();
}
class _TabbarPageState extends State<TabbarPage> {
PageController _pageController;
List images = [
['assets/tab_icon_home.png', 'assets/tab_icon_home_selected.png'],
['assets/tab_icon_category.png', 'assets/tab_icon_category_selected.png'],
['assets/tab_icon_collect.png', 'assets/tab_icon_collect_selected.png'],
['assets/tab_icon_profile.png', 'assets/tab_icon_profile_selected.png'],
];
final List _titles = ['首页', '好物', '双11', '我的'];
final List<Widget> _tabBodies = [
HomePage(),
CategoryPage(),
ActivityPage(),
MinePage(),
];
int _currentIndex = 0;
void initState() {
super.initState();
this._pageController =PageController(initialPage: _currentIndex, keepPage: true);
}
@override
Widget build(BuildContext context) {
return Scaffold(
// body: IndexedStack(
// index: _currentIndex,
// children: _tabBodies,
// ),
body: PageView(
children: _tabBodies,
controller: _pageController,
// physics: NeverScrollableScrollPhysics(),
),
// bottomNavigationBar: BottomNavigationBar(
// backgroundColor: Colors.brown,
// type: BottomNavigationBarType.fixed,
// currentIndex: _currentIndex,
// items: getItems(),
// onTap: (index) {
// setState(() {
// _currentIndex = index;
// // _pageController.jumpToPage(index);
// });
// },
// ),
bottomNavigationBar: _bottomAppBar(),
);
}
BottomAppBar _bottomAppBar() {
double width = MediaQuery.of(context).size.width;
double height = 60;
return BottomAppBar(
child: Container(
//设置背景
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
repeat: ImageRepeat.noRepeat,
image: AssetImage('assets/tab_festival_background_android.png'),
),
),
width: width,
height: height,
child: Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: _customItems(),
),
),
);
}
/*获取tab图片*/
Widget getTabIcon(int index) {
if (_currentIndex == index) {
return Image.asset(
images[index][1],
width: 30,
fit: BoxFit.fitHeight,
);
}
return Image.asset(images[index][0], width: 30, fit: BoxFit.fitHeight);
}
/*获取tab文字*/
Text getTabText(int index) {
if (_currentIndex == index) {
return Text(
_titles[index],
style: TextStyle(color: Color(0xFFEA164B), fontSize: 11),
);
}
return Text(
_titles[index],
style: TextStyle(color: Color(0xFF8228CF), fontSize: 11),
);
}
/*获取tabbatItem*/
List<BottomNavigationBarItem> getItems() {
return [
BottomNavigationBarItem(
icon: getTabIcon(0),
title: getTabText(0),
),
BottomNavigationBarItem(icon: getTabIcon(1), title: getTabText(1)),
BottomNavigationBarItem(icon: getTabIcon(2), title: getTabText(2)),
BottomNavigationBarItem(icon: getTabIcon(3), title: getTabText(3)),
];
}
List<Widget> _customItems() {
double width = MediaQuery.of(context).size.width;
//均分成4分
double itemWidth = width / 4.0;
return images.map((img) {
int index = images.indexOf(img);
return GestureDetector(
onTap: () {
setState(() {
_currentIndex = index;
_pageController.jumpToPage(index);
});
},
child: Container(
color: Colors.transparent,
width: itemWidth * 0.8,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
getTabIcon(index),
getTabText(index),
],
),
),
);
}).toList();
}
}
tabbar中通过pageView来实现页面的持久保存,细节参考
flutter 自定义tabbar 给tabbar添加背景功能的更多相关文章
- AJ学IOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮
AJ分享,必须精品 一:效果图 自定义tabBar实现最下面中间的添加按钮 二:思路 首先在自己的tabBarController中把系统的tabBar设置成自己的tabBar(NYTabBar),这 ...
- iOS 自定义UITabBarController的tabBar
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDeleg ...
- sharepoint 2010 如何给文档库或自定义列表添加评论功能
转:http://www.cfanz.cn/?c=article&a=read&id=40924 最近公司在知识库中,有一个需求,就是想要给文档添加评论功能,在sharepoint 2 ...
- SharePoint 2007 文档库中的文档添加评论功能
背景:接到一个项目,要求文档管理,当然文档库就可以了,但是要求文档需要大家去读,读完以后还可以发表评论,这Moss貌似就有点困难了.和同事一起合计,想来想去也没有太好的办法,后来想到传统开发,两个表的 ...
- Android 三档自定义滑动开关,禁止点击功能的实现,用默认的seekbar组件实现
android三档自定义滑动开关,禁止点击功能的实现,普通开关网上有很多例子,三档滑动开关的则找了整天都没有相关例子,开始用普通开关的源码修改了自己实现了一个类,但效果不如人意,各种边界情况的算法很难 ...
- 在VC中,为图片按钮添加一些功能提示(转)
在VC中,也常常为一些图片按钮添加一些功能提示.下面讲解实现过程:该功能的实现主要是用CToolTipCtrl类.该类在VC msdn中有详细说明.首先在对话框的头文件中加入初始化语句:public ...
- Egret 位图,纹理,添加背景 学习
1,重新设置舞台大小,可以直接到VS中的 "解决方案"中,找到 launcher->egret_loader.js中,找到setDesignSize方法,修改其中大小即可:有 ...
- 为HTML表格添加交互功能------DataTables
DataTables是一个功能强大的Javascript库,用于为HTML表格添加交互功能,虽然简单性是整个项目的核心设计原则,但入门看起来相当艰巨.但是,采取这些第一步并在您的网站上运行DataTa ...
- 为CSDN博客添加打赏功能
随着移动支付在国内的兴起,越来越多的付费内容越多如雨后春笋般的冒了出来.其中以<逻辑思维>.罗振宇.李笑来为主要代表作品和人物. 现在很多博客或者个人网站里面都有打赏功能,这算是对博主的劳 ...
随机推荐
- 学java编程软件开发,非计算机专业是否能学
近几年互联网的发展越来越好,在国外,java程序员已经成为高薪以及稳定职业的代表,虽然国内的有些程序员很苦逼,但是那只是少数,按照国外的大方向来看,程序员还是一个很吃香的职业.根据编程语言的流行程度, ...
- Delphi 抽象方法
- linux中公钥和私钥的区别以及关系
导读 在学习ssh章节时,一定有不少人对公钥和私钥产生过不解.在搜索公钥跟私钥的理解时,发现了这篇有趣的图解小文章,与大家共享. 1. 鲍勃有两把钥匙,一把是公钥,另一把是私钥. 2. 鲍勃把公钥送给 ...
- Layui 内置方法 - layer.prompt_(输入层)
prompt的参数也是向前补齐的.options不仅可支持传入基础参数,还可以传入prompt专用的属性.当然,也可以不传,yes携带value 表单值index 索引elem 表单元素. promp ...
- Create Advanced Web Applications With Object-Oriented Techniques
Create Advanced Web Applications With Object-Oriented Techniques Ray Djajadinata Recently I intervie ...
- 01 salt平台,软件架构图
1.前期调研 1.别人家的 https://www.cnblogs.com/ssyfj/p/9060367.html#top https://www.cnblogs.com/evilliu/artic ...
- [Functional Programming] Reader with Async ADT
ReaderT is a Monad Transformer that wraps a given Monad with a Reader. This allows the interface of ...
- Google Maps API3 之 Hello World
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-e ...
- Luogu P5022 旅行 搜索+贪心
好吧...一直咕..现在才过...被卡常卡到爆... 写的垃圾版本,$n^2$无脑删边..可以发现走出来的是棵树...更优秀的及数据加强版先咕着...一定写.qwq #include<cstdi ...
- 在gitlab上删除分支后,本地git branch -r还能看到
1. git remote prune --dry-run origin 查看当前有哪些是该消失还存在的分支 2. git remote prune origin 删除上面展示的所有分支 3. git ...