当前位置:网站首页>Fluent learning (5) GridView
Fluent learning (5) GridView
2022-07-03 23:04:00 【Sleep in the wind】
flutter Study (5) GridView
Gridview It's grid layout
List of articles
One .GridView Common properties

Two .GridView.count Implement grid layout
Look at this
import 'package:flutter/material.dart'; import './res/listData.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { const MyApp({ Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Flutter list ')), body: LayoutDemo())); } } class LayoutDemo extends StatelessWidget { List<Widget> list = []; // get bulidList => null; List<Widget> _buildList() { for (var i = 0; i < 20; i++) { list.add(ListTile(title: Text(' This is the first $i A text '))); } return list; } @override Widget build(BuildContext context) { return GridView.count( crossAxisCount: 4, // Two children: _buildList()); } }effect

I made a stupid mistake at first
class LayoutDemo extends StatelessWidget { List<Widget> list = []; // get bulidList => null; buildList() { for (var i = 0; i < 20; i++) { list.add(ListTile(title: Text(' This is the first $i A text '))); } } @override Widget build(BuildContext context) { return GridView.count( crossAxisCount: 4, // Two children: list); } }Found that the page is empty
The problem is that buildList Write it there , If not called , He won't run by himself , It's not a construction method
The construction method can drop !
class LayoutDemo extends StatelessWidget { List<Widget> list = []; // get bulidList => null; LayoutDemo() { for (var i = 0; i < 20; i++) { list.add(ListTile(title: Text(' This is the first $i A text '))); } } @override Widget build(BuildContext context) { return GridView.count( crossAxisCount: 6, //6 Column children: list); } }
Put it in Container Inside
class LayoutDemo extends StatelessWidget { List<Widget> list = []; // get bulidList => null; List<Widget> _buildList() { for (var i = 0; i < 20; i++) { list.add(Container( margin: EdgeInsets.all((20)), color: Colors.deepPurpleAccent, alignment: Alignment.center, child: Text(' The first $i Data ', style: TextStyle(color: Colors.cyan, fontSize: 20)))); } return list; } @override Widget build(BuildContext context) { return GridView.count( crossAxisCount: 3, // Two children: _buildList()); } }
The interval above is container Of margin Property implemented
But you can use GridView Own attributes to achieve
Let's start with margin Get rid of
class LayoutDemo extends StatelessWidget { List<Widget> list = []; // get bulidList => null; List<Widget> _buildList() { for (var i = 0; i < 20; i++) { list.add(Container( color: Colors.deepPurpleAccent, alignment: Alignment.center, child: Text(' The first $i Data ', style: TextStyle(color: Colors.cyan, fontSize: 20)))); } return list; } @override Widget build(BuildContext context) { return GridView.count( crossAxisCount: 3, // Two crossAxisSpacing: 20.0, // Right and left mainAxisSpacing: 20.0, padding: EdgeInsets.all(15), children: _buildList()); } }
Grid layout settings Height Invalid , But it can be adjusted Widget Aspect ratio
childAspectRatio: 0.7,
listData Here's the data
class LayoutDemo extends StatelessWidget { List<Widget> list = []; // get bulidList => null; List<Widget> _getListData() { var tempList = listData.map((value) { return Container( child: Column(children: <Widget>[ Image.network(value['imageUrl']), SizedBox( height: 10, ), // Make pictures and texts directly spaced Text(value['title'], textAlign: TextAlign.center, style: TextStyle(fontSize: 20)) ] // hold listData Put the data in the component ), decoration: BoxDecoration(border: Border.all(color: Colors.cyan)), ); //ListView The component adapts to the width , So use Column Here, }); return tempList.toList(); } @override Widget build(BuildContext context) { return GridView.count( crossAxisCount: 2, // Two crossAxisSpacing: 10.0, // Right and left mainAxisSpacing: 80.0, // Up and down spacing padding: EdgeInsets.all(15), children: _getListData()); } }
3、 ... and .GridView.builder Implement grid layout
import 'package:flutter/material.dart';
import './res/listData.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Flutter list ')), body: LayoutDemo()));
}
}
class LayoutDemo extends StatelessWidget {
List<Widget> list = [];
// get bulidList => null;
Widget _getListData(context, index) {
return Container(
child: Column(children: <Widget>[
Image.network(listData[index]['imageUrl']),
SizedBox(
height: 10,
), // Make pictures and texts directly spaced
Text(listData[index]['title'],
textAlign: TextAlign.center, style: TextStyle(fontSize: 20))
]
// hold listData Put the data in the component
),
decoration: BoxDecoration(border: Border.all(color: Colors.cyan)),
); //ListView The component adapts to the width , So use Column Here,
}
@override
Widget build(BuildContext context) {
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
mainAxisSpacing: 10.0,
crossAxisCount: 2,
),
itemCount: listData.length,
itemBuilder: _getListData,
);
}
}

边栏推荐
- Hcip day 16 notes
- How can enterprises and developers take advantage of the explosion of cloud native landing?
- [golang] leetcode intermediate - alphabetic combination of island number and phone number
- Gorilla/mux framework (RK boot): add tracing Middleware
- Common problems in multi-threaded learning (I) ArrayList under high concurrency and weird hasmap under concurrency
- finalize finalization finally final
- AST (Abstract Syntax Tree)
- In VS_ In 2019, scanf and other functions are used to prompt the error of unsafe functions
- C deep anatomy - the concept of keywords and variables # dry inventory #
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
猜你喜欢

Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?

Ppt image processing
![[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)](/img/b8/e2a59772d009b6ee262fb4807f2cd2.jpg)
[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)

Es6~es12 knowledge sorting and summary

The 2022 global software R & D technology conference was released, and world-class masters such as Turing prize winners attended

Pat grade A - 1164 good in C (20 points)

Scratch uses runner Py run or debug crawler

2022.02.13

How to solve the problem of requiring a password when accessing your network neighborhood on your computer

Harbor integrated LDAP authentication
随机推荐
Yyds dry goods inventory hands-on teach you to create a jigsaw puzzle using the canvasapi
Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
STM32 multi serial port implementation of printf -- Based on cubemx
Unsafe and CAS principle
MLX90614 driver, function introduction and PEC verification
[actual combat record] record the whole process of the server being attacked (redis vulnerability)
X Opencv feature point detection and matching
Is the controller a single instance or multiple instances? How to ensure the safety of concurrency
4 environment construction -standalone ha
Yyds dry goods inventory Spring Festival "make" your own fireworks
Learning notes of raspberry pie 4B - IO communication (SPI)
Classification and extension of OC
Text replacement demo
2.14 summary
Teach you to easily learn the type of data stored in the database (a must see for getting started with the database)
Summary of basic knowledge of exception handling
Leetcode: a single element in an ordered array
[Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)
FPGA tutorial and Allegro tutorial - link