|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import '../services/api_service.dart'; |
| 3 | +import 'home_screen.dart'; |
| 4 | + |
| 5 | +class AuthScreen extends StatefulWidget { |
| 6 | + final ApiService api; |
| 7 | + const AuthScreen({super.key, required this.api}); |
| 8 | + |
| 9 | + @override |
| 10 | + State<AuthScreen> createState() => _AuthScreenState(); |
| 11 | +} |
| 12 | + |
| 13 | +class _AuthScreenState extends State<AuthScreen> { |
| 14 | + final _nameController = TextEditingController(); |
| 15 | + final _emailController = TextEditingController(); |
| 16 | + final _passwordController = TextEditingController(); |
| 17 | + String _message = ''; |
| 18 | + bool _loading = false; |
| 19 | + |
| 20 | + Future<void> _submit(bool isSignUp) async { |
| 21 | + setState(() { |
| 22 | + _loading = true; |
| 23 | + _message = ''; |
| 24 | + }); |
| 25 | + try { |
| 26 | + if (isSignUp) { |
| 27 | + await widget.api.signUp( |
| 28 | + _nameController.text, |
| 29 | + _emailController.text, |
| 30 | + _passwordController.text, |
| 31 | + ); |
| 32 | + } else { |
| 33 | + await widget.api.logIn(_emailController.text, _passwordController.text); |
| 34 | + } |
| 35 | + if (!mounted) return; |
| 36 | + Navigator.of(context).pushReplacement( |
| 37 | + MaterialPageRoute(builder: (_) => HomeScreen(api: widget.api)), |
| 38 | + ); |
| 39 | + } catch (e) { |
| 40 | + setState(() => _message = e.toString().replaceFirst('Exception: ', '')); |
| 41 | + } finally { |
| 42 | + setState(() => _loading = false); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @override |
| 47 | + Widget build(BuildContext context) { |
| 48 | + return Scaffold( |
| 49 | + backgroundColor: const Color(0xFF0B0F1A), |
| 50 | + body: SafeArea( |
| 51 | + child: Padding( |
| 52 | + padding: const EdgeInsets.all(24), |
| 53 | + child: Column( |
| 54 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 55 | + children: [ |
| 56 | + const Text( |
| 57 | + 'CipherStream', |
| 58 | + style: TextStyle( |
| 59 | + color: Colors.white, |
| 60 | + fontSize: 28, |
| 61 | + fontWeight: FontWeight.bold, |
| 62 | + ), |
| 63 | + ), |
| 64 | + const SizedBox(height: 4), |
| 65 | + const Text( |
| 66 | + 'free access — no unlocks needed during launch', |
| 67 | + style: TextStyle(color: Color(0xFF3ECF8E), fontSize: 13), |
| 68 | + ), |
| 69 | + const SizedBox(height: 32), |
| 70 | + _buildField(_nameController, 'Name'), |
| 71 | + _buildField(_emailController, 'Email'), |
| 72 | + _buildField(_passwordController, 'Password', obscure: true), |
| 73 | + const SizedBox(height: 16), |
| 74 | + Row( |
| 75 | + children: [ |
| 76 | + Expanded( |
| 77 | + child: ElevatedButton( |
| 78 | + onPressed: _loading ? null : () => _submit(true), |
| 79 | + child: const Text('Sign up'), |
| 80 | + ), |
| 81 | + ), |
| 82 | + const SizedBox(width: 12), |
| 83 | + Expanded( |
| 84 | + child: OutlinedButton( |
| 85 | + onPressed: _loading ? null : () => _submit(false), |
| 86 | + child: const Text('Log in'), |
| 87 | + ), |
| 88 | + ), |
| 89 | + ], |
| 90 | + ), |
| 91 | + const SizedBox(height: 12), |
| 92 | + if (_message.isNotEmpty) |
| 93 | + Text(_message, style: const TextStyle(color: Colors.redAccent)), |
| 94 | + ], |
| 95 | + ), |
| 96 | + ), |
| 97 | + ), |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + Widget _buildField(TextEditingController controller, String label, |
| 102 | + {bool obscure = false}) { |
| 103 | + return Padding( |
| 104 | + padding: const EdgeInsets.only(bottom: 12), |
| 105 | + child: TextField( |
| 106 | + controller: controller, |
| 107 | + obscureText: obscure, |
| 108 | + style: const TextStyle(color: Colors.white), |
| 109 | + decoration: InputDecoration( |
| 110 | + labelText: label, |
| 111 | + labelStyle: const TextStyle(color: Colors.grey), |
| 112 | + enabledBorder: OutlineInputBorder( |
| 113 | + borderRadius: BorderRadius.circular(8), |
| 114 | + borderSide: const BorderSide(color: Color(0xFF232C42)), |
| 115 | + ), |
| 116 | + ), |
| 117 | + ), |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
0 commit comments