当前位置:网站首页>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
边栏推荐
- Summary of 2022 China Merchants fintech competition
- ShareIt has outstanding strength and landed in the top 7 of the global IAP strength list
- Après avoir échoué à l'examen d'entrée à l'Université de technologie de Harbin, vous devez rester à l'Université en tant que « chercheur » après avoir obtenu votre diplôme.
- Xiaobai's e-commerce business is very important to choose the right mall system!
- ASP. NET CORE Study02
- Deep understanding of Bayes theorem
- Google Earth Engine(GEE)——联合国粮农组织全球有机土壤面积(1992-2018年度)
- An idea plug-in that automatically generates unit tests, which improves the development efficiency by more than 70%!
- June 28, 2022 Daily: Lecun's latest paper: the road to autonomous machine intelligence
- 认识启动函数,找到用户入口
猜你喜欢

Matplotlib_ Study01

pytorch模型

scratch旅行相册 电子学会图形化编程scratch等级考试一级真题和答案解析2022年6月

Mysql database literacy, do you really know what a database is

The $980000 SaaS project failed

FH511+TP4333组成一个户外移动电源照明野营灯方案。

ASP. NET CORE Study08

flutter 系列之:flutter 中常用的 GridView layout 详解

How to find opportunities in a bear market?

ASP. NET CORE Study01
随机推荐
完全背包 初学篇「建议收藏」
投资98万美元的Saas项目失败了
易观分析《2022年中国银行业隐私计算平台供应商实力矩阵分析》研究报告正式启动
Vs2012 VC creates a new blank window application
一文搞懂leveldb写操作
Summary of 2022 China Merchants fintech competition
如何在熊市中寻找机会?
k3s一键安装脚本
$100000 AI competition: the task of finding "worse" big models
ASP. NET CORE Study06
Xiaobai's e-commerce business is very important to choose the right mall system!
scratch旅行相册 电子学会图形化编程scratch等级考试一级真题和答案解析2022年6月
Here comes Wi Fi 7. How strong is it?
ASP. NET CORE Study02
pytorch模型调参、训练相关内容
Microservice stability guarantee
I²C、SMBus、PMBus关系
The white paper on the panorama of the digital economy and the digitalization of consumer finance were released
Siyuan official paid synchronization Guide
go template with...end遍历用法