使用 Wrap 代替 GridView 实现网格布局, 并且可以实现自定义元素的大小
class MyHomePage extends StatelessWidget {
@override
Widget
build(BuildContext context
) {
var width
= MediaQuery
.of(context
).size
.width
;
return Scaffold(
appBar
: AppBar(
title
: Text("首页"),
),
backgroundColor
: Color(0xfff1f1f1),
body
: ListView(
children
: [
Container(
height
: 200,
color
: Colors
.black12
,
child
: Center(child
: Text("顶部")),
),
Container(
height
: 300,
margin
: EdgeInsets
.only(top
: 20),
width
: double
.infinity
,
child
: Wrap(
children
: _createGridViewItem(width
),
),
)
],
),
);
}
_createGridViewItem(width
) {
List
<Widget
> list
= [];
for (var i
= 0; i
< 18; i
++) {
list
.add(Container(
width
: width
/ 4,
height
: 50,
color
: Colors
.primaries
[i
],
));
}
return list
;
}
}
转载请注明原文地址: https://lol.8miu.com/read-1414.html