当前位置: 首页 > news >正文

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(); } }
http://www.cnnetsun.cn/news/1630.html

相关文章:

  • 180KW 一体式充电桩:基于 STM32F429IGT6 的实现方案
  • 基于COMSOL PDE模块构建裂缝流模型的奇妙之旅
  • 探索艾默生高端变频器 EV6000 源代码的奥秘
  • 汇川 ST 梯形图混合编程:自动印刷机项目实战
  • 基于蛇优化器(SO)的无人机路径规划探索
  • 传送带机械手搬运工件监控系统:博途V16的奇妙之旅
  • 基于FPGA的FOC电流环实现:Verilog编写、SVPWM算法、ADC采样、串口通信、Si...
  • 虚拟同步机(VSG)并网控制在I型NPC三电平逆变器中的实现
  • 28、Linux文件IO与标准IO详解:从概念到实战
  • CANoe调用dll库解锁27服务及制作CDD的奇妙之旅
  • 牛场喂料机监控系统改造:从变量更名到通讯实现
  • COMSOL 实现煤体钻孔周围损伤变形:多场耦合的奇妙探索
  • Comsol水力压裂应力 - 渗流 - 损伤模型:探索地下的奥秘
  • COMSOL光学仿真:液晶分子与超表面共舞调制相位
  • 专项智能练习(课程内容)
  • 封装ElementPlusIcons图标和系统应用内置图片为应用图标
  • COMSOL 实现煤体钻孔周围损伤变形:多场耦合下的深度探索
  • 探索电压源型逆变器死区补偿算法:基于电流矢量的创新之路
  • 纯电动汽车两档ATM变速箱Simulink模型探索
  • VMD - CNN - BiGRU - Attention实现时间序列预测:风速数据集的探索
  • 车辆稳定性相平面MATLAB程序绘制之旅
  • 别再拿旧的那套忽悠老板了!GEO优化得用这三层指标说话
  • 放弃补短板!你的盖洛普“天赋代码”,才是AI时代的终极API
  • 当AI下沉到MCU:嵌入式开发者的“能力护城河”正在被重写
  • 分布式驱动电动汽车路面附着系数估计:UKF与CKF的碰撞与交融
  • 基于组态王和 PLC 的全自动洗衣机系统设计探秘
  • Comsol水力压裂应力 - 渗流 - 损伤模型:探索地下奥秘的有力工具
  • MCGS 昆仑通态触摸屏通讯控制西门子 V20 系列变频器程序探索
  • 迈达斯桥梁建模与分析:探索多样桥梁结构的奥秘
  • 数字孪生:从概念到工业级应用的关键跨越