This is the 8th day of my participation in Gwen Challenge
preface
In the previous article we talked about 4 ways to build Flutter, the most commonly used list component, ListView. In this article we will talk about 5 ways to build A GridView, 2 layouts and 2 subitems. See the brain map below at 👇🏻 to see the implementation and effect step by step.
- Collectively referred to as
522 ways
Ha, ha, ha,
The first –GridView
structure
Look at the effect
Look at the source code
One of the benefits of Flutter is that you can always view the well-commented source code. Once you have read the source code, you will know the implementation. Here is a brief overview of the core content.
We just have to focus on thatChildren, gridDelegate, childrenDelegate
These three properties, and this one right hereSliverChildListDelegate
We’ll talk a little bit about that, but here’s an idea of what we’re passing onchildren
Pass to theSliverChildListDelegate
So it must be the one that finally gets through here, so let’s keep going.
Here,buildChildLayout
Does that sound familiar? Last chatListView
The final layout of the subitems is implemented through this method.GridView
Similarly, the differences are as follows:
The name of the | BuildChildLayout difference |
---|---|
ListView | 1. Build with SliverList 2. There is only one delegate parameter |
GridView | 1. Build with SliverGrid 2. There are two parameters: delegate and gridDelegate |
How to use it?
It’s easy to use, just build a List
passed to children and build a gridDelegate.
GridView(
// A fixed number of sub-axis GridDelegate
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
// Set the quantity to 3
crossAxisCount: 3.// Spindle spacing
mainAxisSpacing: 6.// Secondary axis spacing
crossAxisSpacing: 12.// Width to height ratio of subitems
childAspectRatio: 4 / 3,),// Build subitems that can be viewed back to the ListView article
children: List.generate(100, (index) {
returngetItem(index); }),)Copy the code
Vertical screen | landscape |
From the above comparison, we can see the following features regardless of screen width:
- All keep the subterms on the subaxis
The quantity is fixed
The maximum (width) range is not fixed - Keep the spacing the same
- Same aspect to height ratio of subterm
The getItem method implementation
This is simply an Image. If you don’t understand, check out the ListView or its description of Image in my Flutter Widgets column
/// Get subitems
Widget getItem(int index) {
var item = listData[index % 5];
return Image.network(
item.url,
fit: BoxFit.cover,
);
}
Copy the code
The second –GridView.builder
build
Here we use an itemBuilder to build a set of subitems, a gridDelegate. Here we use a gridDelegate with a maximum sub-axis “range”. You can adjust the maximum range value to see how it looks
GridView.builder(
// GridDelegate for the maximum sub-axis "range"
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
// Maximum secondary axis width (or height)
maxCrossAxisExtent: 140.// Spindle spacing
mainAxisSpacing: 6.// Secondary axis spacing
crossAxisSpacing: 12.// Width to height ratio of subitems
childAspectRatio: 16 / 9,
),
itemBuilder: (context, index) {
return getItem(index);
},
// Number of subitems
itemCount: 100.)Copy the code
Contrast effects
Vertical screen | landscape |
From the above comparison, we can see the following features regardless of screen width:
- All keep the subterms on the subaxis
The maximum range is fixed
The number is not fixed - Keep the spacing the same
- Same aspect to height ratio of subterm
Here we also compare the SliverGridDelegateWithFixedCrossAxisCount and SliverGridDelegateWithMaxCrossAxisExtent the difference between the two GridDelegate, The former has a fixed number of ranges, while the latter has a fixed range.
The third –GridView.count
build
GridView.count(
crossAxisCount: 6,
mainAxisSpacing: 6,
crossAxisSpacing: 12,
childAspectRatio: 4 / 3,
children: List.generate(100, (index) {
returngetItem(index); }),)Copy the code
I don’t need to say any more about this, but it isSliverGridDelegateWithFixedCrossAxisCount
Package bai, let’s look at the source code is not
As expected, it was packaged for our convenience.
The fourth –GridView.extent
build
GridView.extent(
// Maximum secondary axis range
maxCrossAxisExtent: 140,
mainAxisSpacing: 6,
crossAxisSpacing: 12,
childAspectRatio: 16 / 9,
children: List.generate(100, (index) {
returngetItem(index); }),)Copy the code
You can infer from this that this is rightSliverGridDelegateWithMaxCrossAxisExtent
Package, take a look at the source code verification
As expected, as we expected, look at the effect
The fifth –GridView.custom
build
The last way to look at the name is to let us customize, but the main one is to customize gridDelegate and childrenDelegate
GridView.custom(
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 140,
mainAxisSpacing: 6,
crossAxisSpacing: 12,
childAspectRatio: 16 / 9,
),
childrenDelegate: SliverChildListDelegate(
List.generate(100, (index) {
returngetItem(index); }),),)Copy the code
Here we review the common constructor classes that summarize these two parameters
The parameter name | Common build classes and instructions |
---|---|
gridDelegate | Axion SliverGridDelegateWithMaxCrossAxisExtent (time range fixed) SliverGridDelegateWithFixedCrossAxisCount (the axis fixed number) |
childrenDelegate | SliverChildListDelegate (list build) SliverChildBuilderDelegate (Builder build) |
Late at night more text is not easy, if there is help please click 👍🏻 to give the biggest support
Source warehouse
Based on the latest version of Flutter 🔥
- Flutter Widgets warehouse
Refer to the link
- GridView (Flutter Widget of the Week)
- Flutter-GridView
Pay attention to column
- This article has been included in the column at 👇 below, you can follow it directly
- Read more | The series continues to be updated
👏 welcome to like ➕ collect ➕ pay attention, please feel free to comment on 👇 if you have any questions, I will reply as soon as possible