123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- import 'package:flutter/material.dart';
- Map<int, String> _months = {
- 1: "Jan",
- 2: "Feb",
- 3: "March",
- 4: "April",
- 5: "May",
- 6: "Jun",
- 7: "July",
- 8: "Aug",
- 9: "Sep",
- 10: "Oct",
- 11: "Nov",
- 12: "Dec",
- };
- Map<int, String> _fullMonths = {
- 1: "January",
- 2: "February",
- 3: "March",
- 4: "April",
- 5: "May",
- 6: "June",
- 7: "July",
- 8: "August",
- 9: "September",
- 10: "October",
- 11: "November",
- 12: "December",
- };
- Map<int, String> _days = {
- 1: "Mon",
- 2: "Tue",
- 3: "Wed",
- 4: "Thu",
- 5: "Fri",
- 6: "Sat",
- 7: "Sun",
- };
- String getMonthAndYear(DateTime dateTime) {
- return _months[dateTime.month] + " " + dateTime.year.toString();
- }
- String getDayAndMonth(DateTime dateTime) {
- return _days[dateTime.weekday] +
- ", " +
- dateTime.day.toString() +
- " " +
- _months[dateTime.month];
- }
- String getDateAndMonthAndYear(DateTime dateTime) {
- return dateTime.day.toString() +
- " " +
- _months[dateTime.month] +
- ", " +
- dateTime.year.toString();
- }
- String getDay(DateTime dateTime) {
- return _days[dateTime.weekday];
- }
- String getMonth(DateTime dateTime) {
- return _months[dateTime.month];
- }
- String getFullMonth(DateTime dateTime) {
- return _fullMonths[dateTime.month];
- }
- String getTime(DateTime dateTime) {
- final hours = dateTime.hour > 9
- ? dateTime.hour.toString()
- : "0" + dateTime.hour.toString();
- final minutes = dateTime.minute > 9
- ? dateTime.minute.toString()
- : "0" + dateTime.minute.toString();
- return hours + ":" + minutes;
- }
- String getFormattedTime(DateTime dateTime) {
- return getDay(dateTime) +
- ", " +
- getMonth(dateTime) +
- " " +
- dateTime.day.toString() +
- ", " +
- dateTime.year.toString() +
- " - " +
- getTime(dateTime);
- }
- String getFormattedDate(DateTime dateTime) {
- return getDay(dateTime) +
- ", " +
- getMonth(dateTime) +
- " " +
- dateTime.day.toString() +
- ", " +
- dateTime.year.toString();
- }
- String daysLeft(int futureTime) {
- int daysLeft = ((futureTime - DateTime.now().microsecondsSinceEpoch) /
- Duration.microsecondsPerDay)
- .ceil();
- return '$daysLeft day' + (daysLeft <= 1 ? "" : "s");
- }
- String formatDuration(Duration position) {
- final ms = position.inMilliseconds;
- int seconds = ms ~/ 1000;
- final int hours = seconds ~/ 3600;
- seconds = seconds % 3600;
- var minutes = seconds ~/ 60;
- seconds = seconds % 60;
- final hoursString = hours >= 10
- ? '$hours'
- : hours == 0
- ? '00'
- : '0$hours';
- final minutesString = minutes >= 10
- ? '$minutes'
- : minutes == 0
- ? '00'
- : '0$minutes';
- final secondsString = seconds >= 10
- ? '$seconds'
- : seconds == 0
- ? '00'
- : '0$seconds';
- final formattedTime =
- '${hoursString == '00' ? '' : hoursString + ':'}$minutesString:$secondsString';
- return formattedTime;
- }
- bool isLeapYear(DateTime dateTime) {
- final year = dateTime.year;
- if (year % 4 == 0) {
- if (year % 100 == 0) {
- if (year % 400 == 0) {
- return true;
- } else {
- return false;
- }
- } else {
- return true;
- }
- } else {
- return false;
- }
- }
- Widget getDayWidget(BuildContext context, int timestamp) {
- return Container(
- padding: const EdgeInsets.fromLTRB(10, 14, 0, 12),
- alignment: Alignment.centerLeft,
- child: Text(getDayTitle(timestamp),
- style: getDayTitle(timestamp) == "Today"
- ? Theme.of(context).textTheme.headline5
- : Theme.of(context).textTheme.caption.copyWith(
- fontSize: 16,
- fontWeight: FontWeight.w600,
- fontFamily: 'Inter-SemiBold')));
- }
- String getDayTitle(int timestamp) {
- final date = DateTime.fromMicrosecondsSinceEpoch(timestamp);
- final now = DateTime.now();
- var title = getDayAndMonth(date);
- if (date.year == now.year && date.month == now.month) {
- if (date.day == now.day) {
- title = "Today";
- } else if (date.day == now.day - 1) {
- title = "Yesterday";
- }
- }
- if (date.year != DateTime.now().year) {
- title += " " + date.year.toString();
- }
- return title;
- }
|