Flutter-自定义折叠流布局实现

2024-05-26 18:28

本文主要是介绍Flutter-自定义折叠流布局实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

需求

在 Flutter 开发中,常常需要实现自定义布局以满足不同的需求。本文将介绍如何通过自定义组件实现一个折叠流布局,该组件能够显示一系列标签,并且在内容超出一定行数时,可以展开和收起。

效果

该折叠流布局可以显示一组标签,并在标签数量超过指定行数时提供展开和收起功能。初始状态下只显示限定行数的标签,并在最后一个位置显示展开按钮。点击展开按钮可以显示全部标签,再次点击展开按钮可以收起标签。

效果图如下:

实现思路

实现一个自定义的折叠流布局组件需要以下几个步骤:

  1. 创建一个 TagFlowWidget 组件,接受标签数据、最大行数和样式等参数。
  2. TagFlowWidget 组件中使用 Flow 布局,并实现自定义的 FlowDelegate 来处理标签布局和展开收起逻辑。
  3. FlowDelegate 中计算当前显示的行数,根据行数决定是否显示展开或收起按钮。

具体实现包括以下关键部分:

  • TagFlowWidget 组件的构建逻辑
  • 自定义 FlowDelegate 的布局和绘制逻辑
  • 展开和收起状态的管理

实现代码

下面是完整的实现代码,包括 TagFlowWidget 组件和 TagFlowDelegate 代理类。

import 'package:flutter/material.dart';class TagFlowWidget extends StatefulWidget {final List<String> items;final int maxRows;final double spaceHorizontal;final double spaceVertical;final double itemHeight;final Color? itemBgColor;final BorderRadiusGeometry? borderRadius;final double? horizontalPadding;final TextStyle? itemStyle;const TagFlowWidget({Key? key,required this.items,required this.maxRows,required this.itemHeight,this.borderRadius,this.horizontalPadding,this.spaceHorizontal = 0,this.spaceVertical = 0,this.itemBgColor,this.itemStyle,}) : super(key: key);TagFlowWidgetState createState() => TagFlowWidgetState();
}class TagFlowWidgetState extends State<TagFlowWidget> {bool isExpanded = false;Widget build(BuildContext context) {return LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {final maxWidth = constraints.maxWidth;return Column(crossAxisAlignment: CrossAxisAlignment.start,mainAxisSize: MainAxisSize.max,children: [Flow(delegate: TagFlowDelegate(maxRows: widget.maxRows,isExpanded: isExpanded,maxWidth: maxWidth,itemHeight: widget.itemHeight,spaceHorizontal: widget.spaceHorizontal,spaceVertical: widget.spaceVertical,itemCount: widget.items.length,),children: [...widget.items.map((item) {return Container(padding: EdgeInsets.symmetric(horizontal: widget.horizontalPadding ?? 0,),height: widget.itemHeight,decoration: BoxDecoration(color: widget.itemBgColor,borderRadius: widget.borderRadius,),child: Column(mainAxisSize: MainAxisSize.max,crossAxisAlignment: CrossAxisAlignment.center,mainAxisAlignment: MainAxisAlignment.center,children: [Text(item,style: widget.itemStyle,),],));}).toList(),GestureDetector(behavior: HitTestBehavior.opaque,onTap: () {setState(() {isExpanded = !isExpanded;});},child: Container(padding: EdgeInsets.symmetric(horizontal: widget.horizontalPadding ?? 0,),height: widget.itemHeight,decoration: BoxDecoration(color: widget.itemBgColor,borderRadius: widget.borderRadius,),child: Icon(isExpanded? Icons.keyboard_arrow_up: Icons.keyboard_arrow_down,),),),],),],);},);}
}class TagFlowET {static int maxRowCount = 0;
}class TagFlowDelegate extends FlowDelegate {final double maxWidth;final double spaceHorizontal;final double spaceVertical;int maxRows;final bool isExpanded;double height = 0;final double itemHeight;final int itemCount;TagFlowDelegate({required this.maxWidth,required this.itemCount,required this.spaceHorizontal,required this.spaceVertical,required this.maxRows,required this.isExpanded,required this.itemHeight,}) {if (!isExpanded) {TagFlowET.maxRowCount = maxRows;}}void paintChildren(FlowPaintingContext context) {TagFlowET.maxRowCount = _getMaxRowCount(context);if (maxRows >= TagFlowET.maxRowCount) {maxRows = TagFlowET.maxRowCount;}double x = 0;double y = 0;double rowHeight = 0;int rowCount = 1;for (int i = 0; i < context.childCount; i++) {final childSize = context.getChildSize(i)!;final arrowBtnSize = context.getChildSize(context.childCount - 1)!;if (rowCount >= maxRows &&!isExpanded &&(x + childSize.width + arrowBtnSize.width) >= maxWidth) {context.paintChild(context.childCount - 1,transform: Matrix4.translationValues(x, y, 0),);break;}if (x + childSize.width > maxWidth) {x = 0;y += rowHeight + spaceVertical;rowHeight = 0;rowCount++;}if (!(i == context.childCount - 1 && isExpanded && rowCount <= maxRows)) {context.paintChild(i,transform: Matrix4.translationValues(x, y, 0),);}x += childSize.width + spaceHorizontal;rowHeight = childSize.height;}}int _getMaxRowCount(FlowPaintingContext context) {double x = 0;var rowCount = 1;for (int i = 0; i < context.childCount; i++) {final childSize = context.getChildSize(i)!;final arrowSize = context.getChildSize(context.childCount - 1)!;if (x + childSize.width > maxWidth) {x = 0;rowCount++;}if (i == context.childCount - 1 &&(x + childSize.width + arrowSize.width) > maxWidth) {rowCount++;}x += childSize.width + spaceHorizontal;}return rowCount;}Size getSize(BoxConstraints constraints) {final height = (itemHeight * TagFlowET.maxRowCount) +(spaceVertical * (TagFlowET.maxRowCount - 1));return Size(constraints.maxWidth, height);}bool shouldRelayout(covariant FlowDelegate oldDelegate) {return oldDelegate != this ||(oldDelegate as TagFlowDelegate).isExpanded != isExpanded;}bool shouldRepaint(covariant FlowDelegate oldDelegate) {return oldDelegate != this ||(oldDelegate as TagFlowDelegate).isExpanded != isExpanded;}
}

