news 2026/6/23 0:29:27

Flutter---通用子项的图片个数不同(2)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Flutter---通用子项的图片个数不同(2)

效果图

说明:版本1的中心水果图片只是简单的排列,这个版本是让图片进行两两交叠

实现步骤

1.构建网格子项

Widget _buildInAudioItem({ required String name, required List<String> images, //用列表接收图片 required VoidCallback onTap, double imageSize = 40, }) { return Column( mainAxisSize: MainAxisSize.min, children: [ GestureDetector( onTap: onTap, child: Container( height: 150, width: 155, decoration: BoxDecoration( color: Color(0xFF1B1D2E), borderRadius: BorderRadius.circular(10), ), child: Center( child: _buildImageLayout(images, imageSize: imageSize), //传递图片和图片尺寸 ), ), ), SizedBox(height: 8), Container( height: 30, child: Text( name, style: TextStyle(color: Colors.white, fontSize: 12), textAlign: TextAlign.center, maxLines: 2, overflow: TextOverflow.ellipsis, ), ), ], ); }

2.构建图片布局 ******

Widget _buildImageLayout(List<String> images, {double imageSize = 40}) { int count = images.length; //图片个数 if (count == 1) { // 单个图片居中显示 return Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( images[0], width: imageSize * 0.5, height: imageSize * 0.5, ), ), ); } else if (count == 2) { // 两个图片交叠显示 double overlap = imageSize * 0.2; // 交叠宽度 double totalWidth = imageSize * 2 - overlap; return Container( width: totalWidth, child: Stack( children: [ // 第一个图片在左边 Positioned( top: 55, left: 0, child: Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D).withOpacity(0.8), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( images[0], width: imageSize * 0.5, height: imageSize * 0.5, ), ), ), ), // 第二个图片在右边,交叠一部分 Positioned( top: 55, left: imageSize - overlap, child: Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D).withOpacity(0.8), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( images[1], width: imageSize * 0.5, height: imageSize * 0.5, ), ), ), ), ], ), ); } else if (count == 3) { // 三个图片交叠显示 double overlap = imageSize * 0.2; // 交叠宽度 double totalWidth = imageSize + (imageSize - overlap) * 2; return Container( width: totalWidth, child: Stack( children: [ // 第一个图片在最左边 Positioned( top:55, left: 0, child: Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D).withOpacity(0.8), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( images[0], width: imageSize * 0.5, height: imageSize * 0.5, ), ), ), ), // 第二个图片在中间 Positioned( top:55, left: imageSize - overlap, child: Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D).withOpacity(0.8), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( images[1], width: imageSize * 0.5, height: imageSize * 0.5, ), ), ), ), // 第三个图片在最右边 Positioned( top:55, left: (imageSize - overlap) * 2, child: Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D).withOpacity(0.8), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( images[2], width: imageSize * 0.5, height: imageSize * 0.5, ), ), ), ), ], ), ); } return SizedBox.shrink(); } }

3.使用子项构建表格

Widget buildInAudioList() { return Padding( padding: EdgeInsets.symmetric(horizontal: 10, vertical: 40), child: GridView.count( crossAxisCount: 2, crossAxisSpacing: 15, mainAxisSpacing: 5, childAspectRatio: 155 / 180, shrinkWrap: true, children: [ _buildInAudioItem( name: "苹果", images: ["assets/images/apple.png"], onTap: () => print("点击苹果"), ), _buildInAudioItem( name: "苹果+香蕉", images: ["assets/images/apple.png", "assets/images/banana.png"], onTap: () => print("点击苹果+香蕉"), ), _buildInAudioItem( name: "苹果、香蕉、樱桃", images: ["assets/images/apple.png", "assets/images/banana.png", "assets/images/cherry.png"], onTap: () => print("点击苹果、香蕉、樱桃"), ), _buildInAudioItem( name: "苹果", images: ["assets/images/apple.png"], onTap: () => print("苹果"), ), ], ), ); }

代码实例

import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class DemoPage extends StatefulWidget { const DemoPage({super.key}); @override State<DemoPage> createState() => _DemoPageState(); } class _DemoPageState extends State<DemoPage> { @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.transparent, body: Container( width: double.infinity, height: double.infinity, decoration: const BoxDecoration( gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [ Color(0xFF060618), Color(0xFF070A23), ], ), ), child: buildInAudioList(), ), ); } Widget buildInAudioList() { return Padding( padding: EdgeInsets.symmetric(horizontal: 10, vertical: 40), child: GridView.count( crossAxisCount: 2, crossAxisSpacing: 15, mainAxisSpacing: 5, childAspectRatio: 155 / 180, shrinkWrap: true, children: [ _buildInAudioItem( name: "苹果", images: ["assets/images/apple.png"], onTap: () => print("点击苹果"), ), _buildInAudioItem( name: "苹果+香蕉", images: ["assets/images/apple.png", "assets/images/banana.png"], onTap: () => print("点击苹果+香蕉"), ), _buildInAudioItem( name: "苹果、香蕉、樱桃", images: ["assets/images/apple.png", "assets/images/banana.png", "assets/images/cherry.png"], onTap: () => print("点击苹果、香蕉、樱桃"), ), _buildInAudioItem( name: "苹果", images: ["assets/images/apple.png"], onTap: () => print("苹果"), ), ], ), ); } // 网格子项 Widget _buildInAudioItem({ required String name, required List<String> images, //用列表接收图片 required VoidCallback onTap, double imageSize = 40, }) { return Column( mainAxisSize: MainAxisSize.min, children: [ GestureDetector( onTap: onTap, child: Container( height: 150, width: 155, decoration: BoxDecoration( color: Color(0xFF1B1D2E), borderRadius: BorderRadius.circular(10), ), child: Center( child: _buildImageLayout(images, imageSize: imageSize), //传递图片和图片尺寸 ), ), ), SizedBox(height: 8), Container( height: 30, child: Text( name, style: TextStyle(color: Colors.white, fontSize: 12), textAlign: TextAlign.center, maxLines: 2, overflow: TextOverflow.ellipsis, ), ), ], ); } // 构建图片布局 Widget _buildImageLayout(List<String> images, {double imageSize = 40}) { int count = images.length; //图片个数 if (count == 1) { // 单个图片居中显示 return Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( images[0], width: imageSize * 0.5, height: imageSize * 0.5, ), ), ); } else if (count == 2) { // 两个图片交叠显示 double overlap = imageSize * 0.2; // 交叠宽度 double totalWidth = imageSize * 2 - overlap; return Container( width: totalWidth, child: Stack( children: [ // 第一个图片在左边 Positioned( top: 55, left: 0, child: Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D).withOpacity(0.8), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( images[0], width: imageSize * 0.5, height: imageSize * 0.5, ), ), ), ), // 第二个图片在右边,交叠一部分 Positioned( top: 55, left: imageSize - overlap, child: Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D).withOpacity(0.8), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( images[1], width: imageSize * 0.5, height: imageSize * 0.5, ), ), ), ), ], ), ); } else if (count == 3) { // 三个图片交叠显示 double overlap = imageSize * 0.2; // 交叠宽度 double totalWidth = imageSize + (imageSize - overlap) * 2; return Container( width: totalWidth, child: Stack( children: [ // 第一个图片在最左边 Positioned( top:55, left: 0, child: Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D).withOpacity(0.8), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( images[0], width: imageSize * 0.5, height: imageSize * 0.5, ), ), ), ), // 第二个图片在中间 Positioned( top:55, left: imageSize - overlap, child: Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D).withOpacity(0.8), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( images[1], width: imageSize * 0.5, height: imageSize * 0.5, ), ), ), ), // 第三个图片在最右边 Positioned( top:55, left: (imageSize - overlap) * 2, child: Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D).withOpacity(0.8), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( images[2], width: imageSize * 0.5, height: imageSize * 0.5, ), ), ), ), ], ), ); } return SizedBox.shrink(); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/22 22:07:57

180KW 一体式充电桩:基于 STM32F429IGT6 的实现方案

stm32 充电桩方案 180KW一体式充电桩&#xff0c;STM32F429IGT6方案。 包含&#xff1a;原理图&#xff0c;pcb&#xff0c;通信协议&#xff0c;接线图&#xff01;&#xff01;cadence画的看的&#xff01;最近在研究充电桩相关的项目&#xff0c;今天就来和大家分享下基于 …

作者头像 李华
网站建设 2026/6/19 22:18:25

基于COMSOL PDE模块构建裂缝流模型的奇妙之旅

基于COMSOL的PDE模块可以用于建立三维的两相裂缝流模型&#xff0c;通过该模型可以实现对不同注采条件下的饱和度分布进行分析。 利用COMSOL的PDE模块&#xff0c;可以轻松地模拟裂缝在地下储层中的流动情况。 二维在石油工程以及诸多涉及地下流体流动的领域中&#xff0c;理解…

作者头像 李华
网站建设 2026/6/22 22:09:02

探索艾默生高端变频器 EV6000 源代码的奥秘

变频器艾默生高端变频器EV6000源代码&#xff0c;代码是汇编和c混编 EV6000变频器是一款高性能的矢量控制变频器&#xff0c;由艾默生设计并推出。 该产品集合了自主研发的无速度传感器矢量控制算法技术、伺服定位控制技术、EMC技术和可靠性技术的优势&#xff0c;使其在性能上…

作者头像 李华
网站建设 2026/6/22 18:06:32

汇川 ST 梯形图混合编程:自动印刷机项目实战

汇川ST梯形图混合编程巅峰之作分期付款程序技术包含ST结构化 FB功能块&#xff0c;循环变址 结构体变量 数组 框架式程序 汇川easy523系列plc编程&#xff0c;汇川it7系列触摸屏程序 自动印刷机程序&#xff0c;自动清洗&#xff0c;自动印刷&#xff0c;自动烘烤&#xff0c;下…

作者头像 李华
网站建设 2026/6/23 3:21:29

基于蛇优化器(SO)的无人机路径规划探索

基于蛇优化器&#xff08;Snake Optimizer&#xff0c;SO&#xff09;的无人机路径规划 该优化算法是一篇SCI一区论文 具有良好的实际应用和改进意义。 一键运行main函数自动保存高质量图片! 1、蛇优化器 摘要&#xff1a;近年来&#xff0c;一些元启发式算法被引入工程和科学领…

作者头像 李华
网站建设 2026/6/23 18:13:18

传送带机械手搬运工件监控系统:博途V16的奇妙之旅

传送带机械手搬运工件监控系统博途V16带运行效果视频最近在研究自动化控制项目&#xff0c;捣鼓出了一个传送带机械手搬运工件的监控系统&#xff0c;用的是博途V16软件&#xff0c;还录了运行效果视频&#xff0c;和大家分享分享这其中的门道。 项目背景与需求 在现代工业生产…

作者头像 李华