import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../providers/settings_provider.dart'; import '../services/api_service.dart'; class RegisterPage extends StatefulWidget { @override _RegisterPageState createState() => _RegisterPageState(); } class _RegisterPageState extends State { final _usernameController = TextEditingController(); final _passwordController = TextEditingController(); bool _isLoading = false; void _handleRegister() 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 api = ApiService(settings, null); try { final response = await api.register(_usernameController.text, _passwordController.text); if (response.statusCode == 201) { ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Account created! Please login."))); Navigator.pop(context); } else { final error = jsonDecode(response.body)['error'] ?? "Registration Failed"; ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(error))); } } catch (e) { ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Network Error"))); } finally { if (mounted) setState(() => _isLoading = false); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text("Create Account")), body: Center( child: SingleChildScrollView( padding: const EdgeInsets.symmetric(horizontal: 32.0), child: ConstrainedBox( constraints: BoxConstraints(maxWidth: 400), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(Icons.person_add_outlined, size: 64, color: Theme.of(context).colorScheme.primary), SizedBox(height: 24), Text( "Join Hightube", style: Theme.of(context).textTheme.headlineMedium?.copyWith(fontWeight: FontWeight.bold), ), SizedBox(height: 48), TextField( controller: _usernameController, decoration: InputDecoration( labelText: "Desired 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), SizedBox( width: double.infinity, height: 50, child: ElevatedButton( onPressed: _isLoading ? null : _handleRegister, style: ElevatedButton.styleFrom( shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), ), child: _isLoading ? CircularProgressIndicator() : Text("REGISTER", style: TextStyle(fontWeight: FontWeight.bold)), ), ), SizedBox(height: 16), TextButton( onPressed: () => Navigator.pop(context), child: Text("Already have an account? Login here"), ), ], ), ), ), ), ); } }