135 lines
4.7 KiB
Dart
135 lines
4.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import '../bloc/auth_bloc.dart';
|
|
import '../bloc/auth_event.dart';
|
|
import '../bloc/auth_state.dart';
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
const LoginPage({super.key});
|
|
|
|
@override
|
|
State<LoginPage> createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _emailController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
|
|
@override
|
|
void dispose() {
|
|
_emailController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Вход'),
|
|
),
|
|
body: BlocListener<AuthBloc, AuthState>(
|
|
listener: (context, state) {
|
|
state.whenOrNull(
|
|
authenticated: (userId) {
|
|
Navigator.of(context).popUntil((route) => route.isFirst);
|
|
},
|
|
error: (message) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(message)),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
child: Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
const Icon(
|
|
Icons.chat_bubble_outline,
|
|
size: 80,
|
|
color: Color(0xFF2481CC),
|
|
),
|
|
const SizedBox(height: 32),
|
|
TextFormField(
|
|
controller: _emailController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Email',
|
|
prefixIcon: Icon(Icons.email_outlined),
|
|
),
|
|
keyboardType: TextInputType.emailAddress,
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Введите email';
|
|
}
|
|
if (!value.contains('@')) {
|
|
return 'Введите корректный email';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
controller: _passwordController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Пароль',
|
|
prefixIcon: Icon(Icons.lock_outlined),
|
|
),
|
|
obscureText: true,
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Введите пароль';
|
|
}
|
|
if (value.length < 6) {
|
|
return 'Пароль должен быть не менее 6 символов';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 32),
|
|
BlocBuilder<AuthBloc, AuthState>(
|
|
builder: (context, state) {
|
|
if (state is AuthLoading) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
return ElevatedButton(
|
|
onPressed: () {
|
|
if (_formKey.currentState?.validate() ?? false) {
|
|
context.read<AuthBloc>().add(
|
|
AuthEvent.loginRequested(
|
|
_emailController.text,
|
|
_passwordController.text,
|
|
),
|
|
);
|
|
}
|
|
},
|
|
child: const Padding(
|
|
padding: EdgeInsets.symmetric(vertical: 12),
|
|
child: Text('Войти', style: TextStyle(fontSize: 16)),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextButton(
|
|
onPressed: () {
|
|
// Navigate to register page
|
|
},
|
|
child: const Text('Нет аккаунта? Зарегистрироваться'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|