Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 23485|回复: 0

Flutter中用ListView嵌套GridView报错异常

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-4-7 15:22:11 | 显示全部楼层 |阅读模式

    flutter中的ListView组件和GridView组件都是常用的布局组件,有时候ListView中需要嵌套GridView来使用,例如下图:

    这种情况就需要在ListView里面再嵌套一个GridView用于排放图片等信息,先来看一下GridView一些常用的参数

    GridView.count(
      crossAxisCount: 3,                            // 每行显示多少个
      crossAxisSpacing: 1.5,                        // 横轴网格间距
      mainAxisSpacing: 1.5,                         // 纵轴网格间距
      childAspectRatio: 11/16,                      // 网格比例
    )

    在GridView中的元素无法设置其宽高,主要通过childAspectRatio来设置其比例,通过比例来显示其大小。

    在项目中如果直接使用ListView嵌套GridView进行布局,其会出现报错异常,例如下面异常:

    I/flutter ( 5969): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
    I/flutter ( 5969): The following assertion was thrown during performResize():
    I/flutter ( 5969): Vertical viewport was given unbounded height.
    I/flutter ( 5969): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
    I/flutter ( 5969): viewport was given an unlimited amount of vertical space in which to expand. This situation
    I/flutter ( 5969): typically happens when a scrollable widget is nested inside another scrollable widget.
    I/flutter ( 5969): If this widget is always nested in a scrollable widget there is no need to use a viewport because
    I/flutter ( 5969): there will always be enough vertical space for the children. In this case, consider using a Column
    I/flutter ( 5969): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
    I/flutter ( 5969): the height of the viewport to the sum of the heights of its children.
    

      

    出现这种情况可在GridView中设置shrinkWrap:true即可解决:

    GridView.count(
      shrinkWrap:true,                              // 处理listview嵌套报错
    ),
    

      

    此时有可能出现手指在GridView区域滑动时ListView无法进行滚动,处理该问题可在GridView中设置physics: NeverScrollableScrollPhysics()来处理:

    GridView.count(
      physics: NeverScrollableScrollPhysics(),      // 处理GridView中滑动父级Listview无法滑动
    )
    

      

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-29 09:11 , Processed in 0.087778 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表