文章目录
GridView列表三种形式
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GridView.count(
crossAxisCount: 3,
children: const [
Icon(Icons.home),
Icon(Icons.settings),
Icon(Icons.all_inclusive),
Icon(Icons.ac_unit)
],
);
}
}
- 可以通过
GridView.extent实现网格布局
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GridView.extent(
maxCrossAxisExtent: 40,
children: const [
Icon(Icons.home),
Icon(Icons.settings),
Icon(Icons.ac_unit),
Icon(Icons.time_to_leave),
],
);
}
}
- 可以通过
GridView.builder实现网格布局
List listData = [
{
"imageUrl": "http://124.223.18.34:5555/static/images/gdyg/gdyg1_1.jpg",
"name": "孤独摇滚第1集"
},
{
"imageUrl": "http://124.223.18.34:5555/static/images/gdyg/gdyg1_2.jpg",
"name": "孤独摇滚第2集"
},
{
"imageUrl": "http://124.223.18.34:5555/static/images/gdyg/gdyg1_3.jpg",
"name": "孤独摇滚第3集"
}
];
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
Widget _initDataWidget(context, index) {
return Container(
padding: const EdgeInsets.all(5),
decoration: BoxDecoration(
color: Colors.blue, borderRadius: BorderRadius.circular(5)),
child: Column(
children: [
Image.network(listData[index]["imageUrl"]),
Text(
listData[index]["name"],
style: const TextStyle(color: Colors.white),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return GridView.builder(
padding: const EdgeInsets.all(10),
itemCount: listData.length,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2, crossAxisSpacing: 10),
itemBuilder: _initDataWidget);
}
}
常用属性
| 名称 | 类型 | 说明 |
|---|
| scrollDirection | Axis | 滚动方法 |
| padding | EdgeInsetsGeometry | 内边距 |
| resolve | bool | 组件反向排序 |
| crossAxisSpacing | double | 水平子Widget之间间距 |
| mainAxisSpacing | double | 垂直子Widget之间间距 |
| crossAxisCount | int,用在GridView.count | 一行的Widget数量 |
| maxCrossAxisExtent | double用在GridView.extent | 横轴子元素的最大长度 |
| childAspectRatio | double | 子Widget宽高比例 |
| children | | [] |
| gridDelegate | SliveGridDelegateWithFiexdCrossAxisCountSliveGridDelegateWithFiexdCrossAxisCount;SliveGridDelegateWithMaxCrossAxisExtent | 控制布局主要用在GridView.builder里面 |
小案例
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GridView.count(
padding: const EdgeInsets.all(15),
mainAxisSpacing: 15,
crossAxisSpacing: 15,
crossAxisCount: 2,
children: [
Container(
decoration: BoxDecoration(
color: Colors.blue, borderRadius: BorderRadius.circular(5)),
child: Image.network(
"http://124.223.18.34:5555/static/images/gdyg/gdyg1_1.jpg",
alignment: Alignment.topCenter,
fit: BoxFit.contain,
),
),
Container(
decoration: BoxDecoration(
color: Colors.blue, borderRadius: BorderRadius.circular(5)),
child: Image.network(
"http://124.223.18.34:5555/static/images/gdyg/gdyg1_2.jpg",
alignment: Alignment.topCenter,
fit: BoxFit.contain,
),
),
Container(
decoration: BoxDecoration(
color: Colors.blue, borderRadius: BorderRadius.circular(5)),
child: Image.network(
"http://124.223.18.34:5555/static/images/gdyg/gdyg1_3.jpg",
alignment: Alignment.topCenter,
fit: BoxFit.contain,
),
)
],
);
}
}

class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GridView.count(
padding: const EdgeInsets.all(15),
mainAxisSpacing: 15,
crossAxisSpacing: 15,
crossAxisCount: 2,
children: [
Container(
decoration: BoxDecoration(
color: Colors.blue, borderRadius: BorderRadius.circular(5)),
child: Column(
children: [
Image.network(
"http://124.223.18.34:5555/static/images/gdyg/gdyg1_1.jpg",
alignment: Alignment.topCenter,
fit: BoxFit.contain,
width: 530,
height: 110,
),
const Text(
"孤独摇滚第一集",
style: TextStyle(
color: Colors.white,
),
)
],
)),
Container(
decoration: BoxDecoration(
color: Colors.blue, borderRadius: BorderRadius.circular(5)),
child: Column(
children: [
Image.network(
"http://124.223.18.34:5555/static/images/gdyg/gdyg1_2.jpg",
alignment: Alignment.topCenter,
fit: BoxFit.contain,
width: 530,
height: 110,
),
const Text(
"孤独摇滚第二集",
style: TextStyle(
color: Colors.white,
),
)
],
)),
Container(
decoration: BoxDecoration(
color: Colors.blue, borderRadius: BorderRadius.circular(5)),
child: Column(
children: [
Image.network(
"http://124.223.18.34:5555/static/images/gdyg/gdyg1_3.jpg",
alignment: Alignment.topCenter,
fit: BoxFit.contain,
width: 530,
height: 110,
),
const Text(
"孤独摇滚第三集",
style: TextStyle(
color: Colors.white,
),
)
],
))
],
);
}
}