具体使用

import 'package:flutter/material.dart';
import 'package:flutter_xy/widgets/xy_app_bar.dart';
import 'package:flutter_xy/xydemo/flow/tag_flow_view.dart';class TagFlowPage extends StatelessWidget {const TagFlowPage({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {return MaterialApp(home: Scaffold(appBar: XYAppBar(title: '折叠标签',onBack: () {Navigator.pop(context);}),body: Container(padding: const EdgeInsets.all(16.0),child: Column(children: [TagFlowWidget(items: const ['衣服','T恤宽松男','男鞋','香蕉苹果','休闲裤','牛仔裤','红薯','红薯','红薯','红薯','西红柿','更多商品','热销商品','最新商品','特价商品','限时特价商品','限时商品','热门商品',],maxRows: 3,spaceHorizontal: 8,spaceVertical: 8,itemHeight: 30,horizontalPadding: 8,itemBgColor: Colors.lightBlue.withAlpha(30),itemStyle: const TextStyle(height: 1.1),borderRadius: const BorderRadius.all(Radius.circular(8)),),],)),),);}
}

通过上述代码,我们实现了一个自定义的折叠流布局组件 TagFlowWidget,并通过 TagFlowDelegate 来控制标签的布局和展开收起的逻辑。希望这篇文章对你在 Flutter 开发中的自定义布局实现有所帮助。

详情见:github.com/yixiaolunhui/flutter_xy

这篇关于Flutter-自定义折叠流布局实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

Golang如何用gorm实现分页的功能

《Golang如何用gorm实现分页的功能》:本文主要介绍Golang如何用gorm实现分页的功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景go库下载初始化数据【1】建表【2】插入数据【3】查看数据4、代码示例【1】gorm结构体定义【2】分页结构体

在Golang中实现定时任务的几种高效方法

《在Golang中实现定时任务的几种高效方法》本文将详细介绍在Golang中实现定时任务的几种高效方法,包括time包中的Ticker和Timer、第三方库cron的使用,以及基于channel和go... 目录背景介绍目的和范围预期读者文档结构概述术语表核心概念与联系故事引入核心概念解释核心概念之间的关系

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C