最近在学习flutter相关的知识,在学到Tabbar的时候默认无法修改它的高度背景色,在网上查看被人的代码后自己封装了一下。你可以复制直接使用。(不排除有更好的实现方法,我也是刚接触flutter)

使用时如果指定有图标的高度要比无图标的高度大26 也可以自己再改下代码自定义。

  bottom: CustomTabBar(// 背景色backgroundColor: Colors.white,// 无图标高度kTabHeight: 26,// 有图标高度kTextAndIconTabHeight: 52,tabs: [Tab(text: "test",icon: Icon(Icons.add),),Tab(text: "test"),Tab(text: "test"),],),
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';class CustomTabBar extends PreferredSize {/// tab 集合final List<CustomHeightTab> _tabs = new List();/// 指示器长度类型final TabBarIndicatorSize indicatorSize;/// tabBar 背景色final Color backgroundColor;/// 无图标tabBar高度final double kTabHeight;/// 有图标tabBar高度final double kTextAndIconTabHeight;/// 选中指示器高度final double indicatorWeight;/// 指示器颜色final Color indicatorColor;/// 多标签是否可以滚动final bool isScrollable;/// tab控制器final TabController controller;/// 指示器边距final EdgeInsetsGeometry indicatorPadding;/// 指示器样式final Decoration indicator;/// 标签颜色final Color labelColor;/// 标签字体样式final TextStyle labelStyle;/// 标签边距final EdgeInsetsGeometry labelPadding;/// 未选中标签样式final Color unselectedLabelColor;/// 未选中标签字体样式final TextStyle unselectedLabelStyle;final DragStartBehavior dragStartBehavior;/// 点击tab触发函数final ValueChanged<int> onTap;/// TabBar中添加Tabvoid addTab(text, [Icon icon, Widget child]) {// 添加真正使用到的自定义tab_tabs.add(CustomHeightTab(text: text,icon: icon,child: child,kTabHeight: this.kTabHeight,kTextAndIconTabHeight: this.kTextAndIconTabHeight,));}CustomTabBar({Key key,@required List<Tab> tabs,this.backgroundColor = Colors.white,this.kTabHeight = 30,this.kTextAndIconTabHeight = 46,this.controller,this.isScrollable = false,this.indicatorColor,this.indicatorWeight = 2.0,this.indicatorPadding = EdgeInsets.zero,this.indicator,this.indicatorSize,this.labelColor=Colors.black,this.labelStyle,this.labelPadding,this.unselectedLabelColor,this.unselectedLabelStyle,this.dragStartBehavior = DragStartBehavior.start,this.onTap,})  : assert(tabs != null),assert(isScrollable != null),assert(dragStartBehavior != null),assert(indicator != null ||(indicatorWeight != null && indicatorWeight > 0.0)),assert(indicator != null || (indicatorPadding != null)) {// 普通Tab全部转为自定义Tab,多一层转换是为了统一高度解耦,防止直接传入LogisticsTab自定义高度冲突for (Tab item in tabs) {addTab(item.text, item.icon, item.child);}}@overrideWidget build(BuildContext context) {// 为了添加背景色,又包装了一层,高度都要统一return Material(//这里设置tab的背景色color: Colors.white,child: CustomHeightTabBar(isScrollable: this.isScrollable,indicatorColor: this.indicatorColor,indicatorWeight: this.indicatorWeight,indicator: this.indicator,indicatorSize: this.indicatorSize,indicatorPadding: this.indicatorPadding,labelStyle: this.labelStyle,labelColor: this.labelColor,labelPadding: this.labelPadding,unselectedLabelColor: this.unselectedLabelColor,unselectedLabelStyle: this.unselectedLabelStyle,dragStartBehavior:this.dragStartBehavior,onTap: this.onTap,kTabHeight: this.kTabHeight,kTextAndIconTabHeight: this.kTextAndIconTabHeight,tabs: this._tabs),);}@overrideSize get preferredSize {for (Widget item in _tabs) {if (item is Tab) {final Tab tab = item;if (tab.text != null && tab.icon != null) {return Size.fromHeight(kTextAndIconTabHeight + indicatorWeight);}}}// tab高度加上指示器高度return Size.fromHeight(kTabHeight + indicatorWeight);}
}class CustomHeightTabBar extends TabBar {final double kTabHeight;final double kTextAndIconTabHeight;CustomHeightTabBar({indicatorSize,indicatorColor,controller,isScrollable,indicatorPadding,indicator,double indicatorWeight = 2,tabs,labelStyle,labelColor = Colors.black,labelPadding,unselectedLabelColor,unselectedLabelStyle,onTap,dragStartBehavior,this.kTabHeight = 23,this.kTextAndIconTabHeight = 46}): super(indicatorColor: indicatorColor,controller: controller,isScrollable: isScrollable,indicatorPadding: indicatorPadding,indicatorSize: indicatorSize,indicator: indicator,indicatorWeight: indicatorWeight,tabs: tabs,labelStyle: labelStyle,labelColor: labelColor,labelPadding: labelPadding,unselectedLabelColor: unselectedLabelColor,unselectedLabelStyle: unselectedLabelStyle,onTap: onTap,dragStartBehavior: dragStartBehavior,);// 重写获取定义高度方法@overrideSize get preferredSize {for (Widget item in tabs) {if (item is Tab) {final Tab tab = item;if (tab.text != null && tab.icon != null) {return Size.fromHeight(kTextAndIconTabHeight + indicatorWeight);}}}return Size.fromHeight(kTabHeight + indicatorWeight);}
}class CustomHeightTab extends Tab {final double kTabHeight;final double kTextAndIconTabHeight;final String text;final Icon icon;final Widget child;CustomHeightTab({Key key,this.text,this.icon,this.child,this.kTabHeight = 23,this.kTextAndIconTabHeight = 23}): super(text: text, icon: icon, child: child);Widget _buildLabelText() {return child ?? Text(text, softWrap: false, overflow: TextOverflow.fade);}@overrideWidget build(BuildContext context) {assert(debugCheckHasMaterial(context));double height;Widget label;if (icon == null) {height = kTabHeight;label = _buildLabelText();} else if (text == null && child == null) {height = kTabHeight;label = icon;} else {height = kTextAndIconTabHeight;label = Column(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: <Widget>[Container(child: icon,margin: const EdgeInsets.only(bottom: 10.0),),_buildLabelText(),],);}return SizedBox(height: height,child: Center(child: label,widthFactor: 1.0,),);}
}

