i am using jiffy package to display datetime. with my code below, the dayInfo is showing both 'YESTERDAY' before and after crossing 0000hrs. i have attached the message list code and a screenshot showing what i am facing. i also note that when i send the message after 0000 hours just now, the banner was 'TODAY', and when i came out and back, it changed to 'YESTERDAY'. am i doing something wrong here?
class __DateLabelState extends State<_DateLabel> {
late String dayInfo;
@override
void initState() {
final createdAt = Jiffy(widget.dateTime);
final now = DateTime.now();
if (Jiffy(createdAt).isSame(now, Units.DAY)) {
dayInfo = 'TODAY';
} else if (Jiffy(createdAt)
.isSame(now.subtract(const Duration(days: 1)), Units.DAY)) {
dayInfo = 'YESTERDAY';
} else if (Jiffy(createdAt).isAfter(
now.subtract(const Duration(days: 7)),
Units.DAY,
)) {
dayInfo = createdAt.EEEE;
} else if (Jiffy(createdAt).isAfter(
Jiffy(now).subtract(years: 1),
Units.DAY,
)) {
dayInfo = createdAt.MMMd;
} else {
dayInfo = createdAt.MMMd;
}
super.initState();
}
MessageList
class _MessageList extends StatelessWidget {
const _MessageList({
Key? key,
required this.messages,
}) : super(key: key);
final List<Message> messages;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: ListView.separated(
itemCount: messages.length 1,
reverse: true,
separatorBuilder: (context, index) {
if (index == messages.length - 1) {
return _DateLabel(dateTime: messages[index].createdAt);
}
if (messages.length == 1) {
return const SizedBox.shrink();
} else if (index >= messages.length - 1) {
return const SizedBox.shrink();
} else if (index <= messages.length) {
final message = messages[index];
final nextMessage = messages[index 1];
if (!Jiffy(message.createdAt.toLocal())
.isSame(nextMessage.createdAt.toLocal(), Units.DAY)) {
return _DateLabel(
dateTime: message.createdAt,
);
} else {
return const SizedBox.shrink();
}
} else {
return const SizedBox.shrink();
}
},
itemBuilder: (context, index) {
if (index < messages.length) {
final message = messages[index];
if (message.user?.id == context.currentUser?.id) {
return _MessageOwnTile(message: message);
} else {
return _MessageTile(message: message);
}
} else {
return const SizedBox.shrink();
}
},
),
);
}
}
CodePudding user response:
to anyone having this issue as well, just add .toLocal() when returning and you will get the correct datetime banner.
class _MessageList extends StatelessWidget {
const _MessageList({
Key? key,
required this.messages,
}) : super(key: key);
final List<Message> messages;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: ListView.separated(
itemCount: messages.length 1,
reverse: true,
separatorBuilder: (context, index) {
if (index == messages.length - 1) {
return _DateLabel(dateTime: messages[index].createdAt.toLocal());
}
if (messages.length == 1) {
return const SizedBox.shrink();
} else if (index >= messages.length - 1) {
return const SizedBox.shrink();
} else if (index <= messages.length) {
final message = messages[index];
final nextMessage = messages[index 1];
if (!Jiffy(message.createdAt.toLocal())
.isSame(nextMessage.createdAt.toLocal(), Units.DAY)) {
return _DateLabel(
dateTime: message.createdAt.toLocal(),
);
} else {
return const SizedBox.shrink();
}
} else {
return const SizedBox.shrink();
}
},
itemBuilder: (context, index) {
if (index < messages.length) {
final message = messages[index];
if (message.user?.id == context.currentUser?.id) {
return _MessageOwnTile(message: message);
} else {
return _MessageTile(message: message);
}
} else {
return const SizedBox.shrink();
}
},
),
);
}
}