当前位置:网站首页>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,
);
}
}
边栏推荐
- 1068. Consolidation of ring stones (ring, interval DP)
- finalize finalization finally final
- Learning notes of raspberry pie 4B - IO communication (SPI)
- Common problems in multi-threaded learning (I) ArrayList under high concurrency and weird hasmap under concurrency
- ThreadLocal function, scene and principle
- Mongoose the table associated with the primary key, and automatically bring out the data of another table
- Go Technology Daily (2022-02-13) - Summary of experience in database storage selection
- How can enterprises and developers take advantage of the explosion of cloud native landing?
- Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
- Recursive least square adjustment
猜你喜欢
Wisdom tooth technology announced that it had completed the round D financing of US $100million and had not obtained a valid patent yet
QGIS grid processing DEM data reclassification
How to switch between dual graphics cards of notebook computer
Sort merge sort
6.0 kernel driver character driver
How to quickly build high availability of service discovery
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
This time, thoroughly understand bidirectional data binding 01
Recursive least square adjustment
Runtime. getRuntime(). totalMemory/maxMemory()
随机推荐
[sg function] lightoj Partitioning Game
. Net ADO splicing SQL statement with parameters
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
Go Technology Daily (2022-02-13) - Summary of experience in database storage selection
Classification and extension of OC
6.2 normalization 6.2.5 third normal form (3NF)
Leetcode week 4: maximum sum of arrays (shape pressing DP bit operation)
Codeforces Round #768 (Div. 1)(A-C)
[15th issue] Tencent PCG background development internship I, II and III (OC)
How to solve the problem of requiring a password when accessing your network neighborhood on your computer
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
Yyds dry goods inventory Spring Festival "make" your own fireworks
Text replacement demo
[golang] leetcode intermediate - alphabetic combination of island number and phone number
Is the controller a single instance or multiple instances? How to ensure the safety of concurrency
Unity shader visualizer shader graph
ThreadLocal function, scene and principle
Hcip 13th day notes
How about opening an account at Hengtai securities? Is it safe?
QGIS grid processing DEM data reclassification