Dimibe / grouped_list

A Flutter ListView in which items can be grouped into sections.
https://pub.dev/packages/grouped_list
MIT License
395 stars 106 forks source link

How can I group by Today, Yesterday and This Month? #117

Closed develogo closed 2 years ago

develogo commented 3 years ago

Look, how can I create 3 groups today, yesterday and the rest of the month?

Im using: groupBy: (ProcedureEntity element) => DateFormat('d') .format(DateTime.parse(element.createdAt)),

jabirmayar commented 3 years ago

This is how I have done it. You can get an idea from this... groupBy: (e) =>DateFormat('yyyy-MM-dd').format( tz.TZDateTime.from(e.reminderDate, tz.local), ) groupHeaderBuilder: (e) =>Text( checkDate( e.reminderDate.toString(), ), textAlign: TextAlign.start, style: TextStyle( fontSize: 15, color: Colors.blueGrey[300], fontWeight: FontWeight.bold), ), ,

// and the function checkDate

`String checkDate(String dateString) {
    tz.TZDateTime checkedTime = tz.TZDateTime.parse(tz.local, dateString);
    tz.TZDateTime currentTime = tz.TZDateTime.now(tz.local);

    if ((currentTime.year == checkedTime.year) &&
        (currentTime.month == checkedTime.month) &&
        (currentTime.day == checkedTime.day)) {
      return "TODAY";
    } else if ((currentTime.year == checkedTime.year) &&
        (currentTime.month == checkedTime.month)) {
      if ((currentTime.day - checkedTime.day) == 1) {
        return "YESTERDAY";
      } else if ((currentTime.day - checkedTime.day) == -1) {
        return "TOMORROW";
      } else {
        return DateFormat.yMMMd()
            .format(tz.TZDateTime.parse(tz.local, dateString));
      }
    }
    return DateFormat.yMMMd().format(tz.TZDateTime.parse(tz.local, dateString));
  }`
Dimibe commented 2 years ago

Seems to be solved