This commit implements a complete authentication flow, including user registration. - Introduces an `AuthRegisterRequested` event to handle user registration. - Persists the user ID in settings upon successful authentication. - Modifies `AuthBloc` to load the user based on the stored ID, improving app launch persistence. - Refactors `UserCubit` to only manage the user state and removes authentication logic. - Removes `UserCubit` initialization from `main.dart` and triggers `AuthStarted` to initiate the authentication process.
100 lines
3.0 KiB
Dart
100 lines
3.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import '/l10n/app_localizations.dart';
|
|
import '../../logic/auth/auth_bloc.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 _nameController = TextEditingController();
|
|
|
|
void _login() {
|
|
if (_formKey.currentState!.validate()) {
|
|
context.read<AuthBloc>().add(AuthRegisterRequested(
|
|
name: _nameController.text,
|
|
email: _emailController.text,
|
|
));
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final localizations = AppLocalizations.of(context)!;
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(localizations.loginPageTitle),
|
|
),
|
|
body: BlocListener<AuthBloc, AuthState>(
|
|
listener: (context, state) {
|
|
if (state is AuthError) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(state.message)),
|
|
);
|
|
}
|
|
},
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
children: [
|
|
TextFormField(
|
|
controller: _nameController,
|
|
decoration: InputDecoration(
|
|
labelText: localizations.nameFieldLabel,
|
|
),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return localizations.nameFieldEmptyError;
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
TextFormField(
|
|
controller: _emailController,
|
|
decoration: InputDecoration(
|
|
labelText: localizations.emailFieldLabel,
|
|
),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return localizations.emailFieldEmptyError;
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 20),
|
|
BlocBuilder<AuthBloc, AuthState>(
|
|
builder: (context, state) {
|
|
final isLoading = state is AuthLoading;
|
|
|
|
return ElevatedButton(
|
|
onPressed: isLoading ? null : _login,
|
|
child: isLoading
|
|
? const SizedBox(
|
|
width: 20,
|
|
height: 20,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: Text(localizations.loginButtonText),
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|