import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'home_screen.dart'; class AuthScreen extends StatefulWidget { const AuthScreen({super.key}); @override State createState() => _AuthScreenState(); } class _AuthScreenState extends State { final _controller = TextEditingController(); final _formKey = GlobalKey(); bool _isLoading = false; String _error = ''; // Secret key for accessing the web app static const String _secretKey = 'DocRec2025TestKey!@#'; @override void initState() { super.initState(); _checkAuthStatus(); } Future _checkAuthStatus() async { final prefs = await SharedPreferences.getInstance(); final isAuthenticated = prefs.getBool('is_authenticated') ?? false; if (isAuthenticated && mounted) { Navigator.of(context).pushReplacement( MaterialPageRoute(builder: (_) => const HomeScreen()), ); } } Future _authenticate() async { if (!_formKey.currentState!.validate()) return; setState(() { _isLoading = true; _error = ''; }); await Future.delayed(const Duration(milliseconds: 500)); // Simulate network delay if (_controller.text.trim() == _secretKey) { final prefs = await SharedPreferences.getInstance(); await prefs.setBool('is_authenticated', true); if (mounted) { Navigator.of(context).pushReplacement( MaterialPageRoute(builder: (_) => const HomeScreen()), ); } } else { setState(() { _error = '密钥错误,请检查后重试'; _isLoading = false; }); } } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Container( constraints: const BoxConstraints(maxWidth: 400), padding: const EdgeInsets.all(24.0), child: Card( child: Padding( padding: const EdgeInsets.all(32.0), child: Form( key: _formKey, child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Icon( Icons.lock_outline, size: 64, color: Theme.of(context).primaryColor, ), const SizedBox(height: 24), Text( 'DocRec 访问验证', style: Theme.of(context).textTheme.headlineSmall, textAlign: TextAlign.center, ), const SizedBox(height: 8), Text( '请输入访问密钥以继续', style: Theme.of(context).textTheme.bodyMedium?.copyWith( color: Colors.grey[600], ), textAlign: TextAlign.center, ), const SizedBox(height: 32), TextFormField( controller: _controller, obscureText: true, decoration: const InputDecoration( labelText: '访问密钥', hintText: '请输入密钥', prefixIcon: Icon(Icons.key), ), validator: (value) { if (value == null || value.trim().isEmpty) { return '请输入访问密钥'; } return null; }, onFieldSubmitted: (_) => _authenticate(), ), if (_error.isNotEmpty) ...[ const SizedBox(height: 16), Text( _error, style: TextStyle( color: Theme.of(context).colorScheme.error, fontSize: 14, ), textAlign: TextAlign.center, ), ], const SizedBox(height: 24), ElevatedButton( onPressed: _isLoading ? null : _authenticate, child: _isLoading ? const SizedBox( height: 20, width: 20, child: CircularProgressIndicator(strokeWidth: 2), ) : const Text('验证访问'), ), ], ), ), ), ), ), ), ); } @override void dispose() { _controller.dispose(); super.dispose(); } }