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

Flutter---GridView+自定义控件

概念:GridView 是 Flutter 中用于创建网格布局的滚动组件,类似于表格但支持滚动。

GridView有四种创建方式

①GridView.count - 固定列数

②GridView.extent - 固定最大宽度

③GridView.builder - 动态创建(推荐用于大量数据)

④GridView.custom - 完全自定义

这里主要分享第一种GridView.count

效果图,如果要看滚动的效果,就把数据多复制几次

步骤:

这个项目的点击事件用到了fluttertoast,所以需要导入fluttertoast外部类。

1.在pubspec.yaml中导入依赖(注意空格的缩进)。

dependencies:fluttertoast: ^8.2.2  # 检查最新版本

2.在需要的页面导入这个类

import 'package:fluttertoast/fluttertoast.dart';

3.自定义控件

//自定义控件Widget buildItem({required String icon, //图片1required String title, //文本1required String subtitle,//文本2required List<Color> colors,//颜色1required 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),),//使用传入的文字1Text(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});@overrideState<StatefulWidget> createState() => _HomePageState();}class _HomePageState extends State<HomePage>{//自定义控件Widget buildItem({required String icon, //图片1required String title, //文本1required String subtitle,//文本2required List<Color> colors,//颜色1required 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),),//使用传入的文字1Text(subtitle,style: const TextStyle(color: Colors.white,fontSize: 10),),//使用传入的文字2],),//右侧图标Image.asset(icon,width: 40,height: 40,),//使用传入的图片1],),],),),),),),);}//UI构建@overrideWidget 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: "你点击了三国演义!!!");}),],),),),],),),);}}

http://www.dtcms.com/a/486047.html

相关文章:

  • OJ竞赛平台----C端题目列表
  • 【完整源码+数据集+部署教程】行人和斑马线检测系统源码和数据集:改进yolo11-RFCBAMConv
  • 做海淘的网站做海淘的网站网站建设案例步骤
  • [Zer0pts2020]Can you guess it?
  • Go 通道非阻塞发送:优雅地处理“通道已满”的场景
  • 设计模式【工厂模式和策略模式】
  • 【Go】P6 Golang 基础:流程控制
  • Perl 基础语法
  • 酒店网站模板网站开发好的语言
  • C++入门——多态
  • 用数据绘图(1):用 Highcharts 打造你的数据艺术世界
  • Hadoop面试题及详细答案 110题 (96-105)-- Hadoop性能优化
  • 监控系统理论与实践:从认知到Zabbix入门
  • ROS 传感器模块的通用架构设计与跨中间件扩展实践
  • 措美网站建设游戏网站开发名字
  • openwrt 环境安装
  • iis 发布网站内部服务器错误东莞沙田门户网站建设
  • 订单 API 接口调试常见问题排查:3 类高频问题 + 落地解决方案
  • JavaWeb--使用JDBC操作数据库(一)
  • 【Web开发】待办事项列表
  • Linux IIO研究(二)
  • 浙江建设厅网站那三类人员爱给网官网免费素材
  • Spring Boot整合Apache Shiro权限认证框架(实战篇)
  • Rust 错误处理
  • 【在 Windows 上运行 Apache Hadoop 或 Spark/GeoTrellis 涉及 HDFS 】
  • Linux操作系统-命令行参数及环境变量
  • 系统架构设计师备考第40天——软件可靠性基础
  • RAG 问题处理系统架构解析:企业级智能问答QuestionsProcessor.py的工程实现
  • LlamaIndex多模态RAG开发实现详解
  • springboot实现微信小程序支付(服务商和普通商户模式)