【发布时间】:2020-06-28 12:53:02
【问题描述】:
我在 ListView.builder 中有一行容器,按下时应该会改变容器背景的颜色。 我使用 switch 语句来改变颜色取决于 isSelected 与否。
问题是当我按下容器时颜色没有改变,即使打印显示我按下了正确的容器。
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: categoryData.length,
itemBuilder: (context,index){
bool isSelected = false;
return GestureDetector(
onTap: (){
switch(index){
case 0 :
isSelected = true;
break;
case 1 :
isSelected = true;
}
print(category[index].name + index.toString());
},
child: Row(
children: <Widget>[
Column(
children: <Widget>[
Container(
width: 68,
height: 68,
decoration: BoxDecoration(
color: isSelected ? Colors.white: Colors.transparent,
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: Colors.white,
width: 1,
),
boxShadow: isSelected
?[
BoxShadow(
color: Color(0x14000000),
blurRadius: 10
)
]: null
),
这是我正在使用的类别列表:
class list {
final String name;
final String count;
final String imageUrl;
list({this.imageUrl, this.name, this.count});
}
List<list> category = [
new list(
imageUrl: "assets/tops.png",
name: "TOPS",
count: "5"
),
new list(
imageUrl: "assets/fashion.png",
name: "Pants",
count: "5"
),
new list(
imageUrl: "assets/dress.png",
name: "DRESSES",
count: "4"
),
new list(
imageUrl: "assets/coat.png",
name: "COATS",
count: "4"
),
new list(
imageUrl: "assets/suits.png",
name: "SUITS",
count: "4"
),
];
【问题讨论】:
-
你可以添加示例 json 数据,以便我可以给你这个例子。
-
@SagarAcharya 我添加了我正在使用的列表
-
只需查看我添加的代码,如果您有任何问题,请告知。