108 lines
4.1 KiB
Dart
108 lines
4.1 KiB
Dart
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../l10n/app_localizations.dart';
|
|
import '../providers/settings_provider.dart';
|
|
import '../services/api_service.dart';
|
|
|
|
class RegisterPage extends StatefulWidget {
|
|
@override
|
|
_RegisterPageState createState() => _RegisterPageState();
|
|
}
|
|
|
|
class _RegisterPageState extends State<RegisterPage> {
|
|
final _usernameController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
bool _isLoading = false;
|
|
|
|
void _handleRegister() async {
|
|
final l10n = AppLocalizations.of(context)!;
|
|
if (_usernameController.text.isEmpty || _passwordController.text.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(l10n.fillAllFields)));
|
|
return;
|
|
}
|
|
|
|
setState(() => _isLoading = true);
|
|
final settings = context.read<SettingsProvider>();
|
|
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(l10n.accountCreated)));
|
|
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(l10n.networkError)));
|
|
} finally {
|
|
if (mounted) setState(() => _isLoading = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context)!;
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text(l10n.createAccount)),
|
|
body: Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.symmetric(horizontal: 32.0),
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: 400),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(Icons.person_add_outlined, size: 64, color: Theme.of(context).colorScheme.primary),
|
|
const SizedBox(height: 24),
|
|
Text(
|
|
l10n.joinHightube,
|
|
style: Theme.of(context).textTheme.headlineMedium?.copyWith(fontWeight: FontWeight.bold),
|
|
),
|
|
const SizedBox(height: 48),
|
|
TextField(
|
|
controller: _usernameController,
|
|
decoration: InputDecoration(
|
|
labelText: l10n.desiredUsername,
|
|
prefixIcon: const Icon(Icons.person),
|
|
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextField(
|
|
controller: _passwordController,
|
|
obscureText: true,
|
|
decoration: InputDecoration(
|
|
labelText: l10n.password,
|
|
prefixIcon: const Icon(Icons.lock),
|
|
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
|
|
),
|
|
),
|
|
const 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 ? const CircularProgressIndicator() : Text(l10n.register, style: const TextStyle(fontWeight: FontWeight.bold)),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: Text(l10n.alreadyHaveAccount),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|