在我的 Flutter 项目中,我想在卡片中显示数据列表的每一项。为此,我在Card 中设置了所有组件(例如图像、文本)。
它看起来像下图-
现在,问题是——
我想将卡片宽度固定为固定大小或包装内容。由于 Card 小部件没有height 和 width属性,那么我应该怎么做才能固定这张卡片的宽度。
这是我的代码-
Card showCard(int position, int index, AsyncSnapshot<List<ModelFrontList>> snapshot) {
return new Card(
elevation: 10.0,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 250,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Image.network(
snapshot.data[position].products[index].image,
height: 150,
width: 50,
),
SizedBox(
width: 20.0,
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text( snapshot.data[position].products[index].nameEnglish),
SizedBox(
height: 10.0,
),
Text( snapshot.data[position].products[index].nameEnglish),
],
),
),
],
),
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
此showCard函数在Container内调用。所以,我需要一个解决方案来修复这张卡的宽度。
解决方案是将卡片包裹在一个大小合适的盒子中。例如,
Card showCard(int position, int index, AsyncSnapshot<List<ModelFrontList>> snapshot) {
SizedBox(
width: 200.0,
height: 300.0,
child: const Card(
elevation: 10.0,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 250,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Image.network(
snapshot.data[position].products[index].image,
height: 150,
width: 50,
),
SizedBox(
width: 20.0,
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text( snapshot.data[position].products[index].nameEnglish),
SizedBox(
height: 10.0,
),
Text( snapshot.data[position].products[index].nameEnglish),
],
),
),
],
),
),
),
);
}
)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7128 次 |
最近记录: |