Home > Enterprise >  Is there a way to save read status in flutter?
Is there a way to save read status in flutter?

Time:11-25

I have made a ListView in Flutter, and each ListTiles leads to another page when they are clicked. I wish add the function that changes the color of the already checked ListTiles to gray.

I know how to set the color of a ListTile using Container, but I couldn't find any example codes that shows how to call whether this item has ever been selected. Could I get some help??

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,
    )
    );
  }
}

CodePudding user response:

You can just have a list of indexes that store indexes that clicked then every time a list tile is selected, you should add tile's index to the list and then setState if you use StateFullWidget or make list type ValueNotifier and listen to list's changes in your build method.

CodePudding user response:

You should create a new database that checks the list index against number of click if the number of click >0 then change color otherwise the default color

CodePudding user response:

You can make new modal class that store 2 kind of variables (String - bool), then you can import this calss and make an object of it, and use the object in the ListTile to mark it as used or not yet.

  • Related