import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:intl/intl.dart'; import 'package:kooltab2/views/widgets/closed_tab_card.dart'; import 'package:provider/provider.dart'; import '../app/router.dart'; import '../models/closed_tab.dart'; import '../models/closed_tab_item.dart'; import '../viewmodels/history_view_model.dart'; class HistoryScreenView extends StatefulWidget { const HistoryScreenView({super.key}); @override State createState() => _HistoryScreenViewState(); } class _HistoryScreenViewState extends State with RouteAware { @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { context.read().load(); }); } @override void didChangeDependencies() { super.didChangeDependencies(); routeObserver.subscribe(this, ModalRoute.of(context)! as PageRoute); } @override void didPopNext() { // Called when you come back to this screen context.read().load(); } @override void dispose() { routeObserver.unsubscribe(this); super.dispose(); } @override Widget build(BuildContext context) { final viewModel = context.watch(); return Scaffold( appBar: AppBar( title: Row( children: [ IconButton( onPressed: () { context.go('/bar'); }, icon: const Icon(Icons.arrow_back), ), const SizedBox(width: 5), const Text('Tab History'), ], ), actionsPadding: const EdgeInsets.symmetric(horizontal: 8), actions: [ IconButton( tooltip: 'Refresh', onPressed: viewModel.load, icon: const Icon(Icons.refresh_rounded), ), const SizedBox(width: 8), ], ), body: Builder( builder: (context) { if (viewModel.isLoading && !viewModel.hasLoaded) { return const Center( child: CircularProgressIndicator(strokeWidth: 2.5), ); } if (viewModel.errorMessage != null) { return Center( child: Padding( padding: const EdgeInsets.all(24), child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon( Icons.error_outline_rounded, size: 40, color: Theme.of(context).colorScheme.error, ), const SizedBox(height: 12), Text( viewModel.errorMessage!, textAlign: TextAlign.center, style: Theme.of(context).textTheme.bodyLarge, ), ], ), ), ); } return Column( children: [ Padding( padding: const EdgeInsets.fromLTRB(20, 16, 20, 8), child: TextField( onChanged: viewModel.search, decoration: const InputDecoration( hintText: 'Search by name…', prefixIcon: Icon(Icons.search_rounded, size: 20), isDense: true, ), ), ), Expanded( child: viewModel.closedTabs.isEmpty ? _EmptyState() : ListView.separated( padding: const EdgeInsets.fromLTRB(20, 8, 20, 20), itemCount: viewModel.closedTabs.length, separatorBuilder: (_, _) => const SizedBox(height: 10), itemBuilder: (context, index) { final closedTab = viewModel.closedTabs[index]; return ClosedTabCard(closedTab: closedTab); }, ), ), ], ); }, ), ); } } class _EmptyState extends StatelessWidget { @override Widget build(BuildContext context) { final scheme = Theme.of(context).colorScheme; return Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Container( padding: const EdgeInsets.all(20), decoration: BoxDecoration( shape: BoxShape.circle, color: scheme.onSurface.withValues(alpha: 0.05), ), child: Icon( Icons.history_rounded, size: 40, color: scheme.onSurface.withValues(alpha: 0.3), ), ), const SizedBox(height: 16), Text( 'No closed tabs yet', style: Theme.of(context).textTheme.titleMedium, ), const SizedBox(height: 4), Text( 'Tabs you close will show up here.', style: Theme.of(context).textTheme.bodyMedium, ), ], ), ); } }