Flutter---GridView四大名著项目

发布时间:2026/7/17 16:30:02

Flutter---GridView四大名著项目
效果图原理使用GridView.count实现实现步骤这个项目的点击事件用到了fluttertoast所以需要导入fluttertoast外部类。1.在pubspec.yaml中导入依赖注意空格的缩进。dependencies: fluttertoast: ^8.2.2 # 检查最新版本2.在需要的页面导入这个类import package:fluttertoast/fluttertoast.dart;3.自定义控件//自定义控件 Widget buildItem({ required String icon, //图片1 required String title, //文本1 required String subtitle,//文本2 required ListColor colors,//颜色1 required VoidCallback callback,//VoidCallback:无参数无返回值的函数类型 }){ return Container( //容器基础样式 height: 85, width: 155, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10),//圆角 gradient: LinearGradient(//垂直渐变 begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: colors ), ), //使用Material Ink InkWell 组合 点击波纹效果 //点击效果实现 child: Material( //提供材质 color: Colors.transparent, child: Ink(//墨水效果容器 child: InkWell(//实际产生波纹效果的组件 borderRadius: BorderRadius.circular(10), onTap: callback,//点击时传入的回调函数 //内部布局结构 child: Padding( padding: const EdgeInsets.only(left: 15,top: 12.5,bottom:10,right:15), child: Column( crossAxisAlignment: CrossAxisAlignment.start,//子组件左对齐 children: [ const Expanded(child: SizedBox()),//弹性撑开空间Expanded会占据所有可用空间将后面的Row推向底部 Row( mainAxisAlignment: MainAxisAlignment.spaceBetween,//两端对齐 children: [ //左侧文本列 Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(title,style: const TextStyle(fontSize:18,fontWeight: FontWeight.bold),),//使用传入的文字1 Text(subtitle,style: const TextStyle(color: Colors.white,fontSize: 10),),//使用传入的文字2 ], ), //右侧图标 Image.asset(icon,width: 40,height: 40,),//使用传入的图片1 ], ), ], ), ), ), ), ), ); }自定义框架的整体布局层级Padding (内边距)└── Column (垂直布局)└── Expanded (弹性空间)└── Row (水平布局)├── Column (文本列)│ ├── Text (主标题)│ └── Text (副标题)└── Image.asset (图标)4.在UI中使用自定义控件return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ Expanded( child: Padding( padding: EdgeInsets.symmetric(horizontal: 10), child: GridView.count( crossAxisCount: 2,//固定列数 crossAxisSpacing: 8,//列间距 mainAxisSpacing: 8,//行间距 childAspectRatio: 1.823,//子项的宽高比 children: [ buildItem( icon: assets/images/apple.png, title: 红楼梦, subtitle: 作者曹雪芹, colors: const [Color(0xFF8EE6FE),Color(0xFF2BBDE7)], callback: (){ Fluttertoast.showToast(msg: 你点击了红楼梦); } ), buildItem( icon: assets/images/banana.png, title: 西游戏, subtitle: 作者吴承恩, colors: const [Color(0xFFFFCC91),Color(0xFFFF8a65)], callback: (){ Fluttertoast.showToast(msg: 你点击了西游记); } ), buildItem( icon: assets/images/cherry.png, title: 水浒传, subtitle: 作者施耐庵, colors: const [Color(0xFF77FA76),Color(0xFF31F0A3)], callback: (){ print(点击事件触发); Fluttertoast.showToast( msg: 你点击了水浒传, ); } ), buildItem( icon: assets/images/mango.png, title: 三国演义, subtitle: 作者罗贯中, colors: const [Color(0xFFCC9EF7),Color(0xFFA973F0)], callback: (){ Fluttertoast.showToast(msg: 你点击了三国演义); } ), ], ), ), ), ], ), ), );点击效果的流程用户点击 → InkWell检测点击 → 产生波纹动画 → 执行callback函数性能优化这个只能做短列表因为每一个项都需要加入一个列表子项有没有方法优化这个问题呢代码实例import package:flutter/cupertino.dart; import package:flutter/material.dart; import package:fluttertoast/fluttertoast.dart; class HomePage extends StatefulWidget{ const HomePage({super.key}); override StateStatefulWidget createState() _HomePageState(); } class _HomePageState extends StateHomePage{ //自定义控件 Widget buildItem({ required String icon, //图片1 required String title, //文本1 required String subtitle,//文本2 required ListColor colors,//颜色1 required VoidCallback callback,//VoidCallback:无参数无返回值的函数类型 }){ return Container( //容器基础样式 height: 85, width: 155, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10),//圆角 gradient: LinearGradient(//垂直渐变 begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: colors ), ), //使用Material Ink InkWell 组合 点击波纹效果 //点击效果实现 child: Material( //提供材质 color: Colors.transparent, child: Ink(//墨水效果容器 child: InkWell(//实际产生波纹效果的组件 borderRadius: BorderRadius.circular(10), onTap: callback,//点击时传入的回调函数 //内部布局结构 child: Padding( padding: const EdgeInsets.only(left: 15,top: 12.5,bottom:10,right:15), child: Column( crossAxisAlignment: CrossAxisAlignment.start,//子组件左对齐 children: [ const Expanded(child: SizedBox()),//弹性撑开空间Expanded会占据所有可用空间将后面的Row推向底部 Row( mainAxisAlignment: MainAxisAlignment.spaceBetween,//两端对齐 children: [ //左侧文本列 Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(title,style: const TextStyle(fontSize:18,fontWeight: FontWeight.bold),),//使用传入的文字1 Text(subtitle,style: const TextStyle(color: Colors.white,fontSize: 10),),//使用传入的文字2 ], ), //右侧图标 Image.asset(icon,width: 40,height: 40,),//使用传入的图片1 ], ), ], ), ), ), ), ), ); } //UI构建 override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ Expanded( child: Padding( padding: EdgeInsets.symmetric(horizontal: 10), child: GridView.count( crossAxisCount: 2,//固定列数 crossAxisSpacing: 8,//列间距 mainAxisSpacing: 8,//行间距 childAspectRatio: 1.823,//子项的宽高比 children: [ buildItem( icon: assets/images/apple.png, title: 红楼梦, subtitle: 作者曹雪芹, colors: const [Color(0xFF8EE6FE),Color(0xFF2BBDE7)], callback: (){ Fluttertoast.showToast(msg: 你点击了红楼梦); } ), buildItem( icon: assets/images/banana.png, title: 西游戏, subtitle: 作者吴承恩, colors: const [Color(0xFFFFCC91),Color(0xFFFF8a65)], callback: (){ Fluttertoast.showToast(msg: 你点击了西游记); } ), buildItem( icon: assets/images/cherry.png, title: 水浒传, subtitle: 作者施耐庵, colors: const [Color(0xFF77FA76),Color(0xFF31F0A3)], callback: (){ print(点击事件触发); Fluttertoast.showToast( msg: 你点击了水浒传, ); } ), buildItem( icon: assets/images/mango.png, title: 三国演义, subtitle: 作者罗贯中, colors: const [Color(0xFFCC9EF7),Color(0xFFA973F0)], callback: (){ Fluttertoast.showToast(msg: 你点击了三国演义); } ), ], ), ), ), ], ), ), ); } }

