Flutter仿Boss-5.Lottie实现的Tab切换

2024-04-05 23:12

本文主要是介绍Flutter仿Boss-5.Lottie实现的Tab切换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果

Lottie引入

根据自己项目适配的Flutter版本引入对应的Lottie版本。

lottie: ^3.1.0

实现

  • lottie 文件,直接下载Boss APK,解压出来就可以拿到。
  • 代码:

LottieBottomBarItem

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lottie/lottie.dart';/// Lottie BottomBarItem
class LottieBottomBarItem extends StatefulWidget {// Tab 名字final String tabName;// Tab 图标final String tabIcon;// 默认颜色final Color tabTextColor;// 选中颜色final Color tabTextSelectedColor;// Tab对应索引final int tabIndex;// 点击回调final Function(int) onTap;// 是否选中final bool isChecked;// 角标final int badger;const LottieBottomBarItem({Key? key,required this.tabName,required this.tabIcon,required this.onTap,required this.tabIndex,this.tabTextColor = Colors.grey,this.tabTextSelectedColor = Colors.black,this.isChecked = false,this.badger = 0,}) : super(key: key);@overrideState<LottieBottomBarItem> createState() => _BottomBarItemState();
}class _BottomBarItemState extends State<LottieBottomBarItem>with TickerProviderStateMixin {AnimationController? _animationController;@overridevoid initState() {super.initState();_animationController = AnimationController(vsync: this, duration: const Duration(milliseconds: 500));if (widget.isChecked) {_animationController?.forward();}}@overridevoid didUpdateWidget(covariant LottieBottomBarItem oldWidget) {super.didUpdateWidget(oldWidget);if (!widget.isChecked && oldWidget != widget) {_animationController?.reset();}}@overridevoid dispose() {_animationController?.dispose();super.dispose();}@overrideWidget build(BuildContext context) {return InkWell(child: Stack(alignment: Alignment.bottomCenter,children: [Positioned(child: Column(children: [Lottie.asset(widget.tabIcon,repeat: false,controller: _animationController,width: 35.w,height: 30.w,),Text(widget.tabName,style: TextStyle(color: widget.isChecked? widget.tabTextSelectedColor: widget.tabTextColor,fontSize: 12.sp,),)],),),Visibility(visible: widget.badger > 0,child: Positioned(right: 30.w,top: 10.w,child: ClipOval(child: Container(alignment: Alignment.center,color: Colors.red,width: 8,height: 8,),),),)],),onTap: () {widget.onTap(widget.tabIndex);_animationController?.forward();},);}
}

HomePage


class HomePage extends StatefulWidget with RouteQueryMixin {HomePage({Key? key}) : super(key: key);@overrideState<StatefulWidget> createState() {return HomeState();}
}class HomeState extends PageState<HomePage> with AutomaticKeepAliveClientMixin {final logic = Get.put(HomeLogic());@overridevoid initState() {super.initState();logic.handleCurrentIndex(params: widget.routeParams);}@overrideWidget build(BuildContext context) {return Scaffold(body: _homeBodyWidget(context),);}Widget _homeBodyWidget(BuildContext context) {return Scaffold(body: Stack(alignment: Alignment.bottomCenter,children: <Widget>[// 子布局PageView(controller: logic.pageController,physics: const NeverScrollableScrollPhysics(),children: _bodyContentWidget(),onPageChanged: (index) {logic.state.currentIndex.value = index;},),],),// 底部栏bottomNavigationBar: Obx(() => BottomAppBar(elevation: 5.0,height: 65,child: Row(mainAxisAlignment: MainAxisAlignment.spaceAround,crossAxisAlignment: CrossAxisAlignment.center,children: List.generate(logic.state.homeBottomBar.length,(index) => Expanded(child: LottieBottomBarItem(tabName: logic.state.homeBottomBar[index].tabName,tabIcon: logic.state.homeBottomBar[index].tabIcon,tabIndex: index,onTap: (index) {logic.state.currentIndex.value = index;logic.pageController.jumpToPage(index);},isChecked: logic.state.currentIndex.value == index,),),)),),),);}@overridebool get wantKeepAlive => true;/// 子布局集合List<Widget> _bodyContentWidget() {return logic.state.homeBottomBar.map((item) => item.child).toList();}
}

HomeState

class HomeTab {HomeTab({required this.tabName,required this.tabIcon,required this.child,this.badger = 0,});String tabName;String tabIcon;Widget child;int badger;
}class HomeState {///当前索引RxInt currentIndex = 0.obs;///底部按钮final List<HomeTab> homeBottomBar = [HomeTab(tabName: '职位',tabIcon: 'assets/lottie/tab/zhiwei.json',child: const WorkPage()),HomeTab(tabName: '有了',tabIcon: 'assets/lottie/tab/youle.json',child: const YoulePage()),HomeTab(tabName: '消息',tabIcon: 'assets/lottie/tab/xiaoxi-c.json',child: const MessagePage()),HomeTab(tabName: '我的',tabIcon: 'assets/lottie/tab/wode-c.json',child: const MinePage()),];
}

HomeLogic

class HomeLogic extends GetxController with HttpApi {final HomeState state = HomeState();late PageController pageController;/// 处理tab默认显示索引void handleCurrentIndex({required Map<String, dynamic> params}) {int size = state.homeBottomBar.length;if (params != null) {int tabIndex = params["tabIndex"] ?? 0;// 默认加载页面if (tabIndex >= size) {state.currentIndex.value = size - 1;} else {state.currentIndex.value = tabIndex;}}// 初始化tab控制器pageController = PageController(initialPage: state.currentIndex.value, keepPage: true);}
}

详情:github.com/yixiaolunhui/flutter_project

这篇关于Flutter仿Boss-5.Lottie实现的Tab切换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/878402

相关文章

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核