【问题标题】:Is there a way to save read status in flutter?有没有办法在颤动中保存读取状态?
【发布时间】: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,
    )
    );
  }
}

【问题讨论】:

标签: flutter dart


【解决方案1】:

您可以只拥有一个存储索引的索引列表,然后每次选择列表磁贴时,您应该将磁贴的索引添加到列表中,然后 setState 如果您使用 StateFullWidget 或创建列表类型 ValueNotifier 并监听列表的更改在您的构建方法中。

【讨论】:

    【解决方案2】:

    你应该创建一个新的数据库来检查列表索引和点击次数,如果点击次数>0然后改变颜色,否则默认颜色

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案3】:

    您可以创建新的模态类来存储 2 种变量(String - bool),然后您可以导入此 cals 并为其创建一个对象,并使用 ListTile 中的对象将其标记为已使用或尚未使用。

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 2021-01-13
      • 2022-10-12
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      • 2017-08-24
      • 2020-09-25
      • 1970-01-01
      相关资源
      最近更新 更多