相关新闻

Java毕业设计-基于 SpringBoot 的大学生运动会管理系统的设计与实现 基于 SpringBoot+Vue 的高校运动会赛事管理系统(源码+LW+部署文档+全bao+远程调试+代码讲解等)

Java毕业设计-基于 SpringBoot 的大学生运动会管理系统的设计与实现 基于 SpringBoot+Vue 的高校运动会赛事管理系统(源码+LW+部署文档+全bao+远程调试+代码讲解等)

2026/7/7 5:26:19

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

VeLoCity皮肤:VLC播放器的终极美化指南,让你的播放器焕然一新 [特殊字符]

VeLoCity皮肤:VLC播放器的终极美化指南,让你的播放器焕然一新 [特殊字符]

2026/7/7 5:26:19

VeLoCity皮肤:VLC播放器的终极美化指南,让你的播放器焕然一新 🎵 【免费下载链接】VeLoCity-Skin-for-VLC Castom skin for VLC Player 项目地址: https://gitcode.com/gh_mirrors/ve/VeLoCity-Skin-for-VLC 想让你的VLC播放器拥有更美…

Milvus-03-向量是怎么来的

Milvus-03-向量是怎么来的

2026/7/7 5:26:19

假设有一个词“苹果”,它的向量是[0.9, 0.9],那么这个[0.9, 0.9]是怎么来的呢?? 这其实是大模型自己算出来的,模型内部是一个的神经网络,里面有几亿个参数(权重)。这些权重是通过大…

重塑地图创作:探索Tiled自动化映射的智能设计哲学

重塑地图创作:探索Tiled自动化映射的智能设计哲学

2026/7/17 16:21:40

重塑地图创作:探索Tiled自动化映射的智能设计哲学 【免费下载链接】tiled Flexible level editor 项目地址: https://gitcode.com/gh_mirrors/ti/tiled 你是否曾为游戏地图中重复的地形绘制而疲惫?当每一个悬崖边缘、每一片草地纹理都需要手动摆放…

FlipIt翻页时钟屏保:3个简单步骤打造你的优雅桌面时间显示器

FlipIt翻页时钟屏保:3个简单步骤打造你的优雅桌面时间显示器

2026/7/17 16:21:40

FlipIt翻页时钟屏保:3个简单步骤打造你的优雅桌面时间显示器 【免费下载链接】FlipIt Flip Clock screensaver 项目地址: https://gitcode.com/gh_mirrors/fl/FlipIt 你是否厌倦了Windows系统自带的单调屏保?是否想要在电脑闲置时,让屏…

WuWa-Mod终极指南:5分钟解锁鸣潮游戏无限可能

WuWa-Mod终极指南:5分钟解锁鸣潮游戏无限可能

