内容简介:TabBar自带下划线,若要取消的话让indicator属性等于新的BoxDecoration即可当TabBar放在底部,作为bottomNavigationBar的时候,会被iPhoneX的底部横条覆盖,解决办法是使用SafeArea空间包住TabBarTab的图标如果使用的是Icon,则自带点击图标变化效果。但如果用的是自定义Image,则需要通过setState管理Image引用的资源才能实现其效果。全部代码如下
TabBar自带下划线,若要取消的话让indicator属性等于新的BoxDecoration即可
TabBar(
indicator: const BoxDecoration(),
....
)
复制代码
iPhone X系列底部横条重叠
当TabBar放在底部,作为bottomNavigationBar的时候,会被iPhoneX的底部横条覆盖,解决办法是使用SafeArea空间包住TabBar
@override
Widget build(BuildContext context) {
return Scaffold(
..... //此处代码省略
bottomNavigationBar: Container(
child: SafeArea(
child: TabBar(
controller: controller,
.....
),
),
),
);
}
复制代码
自定义的Tab图标点击变化
Tab的图标如果使用的是Icon,则自带点击图标变化效果。但如果用的是自定义Image,则需要通过setState管理Image引用的资源才能实现其效果。全部代码如下
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
TabController controller; //底部导航控制器
int _currentIndex = 0; //选中位置
String tab1Res; //Tab1的图片资源
String tab2Res; //Tab2的图片资源
@override
void initState() {
super.initState();
//初始化默认图片资源
tab1Res = 'assets/images/ic_groups_activated.png';
tab2Res = 'assets/images/ic_me.png';
controller = TabController(length: 2, vsync: this);
//TabBar监听器
controller.addListener(() => _onTabChanged());
}
@override
Widget build(BuildContext context) {
return Scaffold(
..... //此处代码省略
bottomNavigationBar: Container(
height: 55,
decoration: BoxDecoration(color: Colors.white, boxShadow: [
BoxShadow(color: const Color(0xFFd0d0d0), blurRadius: 1.0)
]),
child: SafeArea(
child: TabBar(
controller: controller,
tabs: [
Tab(
text: "Tab1",
icon: Image.asset(tab1Res, width: 30, height: 30)),
Tab(
text: "Tab2",
icon: Image.asset(tab2Res, width: 30, height: 30)),
],
),
),
),
);
}
/*
底部导航点击事件
*/
_onTabChanged() {
if (controller.indexIsChanging) {
if (this.mounted) {
this.setState(() {
switch (controller.index) {
case 0:
tab1Res = 'assets/images/ic_groups_activated.png';
tab2Res = 'assets/images/ic_me.png';
break;
case 1:
tab1Res = 'assets/images/ic_groups.png';
tab2Res = 'assets/images/ic_me_activated.png';
break;
}
_currentIndex = controller.index;
});
}
}
}
}
复制代码
监听TabBar点击要判断其是否是位置变化和组件是否挂载
Controller监听器不仅仅是点击的监听,还有Axis变化等监听,需要双重判断
/*
底部导航点击事件
*/
_onTabChanged() {
if (controller.indexIsChanging) { //判断是否是选中位置发生变化
if (this.mounted) { //判断组件是否已被挂载
this.setState(() {
switch (controller.index) {
case 0:
tab1Res = 'assets/images/ic_groups_activated.png';
tab2Res = 'assets/images/ic_me.png';
break;
case 1:
tab1Res = 'assets/images/ic_groups.png';
tab2Res = 'assets/images/ic_me_activated.png';
break;
}
_currentIndex = controller.index;
});
}
}
}
复制代码
大家可以关注下我的微信公众号,谢谢
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Weaving the Web
Tim Berners-Lee / Harper Paperbacks / 2000-11-01 / USD 15.00
Named one of the greatest minds of the 20th century by Time , Tim Berners-Lee is responsible for one of that century's most important advancements: the world wide web. Now, this low-profile genius-wh......一起来看看 《Weaving the Web》 这本书的介绍吧!