news 2026/3/3 23:20:50

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

作者头像

张小明

前端开发工程师

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

效果图

实现步骤

1.构建网格通用子项

Widget _buildInAudioItem({ required String name, required List<String> images, //关键:使用列表存储图片 required VoidCallback onTap, double imageSize = 40, }){ return Column( 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), Text(name,style: TextStyle(color: Colors.white,fontSize: 12),), ], ); }

2.构建图片的布局,根据用户传递的图片个数传递不同的UI格式

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) { // 两个图片并排 return Row( mainAxisAlignment: MainAxisAlignment.center, children: [ _buildSingleImage(images[0], imageSize: imageSize), SizedBox(width: 5,), _buildSingleImage(images[1], imageSize: imageSize), ], ); } else if (count == 3) { // 三个或以上图片,显示前三个 return Row( mainAxisAlignment: MainAxisAlignment.center, children: [ _buildSingleImage(images[0], imageSize: imageSize), SizedBox(width: 5,), _buildSingleImage(images[1], imageSize: imageSize), SizedBox(width: 5,), _buildSingleImage(images[2], imageSize: imageSize), ], ); } return SizedBox.shrink(); }

3.构建单个图片的组件:用圆包裹传递过来的图片

Widget _buildSingleImage(String imagePath, {double imageSize = 40}) { return Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( imagePath, width: imageSize * 0.5, height: imageSize * 0.5, ), ), ); }

4.使用网格列表构建整个列表UI

Widget buildInAudioList() { return Expanded( child: Padding( padding: EdgeInsets.symmetric(horizontal: 10,vertical: 40), child: GridView.count( crossAxisCount: 2,//固定列数 crossAxisSpacing: 15,//列间距 mainAxisSpacing: 20,//行间距 childAspectRatio: 155/170,//子项的宽高比 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("点击苹果"), ), ], ), ), ); }

5.整个背景图

@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: Expanded( // 使用Expanded包裹GridView child: buildInAudioList(), ), ), ); }

代码实例

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: Expanded( // 使用Expanded包裹GridView child: buildInAudioList(), ), ), ); } Widget buildInAudioList() { return Expanded( child: Padding( padding: EdgeInsets.symmetric(horizontal: 10,vertical: 40), child: GridView.count( crossAxisCount: 2,//固定列数 crossAxisSpacing: 15,//列间距 mainAxisSpacing: 20,//行间距 childAspectRatio: 155/170,//子项的宽高比 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( 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), Text(name,style: TextStyle(color: Colors.white,fontSize: 12),), ], ); } // 构建图片布局 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) { // 两个图片并排 return Row( mainAxisAlignment: MainAxisAlignment.center, children: [ _buildSingleImage(images[0], imageSize: imageSize), SizedBox(width: 5,), _buildSingleImage(images[1], imageSize: imageSize), ], ); } else if (count == 3) { // 三个或以上图片,显示前三个 return Row( mainAxisAlignment: MainAxisAlignment.center, children: [ _buildSingleImage(images[0], imageSize: imageSize), SizedBox(width: 5,), _buildSingleImage(images[1], imageSize: imageSize), SizedBox(width: 5,), _buildSingleImage(images[2], imageSize: imageSize), ], ); } return SizedBox.shrink(); } // 单个图片组件 Widget _buildSingleImage(String imagePath, {double imageSize = 40}) { return Container( height: imageSize, width: imageSize, decoration: BoxDecoration( color: Color(0xFF55596D), borderRadius: BorderRadius.circular(imageSize / 2), ), child: Center( child: Image.asset( imagePath, width: imageSize * 0.5, height: imageSize * 0.5, ), ), ); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/3 13:49:53

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

效果图说明&#xff1a;版本1的中心水果图片只是简单的排列&#xff0c;这个版本是让图片进行两两交叠实现步骤1.构建网格子项Widget _buildInAudioItem({required String name,required List<String> images, //用列表接收图片required VoidCallback onTap,double image…

作者头像 李华
网站建设 2026/3/3 9:51:29

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

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

作者头像 李华
网站建设 2026/3/4 1:22:50

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

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

作者头像 李华
网站建设 2026/3/4 19:11:01

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

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

作者头像 李华
网站建设 2026/3/4 11:17:40

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

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

作者头像 李华
网站建设 2026/3/4 11:16:24

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

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

作者头像 李华