flutter 自定义Tabbar高度和背景色相关推荐

  1. Flutter自定义 TabBar

    TabBar常用于放在AppBar中,以标签页的形式展示同一个页面不同内容的主题标签. 常见的属性如下: 1. tabs 标签组: 2. controller 标签控制器: 3. isScrollab ...

  2. Flutter自定义背景色渐变 按钮 组件

    这是笔者写的第一个 Flutter 自定义组件,在这里和大家分享一下,可能稍微有点粗糙,就当学习使用. 首先我们先来看一下效果图: 代码: import 'package:flutter/materi ...

  3. 前端 vue 自定义导航栏组件高度及返回箭头 自定义 tabbar 图标

    前端vue自定义导航栏组件高度及返回箭头 自定义tabbar图标, 下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=12986 喜 ...

  4. Flutter 组件之 Flutter高级自定义TabBar(教程含源码)

    实战需求 Flutter 组件之 Flutter高级自定义TabBar(教程含源码) 本文价值与收获 看完本文后,您将能够作出下面的界面 实战代码 import 'package:flutter/ma ...

  5. Flutter 自定义导航

    上篇文章我们讲到了自定义底部分栏控制器的使用,在开发过程中我们100%会使用到导航,当然Flutter也为我们提供了导航控件,但是自带的往往没有没有那么灵活,所以这个时候就需要我们自定义一个导航,下面 ...

  6. 微信自定义tabbar有小红点_自定义微信小程序tabBar组件上边框的颜色

    背景: 在微信小程序的实际开发过程中,有时候我们需要修改微信小程序提供的 tabBar 组件顶部边框的颜色,以满足项目需求 解决方案: 方式一: 通过tabBar组件自带的 borderStyle 属 ...

  7. Flutter 自定义组件实战之Cupertino(iOS)风格的复选框

    继上一篇Flutter自定义组件的视频短课(视频地址: https://www.bilibili.com/video/BV1ap4y1U7UB/ )后,我们继续来聊自定义组件.视频中我为大家详解了Cu ...

  8. uni-app 使用 Uview2.x 搭建自定义tabbar组件

    先看效果 引言 我的软件需要后续隐藏导航栏等功能,在开发过程中逐渐意识到uni-app原生的tabbar可能不能满足个人的开发要求,而且uView的兼容性是所有非原生组件库中最好的,所以我以uView ...

  9. iOS开发-实现TabBar中间凸起按钮、不规则按钮(自定义TabBar)

    效果: PS:这里需要用到UIView一个分类的一些属性,参考http://blog.csdn.net/doubleface999/article/details/79085764,图标素材等自行上网 ...

最新文章

  1. 不盲目依赖人工智能,海信帮欧尚开了近 300 家无人便利店
  2. 理解 Cinder 架构 - 每天5分钟玩转 OpenStack(45)
  3. AD,proteus操作
  4. Spring BeanDefinition加载
  5. 2021年东港二中高考成绩查询,辽宁省东港二中2021届英语高考模拟试卷1(新课改原创2020版,供2021年课改省份考生使用)...
  6. 【转载】栈溢出原理及实现
  7. vue-cli中的webpack的config配置详细说明
  8. HttpRequest 类
  9. java linq_LINQ和Java
  10. react学习(67)--git 屏蔽文件不被追踪
  11. 在Ubuntu 16.04 安装python3.6 环境并设置为默认
  12. Java中栈,堆,常量池的简单理解
  13. python数据挖掘学习路线图
  14. 经济周期的定义、阶段及特点-宏观经济指标和政策
  15. PDF如何编辑,怎么删除PDF页眉页脚
  16. 如何免费获取一个 Cloudflare 公网 IP
  17. 变限积分求导公式总结_变限积分函数求导以及高阶导数求法的一些总结
  18. 登录计算机隐藏用户名,win10系统隐藏登录界面administrator用户名的办法介绍
  19. K8S 中 Pod 的5种阶段/状态
  20. C语言LCD1602液晶实验报告,LCD1602液晶显示实验报告.doc

热门文章

  1. 【人工智能】给初学者们讲解人工神经网络(ANN)
  2. Python创建并且打开一个mat文件
  3. [sqlite] android create db in SD card
  4. c语言 获取系统版本,[原创]C/C++ 实现获取Windows操作系统版本信息
  5. tensor backward_Pytorch中的backward函数
  6. 苹果今年秋季或发布史上最多新品
  7. 国庆档43.87亿收官:《长津湖》一家独大 影视股喜忧参半
  8. OLED屏智能手机在出货量方面仍未占据主导地位 但预计今年将接近40%
  9. 三星Galaxy S21 Ultra相机再升级:1.08亿主摄+全新传感器夜景更强
  10. 腾讯“狠心”出手了,这款游戏,未成年人每天限玩1.5小时