import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../providers/scanner_provider.dart'; import '../widgets/camera_preview_widget.dart'; import '../widgets/display_config_dialog.dart'; import 'dart:io' show File; import 'package:flutter/foundation.dart'; class ScannerScreen extends ConsumerStatefulWidget { const ScannerScreen({super.key}); @override ConsumerState createState() => _ScannerScreenState(); } class _ScannerScreenState extends ConsumerState { @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { ref.read(scannerProvider.notifier).initialize(); }); } @override Widget build(BuildContext context) { final state = ref.watch(scannerProvider); return Scaffold( backgroundColor: Colors.black, appBar: AppBar( title: const Text('FingerScanner'), backgroundColor: Colors.transparent, elevation: 0, actions: [ IconButton( icon: const Icon(Icons.settings, color: Colors.white), onPressed: () { showDialog( context: context, builder: (context) => const DisplayConfigDialog(), ); }, tooltip: "Display Settings", ), if (state is ScannerReady && !state.isSimulation) IconButton( icon: const Icon(Icons.bug_report, color: Colors.white54), onPressed: () => ref.read(scannerProvider.notifier).startSimulation(), tooltip: "Switch to Simulation", ), ], ), body: Builder( builder: (context) { if (state is ScannerInitial || state is ScannerInitializing) { return const Center(child: CircularProgressIndicator()); } else if (state is ScannerReady) { final canCapture = state.canCapture; return Stack( children: [ CameraPreviewWidget( controller: state.controller, qualityStatus: state.qualityStatus, consecutivePasses: state.consecutivePasses, canCapture: canCapture, ), Positioned( bottom: 30, left: 0, right: 0, child: Center( child: FloatingActionButton.large( onPressed: canCapture ? () => ref.read(scannerProvider.notifier).scan() : null, backgroundColor: canCapture ? Colors.white : Colors.grey.shade800, child: Icon( Icons.camera_alt, color: canCapture ? Colors.black : Colors.grey, ), ), ), ), ], ); } else if (state is ScannerScanning) { return Stack( children: [ CameraPreviewWidget(controller: state.controller), Container( color: Colors.black.withValues(alpha: 0.5), child: const Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ CircularProgressIndicator(color: Colors.greenAccent), SizedBox(height: 20), Text( "Capturing...", style: TextStyle(color: Colors.white), ), ], ), ), ), ], ); } else if (state is ScannerSuccess) { final isSimulation = state.result.imagePath == "simulated_capture.jpg"; return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon(Icons.check_circle, color: Colors.green, size: 80), const SizedBox(height: 20), const Text( "Scan Complete!", style: TextStyle(color: Colors.white, fontSize: 24), ), const SizedBox(height: 10), Text( "Saved to: ${state.result.imagePath}", style: const TextStyle(color: Colors.grey), textAlign: TextAlign.center, ), const SizedBox(height: 30), SizedBox( height: 200, child: isSimulation ? const Icon(Icons.image, size: 100, color: Colors.grey) : (kIsWeb ? Image.network(state.result.imagePath) : Image.file(File(state.result.imagePath))), ), const SizedBox(height: 30), ElevatedButton( onPressed: () { ref.read(scannerProvider.notifier).reset(); }, child: const Text("Scan Again"), ), ], ), ); } else if (state is ScannerError) { return Center( child: Padding( padding: const EdgeInsets.all(32.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon( Icons.no_photography, color: Colors.red, size: 64, ), const SizedBox(height: 24), Text( state.message, style: const TextStyle(color: Colors.white, fontSize: 14), textAlign: TextAlign.center, ), const SizedBox(height: 32), ElevatedButton.icon( icon: const Icon(Icons.refresh), label: const Text("Retry Camera"), onPressed: () => ref.read(scannerProvider.notifier).tryCamera(), style: ElevatedButton.styleFrom( backgroundColor: Colors.white, foregroundColor: Colors.black, padding: const EdgeInsets.symmetric( horizontal: 24, vertical: 12, ), ), ), const SizedBox(height: 16), TextButton( onPressed: () => ref.read(scannerProvider.notifier).startSimulation(), child: const Text( "Run Simulation Mode Instead", style: TextStyle(color: Colors.white54), ), ), ], ), ), ); } return const SizedBox.shrink(); }, ), ); } }