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});

  @override
  State<PopKkCoinGrants> createState() => _PopKkCoinGrantsState();
}

class _PopKkCoinGrantsState extends State<PopKkCoinGrants> {
  @override
  Widget 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 让容器被内容撑满

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肥肥呀呀呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值