这是我的卡片小部件代码。这将作为 ListView.builder 小部件的子项
class CardHome extends StatefulWidget {
String cardHead;
String cardDesc;
String cardTime;
String cardDate;
CardHome(
{this.cardHead, this.cardDesc, this.cardTime, this.cardDate});
@override
_CardHomeState createState() => _CardHomeState();
}
class _CardHomeState extends State<CardHome> {
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.fromLTRB(globals.blockSizeHorizontal * 6,
globals.blockSizeVertical * 3, globals.blockSizeHorizontal * 6, 0),
width: globals.blockSizeHorizontal * 87,
child: Card(
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(SizeConfig.blockSizeHorizontal * 4.5)),
color: Color(globals.secColor),
elevation: 0,
child: Container(
padding: EdgeInsets.symmetric(
vertical: globals.blockSizeVertical * 2.7,
horizontal: globals.blockSizeHorizontal * 6.5),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: globals.blockSizeHorizontal * 56,
child: Text(
'Buy Plane Tickets',
overflow: TextOverflow.ellipsis,
maxLines: 2,
style: TextStyle(
height: globals.blockSizeVertical * 0.14,
fontFamily: 'Gilroy',
fontWeight: FontWeight.bold,
fontSize: globals.blockSizeHorizontal * 3.5,
letterSpacing: globals.blockSizeHorizontal * 0.1,
color: Color(0xff171b20)),
),
),
Text(
'8:20',
style: TextStyle(
fontFamily: 'Arboria',
fontWeight: FontWeight.w400,
fontSize: globals.blockSizeHorizontal * 3.8,
color: globals.inactHeadTextColor),
),
],
),
SizedBox(
height: globals.blockSizeVertical * 2,
),
Container(
width: globals.blockSizeHorizontal * 70,
child: Text(
'Buy Tickets for departure on 14th May, 2020 at 2:15pm',
overflow: TextOverflow.ellipsis,
maxLines: 3,
style: TextStyle(
fontFamily: 'Gilroy',
fontWeight: FontWeight.w600,
fontSize: globals.blockSizeHorizontal * 3.5,
letterSpacing: globals.blockSizeHorizontal * 0.02,
color: Color(0xff171b20)),
),
),
SizedBox(height: globals.blockSizeVertical * 2),
Text(
'23 January',
style: TextStyle(
fontFamily: 'Arboria',
fontWeight: FontWeight.w400,
fontSize: globals.blockSizeHorizontal * 3.3,
color: globals.inactHeadTextColor),
)
],
),
),
),
);
}
}
我需要将此小部件传递给 ListView.builder,因此在传递小部件时,cardHead、cardDesc、cardTime、cardDate 变量需要代替我手动输入的字符串(例如,购买飞机票、1 月 23 日等)。例如,cardHead 用作我可以在 listview 构建器中使用的属性,如下所示:
ListView.builder(
itemCount: headers.length,
itemBuilder: (context, index) {
return CardHome(
cardHead: Text('${headers.[index]}'),
cardDesc: Text('${descriptions.[index]}'),
);
},
);
有什么办法可以做到这一点?