Files
voidea/flutter/lib/features/auth/screens/register_screen.dart
T

146 lines
5.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:flutter_animate/flutter_animate.dart';
import '../../../providers/auth_provider.dart';
import '../../../core/utils/validators.dart';
class RegisterScreen extends ConsumerStatefulWidget {
const RegisterScreen({super.key});
@override
ConsumerState<RegisterScreen> createState() => _RegisterScreenState();
}
class _RegisterScreenState extends ConsumerState<RegisterScreen> {
final _formKey = GlobalKey<FormState>();
final _nameController = TextEditingController();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
final _confirmController = TextEditingController();
bool _obscurePassword = true;
@override
void dispose() {
_nameController.dispose();
_emailController.dispose();
_passwordController.dispose();
_confirmController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final authState = ref.watch(authProvider);
final authNotifier = ref.read(authProvider.notifier);
ref.listen(authProvider, (_, state) {
if (state.status == AuthStatus.authenticated) {
context.go('/');
}
});
return Scaffold(
appBar: AppBar(title: const Text('Регистрация')),
body: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(height: 24),
TextFormField(
controller: _nameController,
decoration: const InputDecoration(
labelText: 'Имя',
prefixIcon: Icon(Icons.person_outline),
),
validator: (v) => Validators.required(v, 'Имя'),
).animate().fadeIn().slideX(),
const SizedBox(height: 16),
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
labelText: 'Email',
prefixIcon: Icon(Icons.email_outlined),
),
validator: Validators.email,
).animate().fadeIn(delay: 100.ms).slideX(),
const SizedBox(height: 16),
TextFormField(
controller: _passwordController,
obscureText: _obscurePassword,
decoration: InputDecoration(
labelText: 'Пароль',
prefixIcon: const Icon(Icons.lock_outlined),
suffixIcon: IconButton(
icon: Icon(_obscurePassword
? Icons.visibility_off
: Icons.visibility),
onPressed: () =>
setState(() => _obscurePassword = !_obscurePassword),
),
),
validator: Validators.password,
).animate().fadeIn(delay: 200.ms).slideX(),
const SizedBox(height: 16),
TextFormField(
controller: _confirmController,
obscureText: true,
decoration: const InputDecoration(
labelText: 'Подтвердите пароль',
prefixIcon: Icon(Icons.lock_outlined),
),
validator: (v) =>
Validators.confirmPassword(v, _passwordController.text),
).animate().fadeIn(delay: 300.ms).slideX(),
const SizedBox(height: 32),
if (authState.error != null)
Padding(
padding: const EdgeInsets.only(bottom: 16),
child: Text(
authState.error!,
style: const TextStyle(color: Color(0xFFEF4444)),
textAlign: TextAlign.center,
),
),
ElevatedButton(
onPressed: authState.status == AuthStatus.loading
? null
: () {
if (_formKey.currentState!.validate()) {
authNotifier.register(
_emailController.text.trim(),
_passwordController.text,
_nameController.text.trim(),
);
}
},
child: authState.status == AuthStatus.loading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
)
: const Text('Зарегистрироваться'),
).animate().fadeIn(delay: 400.ms),
const SizedBox(height: 16),
TextButton(
onPressed: () => context.pop(),
child: const Text('Уже есть аккаунт? Войти'),
),
],
),
),
),
),
);
}
}