c net做的网站,深圳市seo推广联系方式,广东东莞房价2022最新价格,淄博哪里有做网站的文章目录 概念介绍实现方法示例代码 我们在上一章回中介绍了dart语言中的
setter/getter相关的内容#xff0c;本章回中将介绍
局部动态列表.闲话休提#xff0c;让我们一起Talk Flutter吧。 概念介绍
在正常情况下列表位于整个页面中#xff0c;而且可以在整个页面中滚动… 文章目录 概念介绍实现方法示例代码 我们在上一章回中介绍了dart语言中的
setter/getter相关的内容本章回中将介绍
局部动态列表.闲话休提让我们一起Talk Flutter吧。 概念介绍
在正常情况下列表位于整个页面中而且可以在整个页面中滚动我们在这里说的局部动态列表是指在页面中的某一部分区域中显示一个列表列表的滚动范围只限于这个局部区域我们把这样的列表叫作局部动态列表。在实际项目中这样的场景比较多本章回中将详细介绍如何实现局部动态列表。
实现方法
我们首先使用使用Column组件划分布局然后把ListView当作该组件的子组件这样就创建好了局部动态列表。
这个步骤看着简单不过还有一些细节需要说明不然这样的程序会有编译错误Failed assertion: line 1966 pos 12: hasSize。其在原因是Column要求子组件有固定的大小而子组件ListView没有固定的大小。解决方法有两个详细如下
在ListView外面嵌套一个容器组件比如Container并且指定容器的高度相当于间接指定了ListView的高度。使用shrinkWrap属性该属性默认值为false.将它设置为true.表示让ListView拥有一个可扩展的最大值。相当于指定了ListView的高度。
上面的两种方法都可以不过还有一些细节需要解释详细如下
在使用第一种方法时还有一些特殊情况需要注意为了显示ListView中更多的内容把它外层嵌套的容器高度放大一些这样做会有超过屏幕高度的风险此时可以容器的外层再嵌套一个可滚动的组件比如SingleChildScrollView这样即使显示的内容超过屏幕了也可以滚动显示。在使用第二个方法时主要是不好理解shrinkWrap属性的功能大家可以查看源代码中的注释这个注释写的比较清楚详细如下 /// {template flutter.widgets.scroll_view.shrinkWrap}/// Whether the extent of the scroll view in the [scrollDirection] should be/// determined by the contents being viewed.////// If the scroll view does not shrink wrap, then the scroll view will expand/// to the maximum allowed size in the [scrollDirection]. If the scroll view/// has unbounded constraints in the [scrollDirection], then [shrinkWrap] must/// be true.////// Shrink wrapping the content of the scroll view is significantly more/// expensive than expanding to the maximum allowed size because the content/// can expand and contract during scrolling, which means the size of the/// scroll view needs to be recomputed whenever the scroll position changes.////// Defaults to false.////// {youtube 560 315 https://www.youtube.com/watch?vLUqDNnv_dh0}/// {endtemplate}final bool shrinkWrap;示例代码
介绍完局部动态列表的实现方法后我们通完示例代码来演示详细如下
Column(children: [const Image(width: double.infinity,height: 200,image: AssetImage(images/ex.png),fit:BoxFit.fill, ),const Text(data sample),Container(decoration:BoxDecoration(color: Colors.green,borderRadius: BorderRadius.circular(8)) ,height: 200,child: ListView.builder(///column中嵌套ListView会报hassize类型的的错误即使指定list数量也不行///Failed assertion: line 1966 pos 12: hasSize///解决方法1:在ListView外层嵌套一个容器指定容器大小来限定ListView的大小。///该方法相当于创建了个局部List,容器内的List可以自由滚动,也可以通过physics属性让它不滚动///解决方法2使用shrinkWrap属性itemCount: 10,// physics: const NeverScrollableScrollPhysics(),itemBuilder: (context,index){return const Card(child: Padding(padding: EdgeInsets.all(10),child: Text(List item),),);},),),Container(color: Colors.lightBlue,child: ListView.builder(///用来控制List内部的边距包含head和tail,如果去掉head和tail可以使用only方法padding: const EdgeInsets.all(10),shrinkWrap: true,physics: const NeverScrollableScrollPhysics(),itemCount: 20,itemBuilder:(context,index){return const Card(color: Colors.orange,child: Padding(///这个padding可以控制card的大小也就是listItem的大小padding: EdgeInsets.all(8),child: Text(Another List Item),),);},),),],
),上面的示例代码中通过两个ListView演示了两种解决方法大家可以去掉外层的容器或者修改shrikWrap属性值来看看程序出错时的样子然后按照我们介绍的方法来解决错误这样做的效果比较好建议大家自己动手去实践。
看官们关于局部动态列表相关的内容就介绍到这里欢迎大家在评论区交流与讨论!