You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.2 KiB
49 lines
1.2 KiB
part of '../neon.dart'; |
|
|
|
class RelativeTime extends StatelessWidget { |
|
const RelativeTime({ |
|
required this.date, |
|
this.style, |
|
super.key, |
|
}); |
|
|
|
final DateTime date; |
|
final TextStyle? style; |
|
|
|
static String format(final DateTime date) { |
|
final now = DateTime.now(); |
|
var diff = now.difference(date.toLocal()); |
|
final text = StringBuffer(); |
|
|
|
// Sometimes something can be messed up... |
|
if (diff.isNegative) { |
|
if (diff.inMinutes >= 1) { |
|
text.write('-'); |
|
} |
|
diff = Duration(microseconds: -diff.inMicroseconds); |
|
} |
|
|
|
if (diff.inMinutes < 1) { |
|
text.write('now'); |
|
} else if (diff.inHours < 1) { |
|
text.write('${diff.inMinutes}m'); |
|
} else if (diff.inDays < 1) { |
|
text.write('${diff.inHours}h'); |
|
} else if (diff.inDays < 365) { |
|
text.write('${diff.inDays}d'); |
|
} else { |
|
text.write('${diff.inDays ~/ 365}y'); |
|
} |
|
|
|
return text.toString(); |
|
} |
|
|
|
@override |
|
Widget build(final BuildContext context) => StreamBuilder( |
|
stream: Stream.periodic(const Duration(minutes: 1)), |
|
builder: (final context, final _) => Text( |
|
format(date), |
|
style: style, |
|
), |
|
); |
|
}
|
|
|