gpt4 book ai didi

flutter - GridLayout 项目溢出 flutter

转载 作者:行者123 更新时间:2023-12-02 20:04:36 26 4
gpt4 key购买 nike

我有一个 gridlayout,griditems 基本上是:牌有两件事:

  • 图片
  • 正文

但是图像是通过互联网加载的,因此有时它会溢出网格项目框并且这个框不会改变高度,我想要的是图像应该是固定大小的。

这是我的代码:(我该怎么做?)

import 'package:flutter/material.dart';
import '../models/dbModel.dart';

class GridLayout extends StatelessWidget {
final List<DbModel> m;
var appContext;

GridLayout(this.m, this.appContext);

Widget build(context) {
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2, crossAxisSpacing: 7.0, mainAxisSpacing: 7.0),
padding: const EdgeInsets.all(10.0),
itemBuilder: (context, i) {
return GridItem(appContext, m[i]);
},
itemCount: m.length,
);
}
}

class GridItem extends StatelessWidget {
final appContext;
final dbModel;

GridItem(this.appContext, this.dbModel);

Widget build(context) {
return GestureDetector(
onTap: () {},
child: Card(
child: Column(
children: <Widget>[
dbModel.img == "NOLINK" ? Image.network("https://i.ibb.co/Vv6cPj4/404.png",) : Image.network(dbModel.img),
Container(
padding: EdgeInsets.all(10.0),
child: Text("${dbModel.title}"),
),
],
),
),
);
}
}

最佳答案

你需要包装图像小部件 - Image.network with Expanded 并添加 fit: BoxFit.cover,

工作代码:

Widget build(context) {
return GestureDetector(
onTap: () {},
child: Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch, //add this
children: <Widget>[
Expanded(
child: Image.network(
"https://i.ibb.co/Vv6cPj4/404.png",
fit: BoxFit.cover, // add this
),
),
Center(
child: Container(
padding: EdgeInsets.all(10.0),
child: Text("Title under"),
),
),
],
),
),
);
}

输出:

enter image description here

关于flutter - GridLayout 项目溢出 flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55069369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com