我使用Gridview.builder来创建卡片网格。我没有找到Gridview.builder内部的任何属性来确定单个卡片的大小,也没有在Card小部件中找到任何属性来确定卡片的大小。
此外,我想使卡片内的图像和文本具有响应性。以下是返回卡片的方法代码 -
我已经使用了如下的 GridView.builder(“data”是来自 API 的数据):
这段代码的输出结果如下所示,显示出类似于这样的卡片: 上述代码输出的截图
Card myCard(String houseName, String houseImageUrl) {
return new Card(
elevation: 5.0,
child: new InkWell(
child: new Container(
alignment: Alignment.center,
child: new Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Image.network(
houseImageUrl,
height: 180.0,
width: 180.0,
),
new Padding(
padding: const EdgeInsets.all(8.0),
child: new Center(
child: new Text(
houseName,
style: new TextStyle(fontSize: 16.0),
textAlign: TextAlign.center,
),
),
),
],
),
),
));
我已经使用了如下的 GridView.builder(“data”是来自 API 的数据):
new GridView.builder(
itemCount: data == null ? 0 : data.length,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
itemBuilder: (BuildContext context, int index) {
var housename = data.keys.toList()[index].toString();
return myCard(
housename, data[housename]['image'].toString());
})));
这段代码的输出结果如下所示,显示出类似于这样的卡片: 上述代码输出的截图