2026/7/17 16:21:40

WuWa-Mod终极指南:5分钟解锁鸣潮游戏无限可能 【免费下载链接】wuwa-mod Wuthering Waves pak mods 项目地址: https://gitcode.com/GitHub_Trending/wu/wuwa-mod 还在为鸣潮游戏中的技能冷却、体力限制、资源收集而烦恼吗?WuWa-Mod为你提供了一套…

Nova调度过滤器完整介绍:CoreFilter、RamFilter(MemoryFilter)资源过滤机制

Nova调度过滤器完整介绍:CoreFilter、RamFilter(MemoryFilter)资源过滤机制

2026/7/17 16:21:40

Nova默认调度器FilterScheduler依靠多层过滤器层层剔除不满足条件的计算节点,核心资源过滤器为CoreFilter(CPU核数校验)、RamFilter(即MemoryFilter内存校验),同时配套磁盘、可用域、服务状态、主机聚合、亲…

Arch Linux技术解析:从DIY哲学到社区文化

Arch Linux技术解析:从DIY哲学到社区文化

2026/7/17 16:21:40

1. Arch Linux 的江湖地位与技术本质"我用的是 Arch BTW"这句在Linux社区广为流传的梗,本质上反映了一个技术圈特有的文化现象。作为从2002年发展至今的元老级发行版,Arch Linux以其独特的"DIY哲学"在众多Linux发行版中占据特殊地位…

量化软件总成本计算器:数据、服务器、维护时间与换手成本

量化软件总成本计算器:数据、服务器、维护时间与换手成本

2026/7/17 16:11:39

量化软件推荐常把注意力集中于订阅费,实际总成本还包括数据、服务器、环境维护和策略换手。牛股王股票这类量化辅助软件适合不想长期维护Python环境的普通投资者;聚宽提供在线研究路径,省去部分本地环境管理;QMT进入券商侧后&…

Unity游戏文本翻译架构深度解析:XUnity.AutoTranslator的技术实现与工程实践

Unity游戏文本翻译架构深度解析:XUnity.AutoTranslator的技术实现与工程实践

2026/7/17 10:50:47

Unity游戏文本翻译架构深度解析:XUnity.AutoTranslator的技术实现与工程实践 【免费下载链接】XUnity.AutoTranslator 项目地址: https://gitcode.com/gh_mirrors/xu/XUnity.AutoTranslator XUnity.AutoTranslator作为Unity游戏社区中最成熟的文本翻译解决方…

openEuler Raspberry Pi Kernel设备驱动开发指南:为树莓派硬件添加支持

openEuler Raspberry Pi Kernel设备驱动开发指南:为树莓派硬件添加支持

2026/7/16 14:29:31

openEuler Raspberry Pi Kernel设备驱动开发指南:为树莓派硬件添加支持 【免费下载链接】raspberrypi-kernel It provides openEuler kernel source for Raspberry Pi 项目地址: https://gitcode.com/openeuler/raspberrypi-kernel 前往项目官网免费下载&…

openEuler系统集成测试实战:基于smoke-test套件的环境验证技巧

openEuler系统集成测试实战:基于smoke-test套件的环境验证技巧

2026/7/16 14:18:17

openEuler系统集成测试实战:基于smoke-test套件的环境验证技巧 【免费下载链接】integration-test The repo contains test suits for system integration test 项目地址: https://gitcode.com/openeuler/integration-test 前往项目官网免费下载:…

Cursor终端插件生态避坑指南:23个实测低效插件黑名单,附3个自研轻量替代方案

Cursor终端插件生态避坑指南:23个实测低效插件黑名单,附3个自研轻量替代方案

2026/7/17 0:00:57

更多请点击: https://intelliparadigm.com 第一章:Cursor终端插件生态避坑指南概览 Cursor 作为基于 VS Code 内核构建的 AI 原生编辑器,其终端插件生态虽活跃,但存在兼容性断层、权限策略突变与调试链路断裂等典型风险。开发者常…

ChatGPT写作提示词效率革命:单条提示词响应质量提升3.8倍的关键变量(附可复用提示词矩阵表)

ChatGPT写作提示词效率革命:单条提示词响应质量提升3.8倍的关键变量(附可复用提示词矩阵表)

2026/7/17 0:00:57

更多请点击: https://codechina.net 第一章:ChatGPT写作提示词效率革命:单条提示词响应质量提升3.8倍的关键变量(附可复用提示词矩阵表) 提示词工程已从经验试错迈入变量驱动的科学阶段。实证研究表明,影响…

DeepSeek V4替换Codex底座模型的实践与优化

DeepSeek V4替换Codex底座模型的实践与优化

2026/7/17 0:00:57

1. 为什么选择用DeepSeek V4替换Codex的底座模型去年我在开发一个智能代码补全工具时,发现Codex的默认底座模型在复杂业务逻辑场景下表现不尽如人意。经过多次测试对比,DeepSeek V4在以下几个关键指标上展现出明显优势:代码补全准确率&#x…