[英]How to change the card color when toggle button is on in flutter
I want to change the container color when i turn on the toggle button.我想在打开切换按钮时更改容器颜色。 I'm using ListTile, here is my code
我正在使用 ListTile,这是我的代码
Container(
color:Colors.red, //want to change this color
child:ListTile(
dense:true,
trailing:Switch(
value: _selectedProducts.contains(book.id),
onChanged:(bool? selected) {
if (selected != null) {
setState(() {
_onProductSelected(
selected, book.id);
print(selected);
print(book.id);
});
}},
activeTrackColor: HexColor("#b8c2cc"),
activeColor: HexColor("#7367f0"),
),
title: Text(book.title,style: ),
subtitle: Text("Rs/- 40",),
),
);
_onProductSelected
in method i'm handling to on the toggle button on specific one. _onProductSelected
在我正在处理的特定方法上的切换按钮上。
update:更新:
i'm calling this widget buildBook
in Widget build(BuildContext context)
我在
Widget build(BuildContext context)
调用这个小部件buildBook
Expanded(
child: ListView.builder(
itemCount: books.length,
itemBuilder: (context, index) {
final book = books[index];
return Column(
children: [
buildBook(book,index),
Divider()
],
);
},
),
),
Widget buildBook(Book book) => InkWell(
onTap:(){Navigator.of(context).push(MaterialPageRoute(
builder: (context) => ProductProfile(),
));},
child: Container(
color:isChangeColor?Colors.red:Colors.green,
child: ListTile(
dense:true,
leading: SizedBox(
width: 50,
height: 90,
child: ClipRRect(
borderRadius: BorderRadius.circular(10.0),
child: Image.network(
book.urlImage,
fit: BoxFit.cover,
),
),
),
trailing:Switch(
value: _selectedProducts.contains(book.id),
onChanged:(bool? selected) {
if (selected != null) {
// setState(() {
// if(selected){
// cardColor = Colors.blue;
// }else{
// cardColor = Colors.pink;
// }});
setState(() {
isChangeColor = selected;
// isChangeColor=true;
_onProductSelected(
selected, book.id);
print(selected);
print(book.id);
});
}},
activeTrackColor: HexColor("#b8c2cc"),
activeColor: HexColor("#7367f0"),
),
title: Text(book.title,style: GoogleFonts.montserrat(color:Colors.black,fontWeight:FontWeight.w500,fontSize: 15),),
// isThreeLine: true,
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(book.author,style: GoogleFonts.montserrat()),
// SizedBox10(),
Text("Rs/- 40",style: GoogleFonts.montserrat(fontSize: 15,color: Colors.black,fontWeight: FontWeight.w400),),
],
),
),
),
);
}
please help how to do this.请帮助如何做到这一点。
update更新
You can define a variable for card color and change value of that on _onProductSelected
method.您可以为卡片颜色定义一个变量,并在
_onProductSelected
方法上更改该变量的值。
var cardColor = Colors.pink;
Container(
color: Colors.white,
height: MediaQuery.of(context).size.height*0.12,
child: Card(
margin: EdgeInsets.all(10),
color: cardColor,
child: SwitchListTile(
title: Text(book.title,style: TextStyle(
color: Colors.white,
),
),
value: _selectedProducts.contains(book.id),
activeColor: Colors.blue,
inactiveTrackColor: Colors.grey,
onChanged: (bool? selected) {
if (selected != null) {
setState(() {
_onProductSelected(selected, book.id);
});
}},
),
),
));
void _onProductSelected(bool selected, productId) {
if (selected == true) {
setState(() {
cardColor = Colors.orange;
_selectedProducts.add(productId);
});
} else {
setState(() {
cardColor = Colors.pink;
_selectedProducts.remove(productId);
});
}
}
Also for container you can use below code :同样对于容器,您可以使用以下代码:
var containerColor= Colors.pink;
Container(
color:containerColor, //want to change this color
child:ListTile(
dense:true,
trailing:Switch(
value: _selectedProducts.contains(book.id),
onChanged:(bool? selected) {
if (selected != null) {
setState(() {
if(selected){
containerColor = Colors.blue;
}else{
containerColor = Colors.pink;
}
_onProductSelected(
selected, book.id);
print(selected);
print(book.id);
});
}},
activeTrackColor: HexColor("#b8c2cc"),
activeColor: HexColor("#7367f0"),
),
title: Text(book.title,style: ),
subtitle: Text("Rs/- 40",),
),
);
You can use a bool
property.您可以使用
bool
属性。 Based on this true-false condition you can change the color of container.根据此真假条件,您可以更改容器的颜色。
bool isChangeColor;
Container(
color: isChangeColor?Colors.red:Colors.green, //here you check
child:ListTile(
dense:true,
trailing:Switch(
value: _selectedProducts.contains(book.id),
onChanged:(bool? selected) {
if (selected != null) {
setState(() {
isChangeColor = selected;
_onProductSelected(
selected, book.id);
print(selected);
print(book.id);
});
}},
activeTrackColor: HexColor("#b8c2cc"),
activeColor: HexColor("#7367f0"),
),
title: Text(book.title,style: ),
subtitle: Text("Rs/- 40",),
),
);
Complete source code完整的源代码
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: Home(),
));
class Home extends StatefulWidget {
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
bool isChangeColor = false;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Container(
color: isChangeColor ? Colors.red : Colors.green, //here you check
child: ListTile(
dense: true,
trailing: Switch(
value: isChangeColor,
onChanged: (bool selected) {
if (selected != null) {
setState(() {
isChangeColor = selected;
});
}
},
),
title: Text("book.titl"),
subtitle: Text(
"Rs/- 40",
),
),
));
}
}
output:输出:
You can use bool variable like isSelected to check ListTile is selected or not.您可以使用像isSelected这样的bool变量来检查是否选择了 ListTile。
Example :示例:
bool isSelected = true;
Container(
color: isSelected ? Colors.red : Colors.green,
...
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.