当前位置:网站首页>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,
);
}
}
边栏推荐
- Some 5000+ likes, the development notes of a director of cosmic factory, leaked
- pycuda._ driver. LogicError: explicit_ context_ dependent failed: invalid device context - no currently
- The difference between SRAM and DRAM
- 1068. Consolidation of ring stones (ring, interval DP)
- How to solve the problem of computer networking but showing no Internet connection
- Label coco format data and format data in the upper left corner and lower right corner are mutually converted
- Text replacement demo
- [actual combat record] record the whole process of the server being attacked (redis vulnerability)
- Xiangong intelligent obtained hundreds of millions of yuan of b-round financing to accelerate the process of building non-standard solutions with standardized products
- Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
猜你喜欢
Team collaborative combat penetration tool CS artifact cobalt strike
In VS_ In 2019, scanf and other functions are used to prompt the error of unsafe functions
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Data consistency between redis and database
webAssembly
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
Shiftvit uses the precision of swing transformer to outperform the speed of RESNET, and discusses that the success of Vit does not lie in attention!
How to quickly build high availability of service discovery
STM32 multi serial port implementation of printf -- Based on cubemx
The reason why the computer runs slowly and how to solve it
随机推荐
How to restore the factory settings of HP computer
Sort merge sort
How about agricultural futures?
Hcip day 14 notes
Summary of fluent systemchrome
Hcip day 16 notes
STM32 multi serial port implementation of printf -- Based on cubemx
Exness: the Central Bank of England will raise interest rates again in March, and inflation is coming
Learning methods of zynq
Firefox set up proxy server
Es6~es12 knowledge sorting and summary
Data consistency between redis and database
(POJ - 2912) rochambau (weighted concurrent search + enumeration)
[automation operation and maintenance novice village] flask-2 certification
How to solve the problem of requiring a password when accessing your network neighborhood on your computer
C deep anatomy - the concept of keywords and variables # dry inventory #
Errors taken 1 Position1 argument but 2 were given in Mockingbird
How to quickly build high availability of service discovery
Yyds dry goods inventory Spring Festival "make" your own fireworks
The overseas listing of Shangmei group received feedback, and brands such as Han Shu and Yiye have been notified for many times and received attention