【发布时间】:2021-09-29 11:44:52
【问题描述】:
我有一个列表视图,它可以从地图中获取属性,例如图片以及点击次数。我想知道如何更改颜色,以便在特定卡片上显示绿色复选框图标。就像现在一样,我只能一次更改所有卡片上所有复选框的颜色。我想我希望能够只选择点击的卡片,以便其复选框变为绿色。这是最相关的代码:
主要:
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: ListView.builder(
itemCount: widget._passoverCol.keys.length,
itemBuilder: (BuildContext context, int index)
return Container(
height: 100,
padding: const EdgeInsets.all(8.0),
child: Stack(
children: <Widget>[
myCard(
fileName: widget._passoverCol.keys.elementAt(index),
displayName: widget._passoverCol.values
.elementAt(index)
.displayName,
tapsCount: widget._passoverCol.values
.elementAt(index)
.tapsCount,
color: cardColor,
onTap: () {
setState(() {
cardColor = Colors.green;
});
},
),
],
));
}),
),
);
}
}
卡片:
class myCard extends StatefulWidget {
const myCard({
required this.tapsCount,
required this.fileName,
required this.displayName,
this.onTap,
this.color,
});
final int? tapsCount;
final String? fileName;
final String? displayName;
final Color? color;
final Function()? onTap;
@override
_myCardState createState() => _myCardState();
}
class _myCardState extends State<myCard> {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: widget.onTap!,
child: Card(
child: Row(
children: <Widget>[
Expanded(
child: Image.asset(
//getImageFile()
widget.fileName!,
height: 100.0,
width: 100.0,
)),
Padding(
padding: const EdgeInsets.only(left: 32.0),
child: Text(widget.displayName!),
),
SizedBox(
width: 15.0,
),
Padding(
padding: const EdgeInsets.only(right: 12.0),
child: Text(
widget.tapsCount!.toString(),
),
),
Icon(FontAwesomeIcons.check, color: widget.color!),
SizedBox(
width: 200.0,
),
],
),
),
);
}
}
【问题讨论】:
-
您可以将白色列表用于特定卡片;否则在
_passoverCol中设置一个颜色属性,默认值为白色