display a maximum of three upcoming events on the home page

This commit is contained in:
Michael Thomas 2023-03-23 12:38:51 -04:00
parent cb9ccc1865
commit 662a1ae157
2 changed files with 24 additions and 19 deletions

View File

@ -83,15 +83,13 @@ class _HomeContentState extends State<HomeContent> {
), ),
const SizedBox(height: 20), const SizedBox(height: 20),
const HeaderWidget( const HeaderWidget(
title: "Today's Events", title: "Upcoming Events",
link: HeaderLink( link:
text: "View more", HeaderLink(text: "View more", href: EventsRoute()),
href: EventsRoute()
),
), ),
Padding( Padding(
padding: const EdgeInsets.symmetric(horizontal: 20), padding: const EdgeInsets.symmetric(horizontal: 20),
child: EventsList(), child: EventsList(limit: 3),
), ),
const HeaderWidget(title: "Food & Dining"), const HeaderWidget(title: "Food & Dining"),
const RestaurantsList(), const RestaurantsList(),

View File

@ -8,14 +8,17 @@ import 'event_card.dart';
class EventsList extends StatefulWidget { class EventsList extends StatefulWidget {
final DateTimeRange dateRange; final DateTimeRange dateRange;
final int? limit;
const EventsList._({ const EventsList._({
required this.dateRange, required this.dateRange,
this.limit,
Key? key, Key? key,
}) : super(key: key); }) : super(key: key);
factory EventsList({ factory EventsList({
DateTimeRange? dateRange, DateTimeRange? dateRange,
int? limit,
Key? key, Key? key,
}) { }) {
if (dateRange == null) { if (dateRange == null) {
@ -27,6 +30,7 @@ class EventsList extends StatefulWidget {
} }
return EventsList._( return EventsList._(
dateRange: dateRange, dateRange: dateRange,
limit: limit,
key: key, key: key,
); );
} }
@ -46,21 +50,24 @@ class _EventsListState extends State<EventsList> {
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.hasData) { if (snapshot.hasData) {
var events = snapshot.data!.where((event) { var events = snapshot.data!.where((event) {
return event.time.isAfter(widget.dateRange.start) && return event.time.isAfter(widget.dateRange.start) &&
event.time.isBefore(widget.dateRange.end); event.time.isBefore(widget.dateRange.end);
}); });
if (events.isNotEmpty) { if (widget.limit != null) {
return Column( events = events.take(widget.limit!);
children: events.map((event) { }
return Padding( if (events.isNotEmpty) {
padding: const EdgeInsets.only(bottom: 15), return Column(
child: EventCard(event), children: events.map((event) {
);
}).toList());
} else {
return Padding( return Padding(
padding: const EdgeInsets.only(bottom: 15), padding: const EdgeInsets.only(bottom: 15),
child: SizedBox( child: EventCard(event),
);
}).toList());
} else {
return Padding(
padding: const EdgeInsets.only(bottom: 15),
child: SizedBox(
width: double.infinity, width: double.infinity,
height: 50, height: 50,
child: Center( child: Center(