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

相关文章

Nginx实现高并发的项目实践

《Nginx实现高并发的项目实践》本文主要介绍了Nginx实现高并发的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用最新稳定版本的Nginx合理配置工作进程(workers)配置工作进程连接数(worker_co

python中列表list切分的实现

《python中列表list切分的实现》列表是Python中最常用的数据结构之一,经常需要对列表进行切分操作,本文主要介绍了python中列表list切分的实现,文中通过示例代码介绍的非常详细,对大家... 目录一、列表切片的基本用法1.1 基本切片操作1.2 切片的负索引1.3 切片的省略二、列表切分的高

基于Python实现一个PDF特殊字体提取工具

《基于Python实现一个PDF特殊字体提取工具》在PDF文档处理场景中,我们常常需要针对特定格式的文本内容进行提取分析,本文介绍的PDF特殊字体提取器是一款基于Python开发的桌面应用程序感兴趣的... 目录一、应用背景与功能概述二、技术架构与核心组件2.1 技术选型2.2 系统架构三、核心功能实现解析

Flutter监听当前页面可见与隐藏状态的代码详解

《Flutter监听当前页面可见与隐藏状态的代码详解》文章介绍了如何在Flutter中使用路由观察者来监听应用进入前台或后台状态以及页面的显示和隐藏,并通过代码示例讲解的非常详细,需要的朋友可以参考下... flutter 可以监听 app 进入前台还是后台状态,也可以监听当http://www.cppcn

使用Python实现表格字段智能去重

《使用Python实现表格字段智能去重》在数据分析和处理过程中,数据清洗是一个至关重要的步骤,其中字段去重是一个常见且关键的任务,下面我们看看如何使用Python进行表格字段智能去重吧... 目录一、引言二、数据重复问题的常见场景与影响三、python在数据清洗中的优势四、基于Python的表格字段智能去重

Spring AI集成DeepSeek实现流式输出的操作方法

《SpringAI集成DeepSeek实现流式输出的操作方法》本文介绍了如何在SpringBoot中使用Sse(Server-SentEvents)技术实现流式输出,后端使用SpringMVC中的S... 目录一、后端代码二、前端代码三、运行项目小天有话说题外话参考资料前面一篇文章我们实现了《Spring

Nginx中location实现多条件匹配的方法详解

《Nginx中location实现多条件匹配的方法详解》在Nginx中,location指令用于匹配请求的URI,虽然location本身是基于单一匹配规则的,但可以通过多种方式实现多个条件的匹配逻辑... 目录1. 概述2. 实现多条件匹配的方式2.1 使用多个 location 块2.2 使用正则表达式

使用Apache POI在Java中实现Excel单元格的合并

《使用ApachePOI在Java中实现Excel单元格的合并》在日常工作中,Excel是一个不可或缺的工具,尤其是在处理大量数据时,本文将介绍如何使用ApachePOI库在Java中实现Excel... 目录工具类介绍工具类代码调用示例依赖配置总结在日常工作中,Excel 是一个不可或缺的工http://

SpringBoot实现导出复杂对象到Excel文件

《SpringBoot实现导出复杂对象到Excel文件》这篇文章主要为大家详细介绍了如何使用Hutool和EasyExcel两种方式来实现在SpringBoot项目中导出复杂对象到Excel文件,需要... 在Spring Boot项目中导出复杂对象到Excel文件,可以利用Hutool或EasyExcel

Python如何实现读取csv文件时忽略文件的编码格式

《Python如何实现读取csv文件时忽略文件的编码格式》我们再日常读取csv文件的时候经常会发现csv文件的格式有多种,所以这篇文章为大家介绍了Python如何实现读取csv文件时忽略文件的编码格式... 目录1、背景介绍2、库的安装3、核心代码4、完整代码1、背景介绍我们再日常读取csv文件的时候经常