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

边栏推荐
- Creation of the template of the password management software keepassdx
- Yyds dry goods inventory Prometheus alarm Art
- C3p0 connection MySQL 8.0.11 configuration problem
- Esp-idf turns off serial port log output.
- C summary of knowledge point definitions, summary notes
- [golang] leetcode intermediate - alphabetic combination of island number and phone number
- BUUCTF,Misc:LSB
- Introduction to the gtid mode of MySQL master-slave replication
- Blue Bridge Cup -- Mason prime
- . Net ADO splicing SQL statement with parameters
猜你喜欢

Hcip 13th day notes

webAssembly

Wisdom tooth technology announced that it had completed the round D financing of US $100million and had not obtained a valid patent yet

Leetcode week 4: maximum sum of arrays (shape pressing DP bit operation)

2022.02.13

Mysql database - Advanced SQL statement (I)

Runtime. getRuntime(). totalMemory/maxMemory()

Summary of basic knowledge of exception handling

Blue Bridge Cup -- Mason prime

540. Single element in ordered array
随机推荐
QGIS grid processing DEM data reclassification
This time, thoroughly understand bidirectional data binding 01
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
MLX90614 driver, function introduction and PEC verification
3 environment construction -standalone
Interesting 10 CMD commands
Yyds dry goods inventory Spring Festival "make" your own fireworks
2022.02.13
LeetCode 540. A single element in an ordered array
Ansible common usage scenarios
6.0 kernel driver character driver
Leetcode week 4: maximum sum of arrays (shape pressing DP bit operation)
. Net ADO splicing SQL statement with parameters
Subset enumeration method
Mindmanager2022 serial number key decompression installer tutorial
(POJ - 2912) rochambau (weighted concurrent search + enumeration)
Mysql database - Advanced SQL statement (I)
6.2 normalization 6.2.5 third normal form (3NF)
Codeforces Round #768 (Div. 1)(A-C)
Exness: the Central Bank of England will raise interest rates again in March, and inflation is coming