Files
transfer_sms/app/lib/screens/login_screen.dart
2026-04-28 17:34:03 +08:00

122 lines
4.2 KiB
Dart

import 'package:flutter/material.dart';
import '../main.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({super.key});
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _serverCtrl = TextEditingController(text: 'http://192.168.1.100:3000');
final _userCtrl = TextEditingController();
final _passCtrl = TextEditingController();
bool _loading = false;
String? _error;
Future<void> _submit(bool register) async {
setState(() { _loading = true; _error = null; });
final url = _serverCtrl.text.trim();
final user = _userCtrl.text.trim();
final pass = _passCtrl.text.trim();
try {
apiService.configure(url, '');
final token = register
? await apiService.register(user, pass)
: await apiService.login(user, pass);
apiService.configure(url, token);
await settings.setServerUrl(url);
await settings.setToken(token);
await apiService.registerDevice('Android');
if (mounted) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const HomeScreen()),
);
}
} catch (e) {
setState(() => _error = e.toString().replaceFirst('Exception: ', ''));
}
setState(() => _loading = false);
}
@override
Widget build(BuildContext context) {
final colors = Theme.of(context).colorScheme;
return Scaffold(
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 400),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.sms_outlined, size: 64, color: colors.primary),
const SizedBox(height: 16),
Text('SMS Monitor', style: Theme.of(context).textTheme.headlineMedium),
const SizedBox(height: 32),
TextField(
controller: _serverCtrl,
decoration: const InputDecoration(
labelText: 'Server URL',
hintText: 'http://your-server:3000',
prefixIcon: Icon(Icons.dns_outlined),
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
TextField(
controller: _userCtrl,
decoration: const InputDecoration(
labelText: 'Username',
prefixIcon: Icon(Icons.person_outline),
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
TextField(
controller: _passCtrl,
obscureText: true,
decoration: const InputDecoration(
labelText: 'Password',
prefixIcon: Icon(Icons.lock_outline),
border: OutlineInputBorder(),
),
onSubmitted: (_) => _submit(false),
),
if (_error != null) ...[
const SizedBox(height: 12),
Text(_error!, style: TextStyle(color: colors.error)),
],
const SizedBox(height: 24),
Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: _loading ? null : () => _submit(true),
child: const Text('Register'),
),
),
const SizedBox(width: 16),
Expanded(
child: FilledButton(
onPressed: _loading ? null : () => _submit(false),
child: _loading
? const SizedBox(height: 20, width: 20, child: CircularProgressIndicator(strokeWidth: 2))
: const Text('Sign In'),
),
),
],
),
],
),
),
),
),
);
}
}