【发布时间】:2021-11-24 17:03:16
【问题描述】:
我在 Flutter 中做了一个 ListView,每一个 ListTiles 被点击后会跳转到另一个页面。 我希望添加将已选中的 ListTiles 的颜色更改为灰色的功能。
我知道如何使用 Container 设置 ListTile 的颜色, 但我找不到任何示例代码来显示如何调用该项目是否已被选中。 我能得到一些帮助吗?
bool temp = false;
Color temp_color = Colors.white;
class NotificationTiles extends StatelessWidget {
final String title, content, date;
final bool enable;
const NotificationTiles({
Key? key,
required this.title,
required this.content,
required this.date,
required this.enable,
}) : super(key: key);
@override
Widget build(BuildContext context) {
if(temp){temp_color = Colors.white;}
else{temp_color = Colors.grey;}
return Container(
color: temp_color,
child:
ListTile(
title: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(title, style: TextStyle(color: Color(0xFF303030), fontSize:17, fontWeight: FontWeight.bold)),
SizedBox(height: 5),
Text(date, style: TextStyle(color: Colors.grey, fontSize:14,)),
],
),
trailing: Icon(Icons.arrow_forward_ios, color: Colors.black,size: 15,),
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (context) => NotificationPage(
title: this.title, content: this.content, date: this.date))),
enabled: enable,
)
);
}
}
【问题讨论】:
-
您可以通过使用 Provider 来实现。阅读:pub.dev/packages/provider