当前位置:网站首页>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,
);
}
}

边栏推荐
- [Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)
- ADB command to get XML
- Learning methods of zynq
- How can enterprises and developers take advantage of the explosion of cloud native landing?
- QGIS grid processing DEM data reclassification
- How about opening an account at Hengtai securities? Is it safe?
- pycuda._ driver. LogicError: explicit_ context_ dependent failed: invalid device context - no currently
- BUUCTF,Misc:LSB
- 在恒泰证券开户怎么样?安全吗?
- Exness: the Central Bank of England will raise interest rates again in March, and inflation is coming
猜你喜欢

2022.02.14

Pyqt5 sensitive word detection tool production, operator's Gospel

Flutter internationalized Intl

How can enterprises and developers take advantage of the explosion of cloud native landing?

Shell script three swordsman awk

Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place

A preliminary study on the middleware of script Downloader

Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities

Blue Bridge Cup -- Mason prime

webAssembly
随机推荐
Runtime. getRuntime(). totalMemory/maxMemory()
Ppt image processing
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
Yyds dry goods inventory hands-on teach you to create a jigsaw puzzle using the canvasapi
The difference between SRAM and DRAM
How to obtain opensea data through opensea JS
Some 5000+ likes, the development notes of a director of cosmic factory, leaked
Go error collection | talk about the difference between the value type and pointer type of the method receiver
Summary of fluent systemchrome
Mysql database - Advanced SQL statement (I)
Mindmanager2022 serial number key decompression installer tutorial
LeetCode 1647. Minimum deletion times of unique character frequency
Pan Yueming helps Germany's Rochester Zodiac custom wristwatch
Fashion cloud interview questions series - JS high-frequency handwritten code questions
Uboot migration
2022.02.14
Hcip day 16 notes
[Android reverse] use DB browser to view and modify SQLite database (download DB browser installation package | install DB browser tool)
1068. Consolidation of ring stones (ring, interval DP)
Gorilla/mux framework (RK boot): add tracing Middleware