app/lib/screens/saved_runs_screen.dart
John Mizerek 85e8e1a290 Add saved runs feature with history, comparison, and about screen (v1.2)
Features:
- Save calculation runs with custom labels for later retrieval
- View saved runs list with dominant factor summary
- View detailed results of any saved run
- View original inputs in read-only mode
- Use any saved run as starting point for new calculation
- Compare two saved runs side-by-side
- About screen with app info, help links, privacy policy
- Metric/Imperial unit toggle persisted across sessions
- Info icon in app bar on all screens

Technical:
- New SavedRun model with full serialization
- SQLite saved_runs table with CRUD operations
- readOnly mode for all input screens
- Unit preference stored in LocalStorage
- Database schema upgraded to v2

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-21 09:59:58 -08:00

196 lines
5.4 KiB
Dart

import 'package:flutter/material.dart';
import '../models/models.dart';
import '../storage/local_storage.dart';
import '../theme.dart';
import 'about_screen.dart';
import 'saved_run_detail_screen.dart';
class SavedRunsScreen extends StatefulWidget {
const SavedRunsScreen({super.key});
@override
State<SavedRunsScreen> createState() => _SavedRunsScreenState();
}
class _SavedRunsScreenState extends State<SavedRunsScreen> {
List<SavedRun> _savedRuns = [];
bool _loading = true;
@override
void initState() {
super.initState();
_loadSavedRuns();
}
Future<void> _loadSavedRuns() async {
final runs = await LocalStorage.getSavedRuns();
setState(() {
_savedRuns = runs;
_loading = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Saved Runs'),
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () => Navigator.pop(context),
),
actions: [
IconButton(
icon: const Icon(Icons.info_outline),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (_) => const AboutScreen()),
),
),
],
),
body: _loading
? const Center(child: CircularProgressIndicator())
: _savedRuns.isEmpty
? _buildEmptyState()
: _buildRunsList(),
);
}
Widget _buildEmptyState() {
return Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.bookmark_outline,
size: 64,
color: AppColors.textSecondary.withAlpha(128),
),
const SizedBox(height: 16),
Text(
'No saved runs yet',
style: Theme.of(context).textTheme.titleMedium?.copyWith(
color: AppColors.textSecondary,
),
),
const SizedBox(height: 8),
Text(
'Save a calculation from the results screen to compare runs over time.',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: AppColors.textSecondary,
),
textAlign: TextAlign.center,
),
],
),
),
);
}
Widget _buildRunsList() {
return ListView.builder(
padding: const EdgeInsets.all(16),
itemCount: _savedRuns.length,
itemBuilder: (context, index) {
final run = _savedRuns[index];
return _buildRunCard(run);
},
);
}
Widget _buildRunCard(SavedRun run) {
return Card(
margin: const EdgeInsets.only(bottom: 12),
child: InkWell(
onTap: () => _openRunDetail(run),
borderRadius: BorderRadius.circular(12),
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
run.label,
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 4),
Text(
run.displayDate,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: AppColors.textSecondary,
),
),
const SizedBox(height: 8),
Text(
run.dominantFactorSummary,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: AppColors.primary,
),
),
],
),
),
IconButton(
icon: const Icon(Icons.delete_outline),
color: AppColors.error,
onPressed: () => _confirmDelete(run),
),
],
),
),
),
);
}
void _openRunDetail(SavedRun run) async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (_) => SavedRunDetailScreen(savedRun: run),
),
);
// Refresh list in case label was edited
_loadSavedRuns();
}
void _confirmDelete(SavedRun run) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Delete Run?'),
content: Text('Delete "${run.label}"? This cannot be undone.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
Navigator.pop(context);
_deleteRun(run);
},
style: TextButton.styleFrom(foregroundColor: AppColors.error),
child: const Text('Delete'),
),
],
),
);
}
Future<void> _deleteRun(SavedRun run) async {
await LocalStorage.deleteSavedRun(run.id);
_loadSavedRuns();
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Run deleted')),
);
}
}
}