Skip to content

Commit

Permalink
Add support for null dates
Browse files Browse the repository at this point in the history
  • Loading branch information
Process-ing committed Oct 29, 2024
1 parent 913b05f commit 0de771a
Showing 1 changed file with 79 additions and 48 deletions.
127 changes: 79 additions & 48 deletions packages/uni_ui/lib/calendar/calendar_item.dart
Original file line number Diff line number Diff line change
@@ -1,38 +1,100 @@
import 'package:figma_squircle/figma_squircle.dart';
import 'package:flutter/material.dart';

class _CalendarItemDate extends StatelessWidget {
const _CalendarItemDate({
required this.eventPeriod,
});

final DateTimeRange? eventPeriod;

@override
Widget build(BuildContext context) {
if (eventPeriod != null) {
return Column(
children: [
Text(
parsePeriod(eventPeriod!),
style: TextStyle(
fontSize: 15,
height: 1,
fontWeight: FontWeight.w500,
),
),
Text(
eventPeriod!.end.year.toString(),
style: TextStyle(
color: Theme.of(context).colorScheme.outline,
fontSize: 11,
),
),
],
);

} else {
return Padding(
padding: EdgeInsets.only(top: 10),
child: Text(
"TBD",
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
),
),
);
}
}

static String monthToString(int month) {
// TODO: Support Portuguese
const strMonths = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
];
return strMonths[month - 1];
}

static String parsePeriod(DateTimeRange period) {
final start = period.start, end = period.end;

if (start.month == end.month) {
return start.day == end.day
? "${start.day} ${monthToString(start.month)}"
: "${start.day} - ${end.day} ${monthToString(start.month)}";
} else {
return "${start.day} ${monthToString(start.month)} - ${end.day} ${monthToString(end.month)}";
}
}
}

class CalendarItem extends StatelessWidget {
const CalendarItem({
super.key,
required this.eventName,
required this.eventPeriod,
this.eventPeriod,
this.onTap,
});

final String eventName;
final DateTimeRange eventPeriod;
final DateTimeRange? eventPeriod;
final void Function()? onTap;

@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
parsePeriod(eventPeriod),
style: TextStyle(
fontSize: 15,
height: 1,
fontWeight: FontWeight.w500,
),
),
Text(
eventPeriod.end.year.toString(),
style: TextStyle(
color: Theme.of(context).colorScheme.outline,
fontSize: 11,
),
),
_CalendarItemDate(eventPeriod: eventPeriod),
Stack(
alignment: Alignment.bottomCenter,
children: [
Expand Down Expand Up @@ -96,35 +158,4 @@ class CalendarItem extends StatelessWidget {
],
);
}

static String monthToString(int month) {
// TODO: Support English
const strMonths = [
"Jan",
"Fev",
"Mar",
"Abr",
"Mai",
"Jun",
"Jul",
"Ago",
"Set",
"Out",
"Nov",
"Dec"
];
return strMonths[month - 1];
}

static String parsePeriod(DateTimeRange period) {
final start = period.start, end = period.end;

if (start.month == end.month) {
return start.day == end.day
? "${start.day} ${monthToString(start.month)}"
: "${start.day} - ${end.day} ${monthToString(start.month)}";
} else {
return "${start.day} ${monthToString(start.month)} - ${end.day} ${monthToString(end.month)}";
}
}
}

0 comments on commit 0de771a

Please sign in to comment.