import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../providers/auth_provider.dart'; import '../providers/settings_provider.dart'; import '../services/api_service.dart'; import 'register_page.dart'; import 'settings_page.dart'; class LoginPage extends StatefulWidget { @override _LoginPageState createState() => _LoginPageState(); } class _LoginPageState extends State { final _usernameController = TextEditingController(); final _passwordController = TextEditingController(); bool _isLoading = false; void _handleLogin() async { if (_usernameController.text.isEmpty || _passwordController.text.isEmpty) { ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Please fill in all fields"))); return; } setState(() => _isLoading = true); final settings = context.read(); final auth = context.read(); final api = ApiService(settings, null); try { final response = await api.login(_usernameController.text, _passwordController.text); if (response.statusCode == 200) { final data = jsonDecode(response.body); await auth.login(data['token']); } else { final error = jsonDecode(response.body)['error'] ?? "Login Failed"; ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(error))); } } catch (e) { ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Network Error: Could not connect to server"))); } finally { if (mounted) setState(() => _isLoading = false); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( actions: [ IconButton( icon: Icon(Icons.settings), onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (_) => SettingsPage())), ), ], ), body: Center( child: SingleChildScrollView( padding: const EdgeInsets.symmetric(horizontal: 32.0), child: ConstrainedBox( constraints: BoxConstraints(maxWidth: 400), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ // Logo & Name Icon(Icons.flutter_dash, size: 80, color: Theme.of(context).colorScheme.primary), SizedBox(height: 16), Text( "HIGHTUBE", style: TextStyle( fontSize: 32, fontWeight: FontWeight.bold, letterSpacing: 4, color: Theme.of(context).colorScheme.primary, ), ), Text("Open Source Live Platform", style: TextStyle(color: Colors.grey)), SizedBox(height: 48), // Fields TextField( controller: _usernameController, decoration: InputDecoration( labelText: "Username", prefixIcon: Icon(Icons.person), border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)), ), ), SizedBox(height: 16), TextField( controller: _passwordController, obscureText: true, decoration: InputDecoration( labelText: "Password", prefixIcon: Icon(Icons.lock), border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)), ), ), SizedBox(height: 32), // Login Button SizedBox( width: double.infinity, height: 50, child: ElevatedButton( onPressed: _isLoading ? null : _handleLogin, style: ElevatedButton.styleFrom( shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), ), child: _isLoading ? CircularProgressIndicator() : Text("LOGIN", style: TextStyle(fontWeight: FontWeight.bold)), ), ), SizedBox(height: 16), // Register Link TextButton( onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (_) => RegisterPage())), child: Text("Don't have an account? Create one"), ), ], ), ), ), ), ); } }