【发布时间】:2020-10-07 03:16:17
【问题描述】:
我正在尝试更改由 ListView.Builder 创建的点击卡片的颜色。我尝试使用 Inkwell 并将 listview 包装在容器内。
现在的问题是,如果我单击或点击任何列表项,所有卡片的颜色都会改变。我需要更改我没有点击的卡片的颜色。
简单来说,如果用户点击项目,我想显示它被选中,就像我们在其他应用中看到的那样。
下面是代码。
@override
Widget build(BuildContext context) {
return Theme(
isMaterialAppTheme: true,
data: ThemeData(
),
child:Scaffold(
key: _scaffoldKey,
appBar: myAppBar(),
endDrawer: myDrawer(),
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
decoration: BoxDecoration(
color:Colors.grey,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(4.0),
bottomRight: Radius.circular(4.0))),
padding: EdgeInsets.symmetric(horizontal:16.0, vertical: 15.0),
width: double.infinity,
child: Text("Booking Details",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18.0,
color: Colors.white,
letterSpacing: 3,
wordSpacing: 3
),)),
Expanded(child: ListView.builder(
padding: EdgeInsets.all(16.0),
itemCount: lists.length,
itemBuilder: (BuildContext context, int index){
return Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
color:Colors.grey,
),
width: double.infinity,
margin: EdgeInsets.only(left:15, right: 15.0, bottom: 10.0),
child: Material(
borderRadius: BorderRadius.circular(5.0),
elevation: 3.0,
child:InkWell(
onTap: () {
setState(() {
_color = !_color;
});
},
child: Container(
decoration: BoxDecoration(
color: _color ? Colors.deepOrangeAccent : Colors.purpleAccent,
),
padding: EdgeInsets.all(16.0),
child: Row(
children: <Widget>[
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Class From -: ' + lists[index].title+ ',
style: TextStyle(
fontSize: 14.0,
),),
SizedBox(height: 10.0,),
Text('Subject -: ' + lists[index].product,
style: TextStyle(
fontSize: 14.0,
),),
SizedBox(height: 10.0,),
Text('Price -: ' + lists[index].price.toString() + ' USD ' + lists[index].serviceType,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold
),),
SizedBox(height: 20.0,),
],
),
),
],
),
),
),
),
),
],
);
},
),),
Container(
decoration: BoxDecoration(
color:Colors.black12,
),
width: double.infinity,
padding: EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Text("Subtotal \$50", style: TextStyle(
color: Colors.grey.shade700,
fontSize: 16.0
),),
SizedBox(height: 5.0,),
Text("Delivery \$05", style: TextStyle(
color: Colors.grey.shade700,
fontSize: 16.0
),),
SizedBox(height: 10.0,),
Text("Cart Subtotal \$55", style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18.0
),),
SizedBox(height: 20.0,),
SizedBox(
width: double.infinity,
child: MaterialButton(
height: 50.0,
color: Colors.pinkAccent,
child: Text("Secure Checkout".toUpperCase(), style: TextStyle(
color: Colors.white
),),
onPressed: (){},
),
)
],
),
)
],
),
),
我在这里做错了什么?
【问题讨论】:
-
//这里定义索引 int _selectedIndex = -1; //然后更新索引 setState(() { _selectedIndex = index; }); //并更新颜色逻辑颜色:index ==_selectedIndex ? Colors.deepOrangeAccent : Colors.purpleAccent,
-
@Gaurav 感谢您的评论。你能给我举个例子吗?
标签: flutter