add support for badges and notifications, adjust nav pages
All checks were successful
All checks were successful
This commit is contained in:
@@ -36,7 +36,6 @@ class _LatestLocoChangesPanelState extends State<LatestLocoChangesPanel> {
|
||||
final data = context.watch<DataService>();
|
||||
final changes = data.latestLocoChanges;
|
||||
final isLoading = data.isLatestLocoChangesLoading;
|
||||
final hasMore = data.latestLocoChangesHasMore;
|
||||
final textTheme = Theme.of(context).textTheme;
|
||||
|
||||
return Card(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:mileograph_flutter/services/authservice.dart';
|
||||
import 'package:mileograph_flutter/components/pages/settings.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class LoginScreen extends StatefulWidget {
|
||||
@@ -26,7 +27,7 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
if (!valid) return;
|
||||
await auth.tryRestoreSession();
|
||||
if (!mounted) return;
|
||||
context.go('/');
|
||||
context.go('/dashboard');
|
||||
} finally {
|
||||
if (mounted) setState(() => _checkingSession = false);
|
||||
}
|
||||
@@ -85,7 +86,14 @@ class _LoginScreenState extends State<LoginScreen> {
|
||||
IconButton(
|
||||
icon: const Icon(Icons.settings, color: Colors.grey),
|
||||
tooltip: 'Settings',
|
||||
onPressed: () => context.go('/settings'),
|
||||
onPressed: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
fullscreenDialog: true,
|
||||
builder: (_) => const SettingsPage(),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -179,6 +187,7 @@ class _LoginPanelContentState extends State<LoginPanelContent> {
|
||||
setState(() {
|
||||
_loggingIn = false;
|
||||
});
|
||||
context.go('/dashboard');
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
|
||||
@@ -509,7 +509,7 @@ class _DashboardState extends State<Dashboard> {
|
||||
icon: Icons.bookmark,
|
||||
title: 'Trips',
|
||||
action: TextButton(
|
||||
onPressed: () => context.push('/trips'),
|
||||
onPressed: () => context.push('/logbook/trips'),
|
||||
child: const Text('View all'),
|
||||
),
|
||||
child: trips.isEmpty
|
||||
|
||||
48
lib/components/pages/logbook.dart
Normal file
48
lib/components/pages/logbook.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:mileograph_flutter/components/pages/legs.dart';
|
||||
import 'package:mileograph_flutter/components/pages/trips.dart';
|
||||
|
||||
enum LogbookTab { entries, trips }
|
||||
|
||||
class LogbookPage extends StatelessWidget {
|
||||
const LogbookPage({super.key, this.initialTab = LogbookTab.entries});
|
||||
|
||||
final LogbookTab initialTab;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final initialIndex = initialTab == LogbookTab.trips ? 1 : 0;
|
||||
return DefaultTabController(
|
||||
key: ValueKey(initialTab),
|
||||
initialIndex: initialIndex,
|
||||
length: 2,
|
||||
child: Column(
|
||||
children: [
|
||||
TabBar(
|
||||
onTap: (index) {
|
||||
final dest =
|
||||
index == 0 ? '/logbook/entries' : '/logbook/trips';
|
||||
final current = GoRouterState.of(context).uri.path;
|
||||
if (current != dest) {
|
||||
context.go(dest);
|
||||
}
|
||||
},
|
||||
tabs: const [
|
||||
Tab(text: 'Entries'),
|
||||
Tab(text: 'Trips'),
|
||||
],
|
||||
),
|
||||
Expanded(
|
||||
child: TabBarView(
|
||||
children: const [
|
||||
LegsPage(),
|
||||
TripsPage(),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
68
lib/components/pages/more.dart
Normal file
68
lib/components/pages/more.dart
Normal file
@@ -0,0 +1,68 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mileograph_flutter/components/pages/profile.dart';
|
||||
import 'package:mileograph_flutter/components/pages/settings.dart';
|
||||
|
||||
class MorePage extends StatelessWidget {
|
||||
const MorePage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Navigator(
|
||||
onGenerateRoute: (settings) {
|
||||
final name = settings.name ?? '/';
|
||||
Widget page;
|
||||
switch (name) {
|
||||
case '/settings':
|
||||
page = const SettingsPage();
|
||||
break;
|
||||
case '/profile':
|
||||
page = const ProfilePage();
|
||||
break;
|
||||
case '/more/settings':
|
||||
page = const SettingsPage();
|
||||
break;
|
||||
case '/more/profile':
|
||||
page = const ProfilePage();
|
||||
break;
|
||||
case '/':
|
||||
default:
|
||||
page = _MoreHome();
|
||||
}
|
||||
return MaterialPageRoute(builder: (_) => page, settings: settings);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MoreHome extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
Text(
|
||||
'More',
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Card(
|
||||
child: Column(
|
||||
children: [
|
||||
ListTile(
|
||||
leading: const Icon(Icons.emoji_events),
|
||||
title: const Text('Badges'),
|
||||
onTap: () => Navigator.of(context).pushNamed('/more/profile'),
|
||||
),
|
||||
const Divider(height: 1),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.settings),
|
||||
title: const Text('Settings'),
|
||||
onTap: () => Navigator.of(context).pushNamed('/more/settings'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -120,6 +120,7 @@ extension _NewEntrySubmitLogic on _NewEntryPageState {
|
||||
}
|
||||
if (!mounted) return;
|
||||
dataService.refreshLegs();
|
||||
await dataService.fetchNotifications();
|
||||
if (!mounted) return;
|
||||
messenger?.showSnackBar(
|
||||
SnackBar(
|
||||
|
||||
207
lib/components/pages/profile.dart
Normal file
207
lib/components/pages/profile.dart
Normal file
@@ -0,0 +1,207 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:mileograph_flutter/objects/objects.dart';
|
||||
import 'package:mileograph_flutter/services/data_service.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class ProfilePage extends StatefulWidget {
|
||||
const ProfilePage({super.key});
|
||||
|
||||
@override
|
||||
State<ProfilePage> createState() => _ProfilePageState();
|
||||
}
|
||||
|
||||
class _ProfilePageState extends State<ProfilePage> {
|
||||
bool _initialised = false;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
if (_initialised) return;
|
||||
_initialised = true;
|
||||
_refreshAwards();
|
||||
}
|
||||
|
||||
Future<void> _refreshAwards() {
|
||||
return context.read<DataService>().fetchBadgeAwards();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final data = context.watch<DataService>();
|
||||
final awards = data.badgeAwards;
|
||||
final loading = data.isBadgeAwardsLoading;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Badges'),
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () {
|
||||
final navigator = Navigator.of(context);
|
||||
if (navigator.canPop()) {
|
||||
navigator.pop();
|
||||
} else {
|
||||
context.go('/');
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
body: RefreshIndicator(
|
||||
onRefresh: _refreshAwards,
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
if (loading && awards.isEmpty)
|
||||
const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 24.0),
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
)
|
||||
else if (awards.isEmpty)
|
||||
const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 12.0),
|
||||
child: Text('No badges awarded yet.'),
|
||||
)
|
||||
else
|
||||
...awards.map((award) => _buildAwardCard(context, award)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildAwardCard(BuildContext context, BadgeAward award) {
|
||||
final badgeName = _formatBadgeName(award.badgeCode);
|
||||
final tier = award.badgeTier.isNotEmpty
|
||||
? award.badgeTier[0].toUpperCase() + award.badgeTier.substring(1)
|
||||
: '';
|
||||
final tierIcon = _buildTierIcon(award.badgeTier);
|
||||
final scope = _scopeToShow(award);
|
||||
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
if (tierIcon != null) ...[
|
||||
tierIcon,
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
Expanded(
|
||||
child: Text(
|
||||
'$badgeName • $tier',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (award.awardedAt != null)
|
||||
Text(
|
||||
_formatAwardDate(award.awardedAt!),
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
if (scope != null && scope.isNotEmpty) ...[
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
scope,
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
],
|
||||
if (award.loco != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
_buildLocoInfo(context, award.loco!),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLocoInfo(BuildContext context, LocoSummary loco) {
|
||||
final lines = <String>[];
|
||||
final classNum = [
|
||||
if (loco.locoClass.isNotEmpty) loco.locoClass,
|
||||
if (loco.number.isNotEmpty) loco.number,
|
||||
].join(' ');
|
||||
if (classNum.isNotEmpty) lines.add(classNum);
|
||||
if ((loco.name ?? '').isNotEmpty) lines.add(loco.name!);
|
||||
if ((loco.livery ?? '').isNotEmpty) lines.add(loco.livery!);
|
||||
if ((loco.location ?? '').isNotEmpty) lines.add(loco.location!);
|
||||
|
||||
return Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Icon(Icons.train, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: lines.map((line) {
|
||||
return Text(
|
||||
line,
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
String _formatBadgeName(String code) {
|
||||
if (code.isEmpty) return 'Badge';
|
||||
const known = {
|
||||
'class_clearance': 'Class Clearance',
|
||||
'loco_clearance': 'Loco Clearance',
|
||||
};
|
||||
final lower = code.toLowerCase();
|
||||
if (known.containsKey(lower)) return known[lower]!;
|
||||
final parts = code.split(RegExp(r'[_\\s]+')).where((p) => p.isNotEmpty);
|
||||
return parts
|
||||
.map((p) => p[0].toUpperCase() + p.substring(1).toLowerCase())
|
||||
.join(' ');
|
||||
}
|
||||
|
||||
String _formatAwardDate(DateTime date) {
|
||||
final y = date.year.toString().padLeft(4, '0');
|
||||
final m = date.month.toString().padLeft(2, '0');
|
||||
final d = date.day.toString().padLeft(2, '0');
|
||||
return '$y-$m-$d';
|
||||
}
|
||||
|
||||
Widget? _buildTierIcon(String tier) {
|
||||
final lower = tier.toLowerCase();
|
||||
Color? color;
|
||||
switch (lower) {
|
||||
case 'bronze':
|
||||
color = const Color(0xFFCD7F32);
|
||||
break;
|
||||
case 'silver':
|
||||
color = const Color(0xFFC0C0C0);
|
||||
break;
|
||||
case 'gold':
|
||||
color = const Color(0xFFFFD700);
|
||||
break;
|
||||
}
|
||||
if (color == null) return null;
|
||||
return Icon(Icons.emoji_events, color: color);
|
||||
}
|
||||
|
||||
String? _scopeToShow(BadgeAward award) {
|
||||
final scope = award.scopeValue?.trim() ?? '';
|
||||
if (scope.isEmpty) return null;
|
||||
final code = award.badgeCode.toLowerCase();
|
||||
if (code == 'loco_clearance') {
|
||||
// Hide numeric loco IDs; loco details are shown separately.
|
||||
if (int.tryParse(scope) != null) return null;
|
||||
}
|
||||
return scope;
|
||||
}
|
||||
}
|
||||
@@ -244,47 +244,7 @@ class _TractionPageState extends State<TractionPage> {
|
||||
],
|
||||
),
|
||||
),
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
tooltip: 'Refresh',
|
||||
onPressed: _refreshTraction,
|
||||
icon: const Icon(Icons.refresh),
|
||||
),
|
||||
if (_hasClassQuery) ...[
|
||||
const SizedBox(width: 8),
|
||||
FilledButton.tonalIcon(
|
||||
onPressed: _toggleClassStatsPanel,
|
||||
icon: Icon(
|
||||
_showClassStatsPanel ? Icons.bar_chart : Icons.insights,
|
||||
),
|
||||
label: Text(
|
||||
_showClassStatsPanel ? 'Hide class stats' : 'Class stats',
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(width: 8),
|
||||
FilledButton.icon(
|
||||
onPressed: () async {
|
||||
final createdClass = await context.push<String>(
|
||||
'/traction/new',
|
||||
);
|
||||
if (createdClass != null && createdClass.isNotEmpty) {
|
||||
_classController.text = createdClass;
|
||||
_selectedClass = createdClass;
|
||||
if (mounted) {
|
||||
_refreshTraction();
|
||||
}
|
||||
} else if (mounted && createdClass == '') {
|
||||
_refreshTraction();
|
||||
}
|
||||
},
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('New Traction'),
|
||||
),
|
||||
],
|
||||
),
|
||||
_buildHeaderActions(context, isMobile),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
@@ -546,6 +506,78 @@ class _TractionPageState extends State<TractionPage> {
|
||||
return (_selectedClass ?? _classController.text).trim().isNotEmpty;
|
||||
}
|
||||
|
||||
Widget _buildHeaderActions(BuildContext context, bool isMobile) {
|
||||
final refreshButton = IconButton(
|
||||
tooltip: 'Refresh',
|
||||
onPressed: _refreshTraction,
|
||||
icon: const Icon(Icons.refresh),
|
||||
);
|
||||
|
||||
final classStatsButton = !_hasClassQuery
|
||||
? null
|
||||
: FilledButton.tonalIcon(
|
||||
onPressed: _toggleClassStatsPanel,
|
||||
icon: Icon(
|
||||
_showClassStatsPanel ? Icons.bar_chart : Icons.insights,
|
||||
),
|
||||
label: Text(
|
||||
_showClassStatsPanel ? 'Hide class stats' : 'Class stats',
|
||||
),
|
||||
);
|
||||
|
||||
final newTractionButton = FilledButton.icon(
|
||||
onPressed: () async {
|
||||
final createdClass = await context.push<String>(
|
||||
'/traction/new',
|
||||
);
|
||||
if (!mounted) return;
|
||||
if (createdClass != null && createdClass.isNotEmpty) {
|
||||
_classController.text = createdClass;
|
||||
_selectedClass = createdClass;
|
||||
_refreshTraction();
|
||||
} else if (createdClass == '') {
|
||||
_refreshTraction();
|
||||
}
|
||||
},
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('New Traction'),
|
||||
);
|
||||
|
||||
final desktopActions = [
|
||||
refreshButton,
|
||||
if (classStatsButton != null) classStatsButton,
|
||||
newTractionButton,
|
||||
];
|
||||
|
||||
final mobileActions = [
|
||||
newTractionButton,
|
||||
if (classStatsButton != null) classStatsButton,
|
||||
refreshButton,
|
||||
];
|
||||
|
||||
if (isMobile) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
for (var i = 0; i < mobileActions.length; i++) ...[
|
||||
if (i > 0) const SizedBox(height: 8),
|
||||
Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: mobileActions[i],
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
return Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: desktopActions,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _toggleClassStatsPanel() async {
|
||||
if (!_hasClassQuery) return;
|
||||
final targetState = !_showClassStatsPanel;
|
||||
|
||||
@@ -12,6 +12,7 @@ class TripsPage extends StatefulWidget {
|
||||
|
||||
class _TripsPageState extends State<TripsPage> {
|
||||
bool _initialised = false;
|
||||
final Map<int, Future<List<TripLocoStat>>> _tripLocoStatsFutures = {};
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
@@ -23,7 +24,13 @@ class _TripsPageState extends State<TripsPage> {
|
||||
}
|
||||
|
||||
Future<void> _refreshTrips() async {
|
||||
await context.read<DataService>().fetchTripDetails();
|
||||
_tripLocoStatsFutures.clear();
|
||||
final data = context.read<DataService>();
|
||||
await data.fetchTripDetails();
|
||||
if (!mounted) return;
|
||||
for (final trip in data.tripDetails) {
|
||||
_tripStatsFuture(trip.id);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _renameTrip(TripDetail trip, String newName) async {
|
||||
@@ -47,6 +54,13 @@ class _TripsPageState extends State<TripsPage> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<TripLocoStat>> _tripStatsFuture(int tripId) {
|
||||
return _tripLocoStatsFutures.putIfAbsent(
|
||||
tripId,
|
||||
() => context.read<DataService>().fetchTripLocoStats(tripId),
|
||||
);
|
||||
}
|
||||
|
||||
Future<String?> _promptTripName(BuildContext context, String initial) async {
|
||||
final controller = TextEditingController(text: initial);
|
||||
final newName = await showDialog<String>(
|
||||
@@ -80,7 +94,6 @@ class _TripsPageState extends State<TripsPage> {
|
||||
final data = context.watch<DataService>();
|
||||
final tripDetails = data.tripDetails;
|
||||
final tripSummaries = data.trips;
|
||||
final isMobile = MediaQuery.of(context).size.width < 700;
|
||||
final showLoading = data.isTripDetailsLoading && tripDetails.isEmpty;
|
||||
|
||||
return RefreshIndicator(
|
||||
@@ -171,92 +184,191 @@ class _TripsPageState extends State<TripsPage> {
|
||||
}
|
||||
|
||||
final trip = tripDetails[index - 1];
|
||||
return _buildTripCard(context, trip, isMobile);
|
||||
return _buildTripCard(context, trip);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTripCard(BuildContext context, TripDetail trip, bool isMobile) {
|
||||
Widget _buildTripCard(BuildContext context, TripDetail trip) {
|
||||
final legs = trip.legs;
|
||||
final legCount = trip.legCount > 0 ? trip.legCount : legs.length;
|
||||
final dateRange = _formatDateRange(legs);
|
||||
final endpoints = _formatEndpoints(legs);
|
||||
final statsFuture = _tripStatsFuture(trip.id);
|
||||
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12.0),
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Trip',
|
||||
style: Theme.of(context).textTheme.labelMedium,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
trip.name,
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Text(
|
||||
trip.name,
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
trip.mileage.toStringAsFixed(1),
|
||||
style:
|
||||
Theme.of(context).textTheme.headlineSmall?.copyWith(
|
||||
fontWeight: FontWeight.w800,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'${trip.mileage.toStringAsFixed(1)} mi · ${trip.legCount} legs',
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
],
|
||||
),
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.train),
|
||||
tooltip: 'Traction',
|
||||
onPressed: () => _showTripWinners(context, trip),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.open_in_new),
|
||||
tooltip: 'Details',
|
||||
onPressed: () => _showTripDetail(context, trip),
|
||||
'miles',
|
||||
style: Theme.of(context).textTheme.labelMedium?.copyWith(
|
||||
color: Theme.of(context).textTheme.bodySmall?.color,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
if (legs.isNotEmpty)
|
||||
Column(
|
||||
children: legs.take(isMobile ? 2 : 3).map((leg) {
|
||||
return ListTile(
|
||||
dense: isMobile,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
leading: const Icon(Icons.train),
|
||||
title: Text('${leg.start} → ${leg.end}'),
|
||||
subtitle: Text(
|
||||
_formatDate(leg.beginTime),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
trailing: Text(
|
||||
leg.mileage?.toStringAsFixed(1) ?? '-',
|
||||
style: Theme.of(context).textTheme.labelLarge?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
FutureBuilder<List<TripLocoStat>>(
|
||||
future: statsFuture,
|
||||
builder: (context, snapshot) {
|
||||
final chips = <Widget>[
|
||||
_buildMetaChip(context, Icons.timeline, '$legCount legs'),
|
||||
if (dateRange != null)
|
||||
_buildMetaChip(context, Icons.calendar_month, dateRange),
|
||||
if (endpoints != null)
|
||||
_buildMetaChip(context, Icons.route, endpoints),
|
||||
];
|
||||
|
||||
final stats = snapshot.data ?? const [];
|
||||
final hasStats = stats.isNotEmpty;
|
||||
final loading =
|
||||
snapshot.connectionState == ConnectionState.waiting;
|
||||
|
||||
if (loading && !hasStats) {
|
||||
chips.add(
|
||||
_buildMetaChip(context, Icons.train, 'Loading traction...'),
|
||||
);
|
||||
} else if (hasStats) {
|
||||
final winnerCount = stats.where((e) => e.won).length;
|
||||
chips.add(
|
||||
_buildMetaChip(context, Icons.train, '${stats.length} had'),
|
||||
);
|
||||
chips.add(
|
||||
_buildMetaChip(
|
||||
context,
|
||||
Icons.emoji_events_outlined,
|
||||
'$winnerCount winners',
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
if (legs.length > 3)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 6.0),
|
||||
child: Text(
|
||||
'+${legs.length - 3} more legs',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
} else if (snapshot.connectionState == ConnectionState.done) {
|
||||
chips.add(
|
||||
_buildMetaChip(context, Icons.train, 'No traction yet'),
|
||||
);
|
||||
}
|
||||
|
||||
return Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: chips,
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
alignment: WrapAlignment.end,
|
||||
children: [
|
||||
OutlinedButton.icon(
|
||||
icon: const Icon(Icons.train),
|
||||
label: const Text('Locos'),
|
||||
onPressed: () => _showTripWinners(context, trip),
|
||||
),
|
||||
FilledButton.icon(
|
||||
icon: const Icon(Icons.open_in_new),
|
||||
label: const Text('Details'),
|
||||
onPressed: () => _showTripDetail(context, trip),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMetaChip(BuildContext context, IconData icon, String label) {
|
||||
return Chip(
|
||||
avatar: Icon(icon, size: 16),
|
||||
label: Text(label),
|
||||
visualDensity: const VisualDensity(horizontal: -2, vertical: -2),
|
||||
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
);
|
||||
}
|
||||
|
||||
String? _formatDateRange(List<TripLeg> legs) {
|
||||
final beginTimes =
|
||||
legs.map((e) => e.beginTime).whereType<DateTime>().toList();
|
||||
if (beginTimes.isEmpty) return null;
|
||||
final start = beginTimes.first;
|
||||
final end = beginTimes.last;
|
||||
final startStr = _formatFriendlyDate(start);
|
||||
final endStr = _formatFriendlyDate(end);
|
||||
if (startStr == endStr) return startStr;
|
||||
return '$startStr - $endStr';
|
||||
}
|
||||
|
||||
String _formatFriendlyDate(DateTime date) {
|
||||
const months = [
|
||||
'Jan',
|
||||
'Feb',
|
||||
'Mar',
|
||||
'Apr',
|
||||
'May',
|
||||
'Jun',
|
||||
'Jul',
|
||||
'Aug',
|
||||
'Sep',
|
||||
'Oct',
|
||||
'Nov',
|
||||
'Dec',
|
||||
];
|
||||
final day = date.day.toString().padLeft(2, '0');
|
||||
final monthIndex = (date.month - 1).clamp(0, months.length - 1).toInt();
|
||||
final month = months[monthIndex];
|
||||
return '$day $month ${date.year}';
|
||||
}
|
||||
|
||||
String? _formatEndpoints(List<TripLeg> legs) {
|
||||
if (legs.isEmpty) return null;
|
||||
final start = legs.first.start;
|
||||
final end = legs.last.end;
|
||||
if (start.isEmpty && end.isEmpty) return null;
|
||||
final startLabel = start.isNotEmpty ? start : '—';
|
||||
final endLabel = end.isNotEmpty ? end : '—';
|
||||
return '$startLabel → $endLabel';
|
||||
}
|
||||
|
||||
String _formatDate(DateTime? date) {
|
||||
if (date == null) return '';
|
||||
return '${date.year.toString().padLeft(4, '0')}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
||||
@@ -323,6 +435,7 @@ class _TripsPageState extends State<TripsPage> {
|
||||
data.fetchTripDetails(),
|
||||
data.fetchTrips(),
|
||||
]);
|
||||
_tripLocoStatsFutures.remove(trip.id);
|
||||
if (!mounted) return;
|
||||
messenger?.showSnackBar(
|
||||
SnackBar(content: Text('Deleted "${trip.name}"')),
|
||||
@@ -424,10 +537,9 @@ class _TripsPageState extends State<TripsPage> {
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
builder: (_) {
|
||||
final data = context.read<DataService>();
|
||||
return SafeArea(
|
||||
child: FutureBuilder<List<TripLocoStat>>(
|
||||
future: data.fetchTripLocoStats(trip.id),
|
||||
future: _tripStatsFuture(trip.id),
|
||||
builder: (ctx, snapshot) {
|
||||
final items = snapshot.data ?? [];
|
||||
final loading =
|
||||
|
||||
Reference in New Issue
Block a user