MotorSignal-App/lib/features/history/history_screen.dart

67 lines
2.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'dart:math' as math;
import '../../database/database.dart';
class HistoryScreen extends ConsumerWidget {
const HistoryScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final database = ref.watch(databaseProvider);
return Scaffold(
appBar: AppBar(title: const Text('Ride History')),
body: StreamBuilder<List<Ride>>(
stream: database.select(database.rides).watch(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
}
final rides = snapshot.data ?? [];
if (rides.isEmpty) {
return const Center(
child: Text(
'No recorded rides yet.',
style: TextStyle(fontSize: 18, color: Colors.grey),
),
);
}
return ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: rides.length,
itemBuilder: (context, index) {
final ride = rides[index];
return Card(
margin: const EdgeInsets.symmetric(vertical: 8),
child: ListTile(
title: Text('Ride ${ride.id}', style: const TextStyle(fontWeight: FontWeight.bold)),
subtitle: Text(
'Started: ${ride.startTime.toLocal().toString().split('.')[0]}\n'
'Duration: ${ride.endTime?.difference(ride.startTime).inMinutes ?? 0} mins',
),
trailing: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text('${ride.topSpeedMph.toStringAsFixed(1)} mph', style: const TextStyle(color: Colors.cyanAccent, fontWeight: FontWeight.bold)),
Text('${math.max(ride.maxLeanLeft, ride.maxLeanRight).toStringAsFixed(1)}° lean'),
],
),
),
);
},
);
},
),
);
}
}