当前位置:网站首页>Flutter series part: detailed explanation of GridView layout commonly used in flutter
Flutter series part: detailed explanation of GridView layout commonly used in flutter
2022-06-28 13:01:00 【51CTO】
brief introduction
GridView Is a grid layout , If the subassemblies in the filling process are beyond the scope of the display , that GridView It will scroll automatically .
Because of this scrolling feature , therefore GridView Is a very easy to use Widget. Today, let's explore GridView This layout Secrets of components .
GridView Detailed explanation
GridView Is a scrollable view, That is to say ScrollView, in fact GridView Inherited from BoxScrollView:
And its parent class BoxScrollView, It is inherited from ScrollView:
You can see BoxScrollView Is an abstract class , It has two subclasses , These are what we are going to talk about today GridView And the next issue ListView.
The difference between the two components is GridView It's a 2D Layout , and ListView It's a linear layout Layout .
As BoxScrollView Subclasses of ,GridView Need to achieve buildChildLayout The method is as follows :
here GridView Returned a SliverGrid, This SliverGrid There are two properties in , Namely childrenDelegate and gridDelegate.
among gridDelegate It's a SliverGridDelegate Example , Used to control sub components in GridView Layout in .
childrenDelegate It's a SliverChildDelegate Example , Used to generate GridView The subcomponents in .
These two attributes are GridView In the constructor of , We will explain it in detail next .
GridView Constructor for
GridView There are many constructors , The first is a full - parameter constructor that contains all the parameters :
In this constructor , You need to pass in a custom gridDelegate, So in the constructor gridDelegate yes required state :
It's mentioned above that GridView Two custom attributes in , The other one is childrenDelegate, This attribute is constructed from other parameters passed in , As shown below :
Another one GridView The constructor for is called GridView.builder, The difference between this constructor and the default constructor is childrenDelegate Different implementations of , Let's see GridView.builder in childrenDelegate The implementation of the :
Compared with ,GridView.builder Medium childrenDelegate Two more parameters , Namely itemBuilder and itemCount.
So what are these two parameters used for ?
Consider one that has many chil Of GridView, In order to improve GridView Display performance , We can't take out all of them at once child Element , Instead, it will be dynamically created and drawn in scrolling , And here it is itemCount Namely child Maximum capacity of .
and itemBuilder Is a dynamic creation child Creator of , Thus, dynamic creation child The needs of .
The next constructor is called GridView.custom, Because it's called custom, So this constructor's SliverGridDelegate and SliverChildDelegate It's all customizable , In other words, these two parameters can be passed in from the outside , So these two parameters are both necessary :
GirdView There is also a constructor called GridView.count, there count Refer to GridView You can specify cross axis The number of components that can be included in the , So here gridDelegate It USES one SliverGridDelegateWithFixedCrossAxisCount:
You can set crossAxisCount Value .
the last one GridView The constructor for is called GridView.extent, It and count The constructor for is very similar to , however extent What is provided is a maximum cross-axis extent, Instead of a fixed count value , So here gridDelegate It's a SliverGridDelegateWithMaxCrossAxisExtent object :
How do you understand that ? for instance , If GirdView It is vertical rolling , And its width yes 400 pixels, If this time maxCrossAxisExtent Set to 120, So a row can only have three columns . We can adjust maxCrossAxisExtent Value , To adjust view The display of .
We can select the corresponding constructor according to our needs , So as to meet our different needs .
GridView Use
With GridView Constructor for ,GridView It's easy to use .
For example, we dynamically create a containing image Of child, Form a gridView:
there buildChild Used to generate a containing Widget Of list, As shown below :
Finally, the GridViewApp Put it in Scaffold Of body Run in :
Finally, we can get the following image :

Here we are using GridView.extent Constructors , You can try other constructors by yourself .
summary
GridView Is a component that we often use in our daily work , I hope you can master .
Examples of this article : https://github.com/ddean2009/learn-flutter.git
边栏推荐
- plt.savefig()的用法以及保存路径
- Enterprise source code confidentiality scheme sharing
- ASP. NET CORE Study04
- Stm32f1 and stm32cubeide programming example - matrix keyboard driver
- Mysq 8.0 launched histogram, which greatly improved the performance!
- Siyuan official paid synchronization Guide
- UDP RTP packet frame loss
- 专业英语历年题
- Copy 10 for one article! The top conference papers published in South Korea were exposed to be plagiarized, and the first author was "original sin"?
- arcgis pro 可以实现直连postgresql,编辑图层要素吗
猜你喜欢

Evaluation of IP location query interface I

Online JSON to plaintext tool

Tencent has confirmed that QQ has stolen numbers on a large scale, iphone14 has no chance of type-C, and 5g, the fourth largest operator, has officially released numbers. Today, more big news is here

一款自动生成单元测试的 IDEA 插件,开发效率提升 70% 以上!

Matplotlib_ Study01

Digital twin energy system, creating a "perspective" in the low-carbon era
思源官方付费同步使用指南

Finereport installation tutorial

Fastposter v2.8.4 release e-commerce poster generator

如何在熊市中寻找机会?
随机推荐
BUUCTF:[WUSTCTF2020]朴实无华
codeblocks mingw安装配置问题
词云的可视化设计教程
The Research Report of Analysys' 2022 China Banking privacy computing platform supplier strength matrix analysis' was officially launched
电脑无线网络不显示网络列表应该如何解决
An idea plug-in that automatically generates unit tests, which improves the development efficiency by more than 70%!
Copy 10 for one article! The top conference papers published in South Korea were exposed to be plagiarized, and the first author was "original sin"?
SHAREit实力出众,登陆全球 IAP 实力榜 Top7
align-items 与 align-content 的区别
Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)
百度APP 基于Pipeline as Code的持续集成实践
UDP RTP packet frame loss
. Net hybrid development solution 24 webview2's superior advantages over cefsharp
关于IP定位查询接口的测评Ⅰ
ASP. NET CORE Study11
Vscode如何设置自动保存代码
mysql数据库扫盲,你真的知道什么是数据库嘛
Centos6.5 php+mysql MySQL library not found
腾讯确认QQ大规模盗号,iPhone14无缘Type-C,第四大运营商5G正式放号,今日更多大新闻在此...
How to find opportunities in a bear market?