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

flutter-GridView使用

先看效果

代码实现

import 'package:app/common/util/k_log_util.dart';
import 'package:app/gen/assets.gen.dart';
import 'package:app/pages/widget/top_appbar.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:timeago/timeago.dart';class PopKkCoinGrants extends StatefulWidget {const PopKkCoinGrants({super.key});@overrideState<PopKkCoinGrants> createState() => _PopKkCoinGrantsState();
}class _PopKkCoinGrantsState extends State<PopKkCoinGrants> {@overrideWidget build(BuildContext context) {return Scaffold(appBar: TopAppBar(context, ""),body: Container(margin: EdgeInsets.all(15.r),child: SingleChildScrollView(child: Column(children: [_topStaticInfo(),_topCountDown(),_pastGrantsListTop(),_pastGrantsList()]),)),);}/// 顶部信息Widget _topStaticInfo() {return Column(children: [Assets.image.kKCoin.image(width: 80.r),SizedBox(height: 11.r,),Text("KKCoin Grants",style: TextStyle(color: Colors.white,fontSize: 16.sp,fontWeight: FontWeight.w500,),textAlign: TextAlign.center,),SizedBox(height: 15.r,),Padding(padding: EdgeInsets.symmetric(horizontal: 5.r),child: Text("There are 10 billion KKcoin tokens, and most will be given to unique humans with a verified Konnect ID.",style: TextStyle(color: const Color(0xff676970),fontSize: 13.sp,fontWeight: FontWeight.w500,height: 1.5,leadingDistribution: TextLeadingDistribution.even,),textAlign: TextAlign.center,),),]);}/// 顶部倒计时Widget _topCountDown() {return Container(decoration: BoxDecoration(color: const Color(0xff25272B),borderRadius: BorderRadius.all(Radius.circular(20.r)),),padding: EdgeInsets.all(25.r),margin: EdgeInsets.only(top: 25.r, bottom: 25.r),child: Row(children: [Assets.image.kKCoin.image(width: 56.r),SizedBox(width: 15.r,),Column(crossAxisAlignment: CrossAxisAlignment.start,children: [Text("Next grant",style: TextStyle(color: const Color(0xff676970),fontWeight: FontWeight.w500,fontSize: 12.sp,),),SizedBox(height: 5.r,),Text("28 Jul",style: TextStyle(color: Colors.white,fontWeight: FontWeight.w500,fontSize: 17.sp,),),],),Expanded(child: Container()),Text("21:52:19",style: TextStyle(color: Colors.white,fontSize: 14.sp,fontWeight: FontWeight.w400,),)]),);}// 列表部分 头Widget _pastGrantsListTop() {return Padding(padding: EdgeInsets.only(bottom: 19.r),child: Row(children: [Text("Past grants",style: TextStyle(color: Colors.white,fontSize: 15.sp,fontWeight: FontWeight.w600,)),Expanded(child: Container()),Text("07/2023",style: TextStyle(color: Color(0xff676970),fontWeight: FontWeight.w500,fontSize: 12.sp,),)]),);}// 列表部分Widget _pastGrantsList() {return GridView.builder(physics: const NeverScrollableScrollPhysics(),shrinkWrap: true,itemCount: 10,gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(//横轴元素个数crossAxisCount: 2,//纵轴间距mainAxisSpacing: 15.r,//横轴间距crossAxisSpacing: 15.r,//子组件宽高长度比例childAspectRatio: 165 / 105,),itemBuilder: (_, position) {KLogUtil.d(position);return _pastGrantsListItem(position / 2 == 0);});}// 列表一项Widget _pastGrantsListItem(bool isPrimary) {TextStyle curStyle = isPrimary ? _textStyleWhite() : _textStyleGray();DecorationImage curBgImage = isPrimary ? _imagePrimary() : _imageGray();return Container(width: 165.r,height: 105.r,decoration: BoxDecoration(borderRadius: BorderRadius.circular(10.r),image: curBgImage,),child: Stack(children: [Positioned(top: 54.r,left: 10.r,child: Row(children: [Text("DATE",style: curStyle,),SizedBox(width: 22.r,),Text("26 / 07 / 2023",style: curStyle,)],),),Positioned(top: 80.r,left: 10.r,child: Row(children: [Text("KKCoin",style: curStyle,),SizedBox(width: 12.r,),Text("256.23",style: curStyle,)],),),]),);}// 白色文字样式TextStyle _textStyleWhite() {return TextStyle(color: Colors.white,fontSize: 11.sp,fontWeight: FontWeight.w400,);}// 灰色的文字样式TextStyle _textStyleGray() {return TextStyle(color: Color(0xff45474D),fontWeight: FontWeight.w500,fontSize: 11.sp,);}// 灰色背景DecorationImage _imageGray() {return DecorationImage(image: Assets.image.pastGrantsGray.provider(),fit: BoxFit.cover,);}// 主色背景DecorationImage _imagePrimary() {return DecorationImage(image: Assets.image.pastGrantsPrimary.provider(),fit: BoxFit.cover,);}
}

关键部分

GridView.builder(physics: const NeverScrollableScrollPhysics(),shrinkWrap: true,itemCount: 10,gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(//横轴元素个数crossAxisCount: 2,//纵轴间距mainAxisSpacing: 15.r,//横轴间距crossAxisSpacing: 15.r,//子组件宽高长度比例childAspectRatio: 165 / 105,),itemBuilder: (_, position) {KLogUtil.d(position);return _pastGrantsListItem(position / 2 == 0);});}

其中physics属性  physics: const NeverScrollableScrollPhysics()会禁止页面滚动

shrinkWrap 让容器被内容撑满

http://www.lryc.cn/news/108514.html

相关文章:

  • Unity Shader编辑器工具类ShaderUtil 常用函数和用法
  • 详解Spring中涉及的技术
  • 阿里云ssl免费数字证书快过期 如何更换
  • 利用OpenCV实现图像拼接
  • 【java安全】无Commons-Collections的Shiro550反序列化利用
  • CSS 滚动条
  • Linux: security: openssh: sshd 出现defunct的一种情况
  • Self-regulating Prompts: Foundational Model Adaptation without Forgetting
  • 平时工资不够用?推荐4种适合工作之余做的兼职副业!
  • 21.Netty源码之编码器
  • Linux 快速创建桌面图标
  • 数据结构—哈夫曼树及其应用
  • NeRF-SLAM: Real-Time Dense Monocular SLAM with Neural Radiance Fields 论文阅读
  • 机器学习之弹性网络(Elastic Net)
  • 嵌入式入门教学——C51
  • 2023-08-03力扣每日一题
  • 【蓝桥杯备考资料】如何进入国赛?
  • QtWebApp开发https服务器,完成客户端与服务器基于ssl的双向认证
  • 动态IP代理的优势展现与应用场景
  • ad+硬件每日学习十个知识点(22)23.8.2(LDO datasheet手册解读)
  • 这可是全网最全的网络工程师零基础实战视频整理,最新版分享
  • 笔记本WIFI连接无网络【实测有效解决方案,不用重启电脑】
  • js 正则表达式配合replace进行过滤html字符串遇到的性能问题
  • 2022牛客寒假算法基础集训营1
  • API对接:构建连接不同系统的技术桥梁
  • 【MySQL】仓储--维护出入库流水、库存,去重数量逻辑修正
  • 用Log4j 2记录日志
  • 【Java面试】Paxos和Raft协议的区别?
  • 手机浏览器H5打开微信小程序支付,自定义传参
  • Aligning Large Language Models with Human: A Survey