feat(App): update Finlytic Flutter app UI and blocs

This commit is contained in:
2026-08-09 21:01:46 +02:00
parent e7427b7464
commit a708d2977c
591 changed files with 1095105 additions and 0 deletions
@@ -0,0 +1,61 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:finlytic_app/core/network/api_client.dart';
import 'package:finlytic_app/core/services/secure_storage_service.dart';
import 'package:finlytic_app/features/auth/repositories/auth_repository.dart';
import 'auth_event.dart';
import 'auth_state.dart';
export 'auth_event.dart';
export 'auth_state.dart';
class AuthBloc extends Bloc<AuthEvent, AuthState> {
final AuthRepository repository;
AuthBloc({
required ApiClient apiClient,
required SecureStorageService storageService,
AuthRepository? repository,
}) : repository = repository ?? AuthRepository(apiClient: apiClient, storageService: storageService),
super(AuthInitial()) {
on<CheckAuthStatus>(_onCheckAuthStatus);
on<LoginRequested>(_onLoginRequested);
on<RegisterRequested>(_onRegisterRequested);
on<LogoutRequested>(_onLogoutRequested);
}
Future<void> _onCheckAuthStatus(CheckAuthStatus event, Emitter<AuthState> emit) async {
final user = await repository.checkAuthStatus();
if (user != null) {
emit(Authenticated(user));
} else {
emit(Unauthenticated());
}
}
Future<void> _onLoginRequested(LoginRequested event, Emitter<AuthState> emit) async {
emit(AuthLoading());
try {
final user = await repository.login(event.email, event.password);
emit(Authenticated(user));
} on RequiresPasswordChangeException catch (e) {
emit(AuthRequiresPasswordChange(e.userId));
} catch (e) {
emit(AuthFailure(e.toString()));
}
}
Future<void> _onRegisterRequested(RegisterRequested event, Emitter<AuthState> emit) async {
emit(AuthLoading());
try {
final user = await repository.register(event.email, event.password, event.fullName);
emit(Authenticated(user));
} catch (e) {
emit(AuthFailure(e.toString()));
}
}
Future<void> _onLogoutRequested(LogoutRequested event, Emitter<AuthState> emit) async {
await repository.logout();
emit(Unauthenticated());
}
}
@@ -0,0 +1,33 @@
import 'package:equatable/equatable.dart';
abstract class AuthEvent extends Equatable {
const AuthEvent();
@override
List<Object?> get props => [];
}
class CheckAuthStatus extends AuthEvent {}
class LoginRequested extends AuthEvent {
final String email;
final String password;
const LoginRequested(this.email, this.password);
@override
List<Object?> get props => [email, password];
}
class RegisterRequested extends AuthEvent {
final String email;
final String password;
final String fullName;
const RegisterRequested(this.email, this.password, this.fullName);
@override
List<Object?> get props => [email, password, fullName];
}
class LogoutRequested extends AuthEvent {}
@@ -0,0 +1,41 @@
import 'package:equatable/equatable.dart';
import 'package:finlytic_app/features/auth/models/user_model.dart';
abstract class AuthState extends Equatable {
const AuthState();
@override
List<Object?> get props => [];
}
class AuthInitial extends AuthState {}
class AuthLoading extends AuthState {}
class Authenticated extends AuthState {
final UserModel user;
const Authenticated(this.user);
@override
List<Object?> get props => [user];
}
class Unauthenticated extends AuthState {}
class AuthRequiresPasswordChange extends AuthState {
final String userId;
const AuthRequiresPasswordChange(this.userId);
@override
List<Object?> get props => [userId];
}
class AuthFailure extends AuthState {
final String error;
const AuthFailure(this.error);
@override
List<Object?> get props => [error];
}
@@ -0,0 +1,43 @@
import 'package:equatable/equatable.dart';
/// UserModel representing authenticated user session data.
class UserModel extends Equatable {
final String userId;
final String email;
final String fullName;
final String role;
final String? token;
const UserModel({
required this.userId,
required this.email,
required this.fullName,
required this.role,
this.token,
});
bool get isAdmin => role.toLowerCase() == 'admin';
factory UserModel.fromJson(Map<String, dynamic> json, {String? token}) {
return UserModel(
userId: json['userId']?.toString() ?? json['id']?.toString() ?? '',
email: json['email']?.toString() ?? '',
fullName: json['fullName']?.toString() ?? json['name']?.toString() ?? '',
role: json['role']?.toString() ?? 'User',
token: token ?? json['token']?.toString(),
);
}
Map<String, dynamic> toJson() {
return {
'userId': userId,
'email': email,
'fullName': fullName,
'role': role,
'token': token,
};
}
@override
List<Object?> get props => [userId, email, fullName, role, token];
}
@@ -0,0 +1,78 @@
import 'package:finlytic_app/core/network/api_client.dart';
import 'package:finlytic_app/core/services/secure_storage_service.dart';
import 'package:finlytic_app/features/auth/models/user_model.dart';
class AuthRepository {
final ApiClient apiClient;
final SecureStorageService storageService;
AuthRepository({
required this.apiClient,
required this.storageService,
});
Future<UserModel?> checkAuthStatus() async {
final token = await storageService.getToken();
if (token == null || token.isEmpty) {
return null;
}
try {
final response = await apiClient.get('/api/v1/user/me');
if (response.statusCode == 200 && response.data != null) {
return UserModel.fromJson(response.data, token: token);
} else {
await storageService.clearAll();
return null;
}
} catch (_) {
await storageService.clearAll();
return null;
}
}
Future<UserModel> login(String email, String password) async {
final res = await apiClient.post('/api/v1/auth/login', data: {
'email': email,
'password': password,
});
if (res.statusCode == 200 && res.data != null) {
if (res.data['requiresPasswordChange'] == true) {
throw RequiresPasswordChangeException(res.data['userId']?.toString() ?? '');
}
final token = res.data['token']?.toString() ?? '';
final user = UserModel.fromJson(res.data, token: token);
await storageService.saveToken(token);
await storageService.saveUserEmail(user.email);
return user;
} else {
throw Exception('Ungültige Anmeldedaten');
}
}
Future<UserModel> register(String email, String password, String fullName) async {
final res = await apiClient.post('/api/v1/auth/register', data: {
'email': email,
'password': password,
'fullName': fullName,
});
if (res.statusCode == 200 && res.data != null) {
final token = res.data['token']?.toString() ?? '';
final user = UserModel.fromJson(res.data, token: token);
await storageService.saveToken(token);
await storageService.saveUserEmail(user.email);
return user;
} else {
throw Exception('Registrierung fehlgeschlagen');
}
}
Future<void> logout() async {
await storageService.clearAll();
}
}
class RequiresPasswordChangeException implements Exception {
final String userId;
RequiresPasswordChangeException(this.userId);
}
@@ -0,0 +1,137 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:finlytic_app/core/network/api_client.dart';
import 'package:finlytic_app/core/theme/app_theme.dart';
import 'package:finlytic_app/features/auth/bloc/auth_bloc.dart';
class ChangeInitialPasswordScreen extends StatefulWidget {
final String userId;
const ChangeInitialPasswordScreen({super.key, required this.userId});
@override
State<ChangeInitialPasswordScreen> createState() => _ChangeInitialPasswordScreenState();
}
class _ChangeInitialPasswordScreenState extends State<ChangeInitialPasswordScreen> {
final _formKey = GlobalKey<FormState>();
final _passwordController = TextEditingController();
final _confirmPasswordController = TextEditingController();
bool _isLoading = false;
void _onChangePassword() async {
if (_formKey.currentState?.validate() ?? false) {
setState(() => _isLoading = true);
try {
final apiClient = context.read<ApiClient>();
final res = await apiClient.post('/api/v1/auth/change-initial-password', data: {
'userId': widget.userId,
'newPassword': _passwordController.text,
});
if (res.statusCode == 200) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Passwort erfolgreich geändert! Bitte melden Sie sich erneut an.'), backgroundColor: AppTheme.accentCyan),
);
context.read<AuthBloc>().add(LogoutRequested()); // Force re-login
}
} else {
throw Exception('Fehler beim Ändern des Passworts.');
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(e.toString()), backgroundColor: Colors.red),
);
}
} finally {
if (mounted) {
setState(() => _isLoading = false);
}
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Container(
constraints: const BoxConstraints(maxWidth: 400),
padding: const EdgeInsets.all(28),
decoration: BoxDecoration(
color: AppTheme.cardSurface,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: AppTheme.glassBorder),
boxShadow: [
BoxShadow(
color: AppTheme.accentCyan.withValues(alpha: 0.08),
blurRadius: 24,
spreadRadius: 2,
),
],
),
child: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.lock_reset, size: 54, color: AppTheme.accentCyan),
const SizedBox(height: 12),
const Text(
'PASSWORT ÄNDERN',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, letterSpacing: 1),
),
const SizedBox(height: 8),
Text(
'Ein Administrator hat Ihr Passwort zurückgesetzt oder diesen Account neu erstellt. Bitte vergeben Sie ein neues Passwort.',
textAlign: TextAlign.center,
style: TextStyle(color: AppTheme.textSecondary, fontSize: 13),
),
const SizedBox(height: 24),
TextFormField(
controller: _passwordController,
obscureText: true,
decoration: const InputDecoration(labelText: 'Neues Passwort', prefixIcon: Icon(Icons.lock)),
validator: (v) => v == null || v.length < 6 ? 'Passwort muss mindestens 6 Zeichen lang sein' : null,
),
const SizedBox(height: 16),
TextFormField(
controller: _confirmPasswordController,
obscureText: true,
decoration: const InputDecoration(labelText: 'Passwort bestätigen', prefixIcon: Icon(Icons.lock_outline)),
validator: (v) => v != _passwordController.text ? 'Passwörter stimmen nicht überein' : null,
),
const SizedBox(height: 24),
_isLoading
? CircularProgressIndicator(color: AppTheme.accentCyan)
: SizedBox(
width: double.infinity,
height: 48,
child: ElevatedButton(
onPressed: _onChangePassword,
style: ElevatedButton.styleFrom(
backgroundColor: AppTheme.accentCyan,
foregroundColor: Colors.black,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
),
child: const Text('Passwort Speichern', style: TextStyle(fontWeight: FontWeight.bold)),
),
),
const SizedBox(height: 16),
TextButton(
onPressed: () => context.read<AuthBloc>().add(LogoutRequested()),
child: Text('Abbrechen & Zurück zum Login', style: TextStyle(color: AppTheme.textSecondary)),
),
],
),
),
),
),
),
);
}
}
@@ -0,0 +1,104 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../core/theme/app_theme.dart';
import '../bloc/auth_bloc.dart';
/// User Login Screen widget.
class LoginScreen extends StatefulWidget {
const LoginScreen({super.key});
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _formKey = GlobalKey<FormState>();
final _emailController = TextEditingController(text: 'admin@finlytic.com');
final _passwordController = TextEditingController(text: 'AdminDefaultPassword2026!');
void _onLogin() {
if (_formKey.currentState?.validate() ?? false) {
context.read<AuthBloc>().add(LoginRequested(
_emailController.text.trim(),
_passwordController.text.trim(),
));
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Container(
constraints: const BoxConstraints(maxWidth: 400),
padding: const EdgeInsets.all(28),
decoration: BoxDecoration(
color: AppTheme.cardSurface,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: AppTheme.glassBorder),
boxShadow: [
BoxShadow(
color: AppTheme.primaryEmerald.withValues(alpha: 0.08),
blurRadius: 24,
spreadRadius: 2,
),
],
),
child: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.auto_graph_rounded, size: 54, color: AppTheme.primaryEmerald),
const SizedBox(height: 12),
const Text(
'FINLYTIC',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, letterSpacing: 2),
),
Text('Enterprise Financial Intelligence', style: TextStyle(color: AppTheme.textMuted, fontSize: 12)),
const SizedBox(height: 28),
TextFormField(
controller: _emailController,
decoration: const InputDecoration(labelText: 'E-Mail', prefixIcon: Icon(Icons.email_outlined)),
validator: (v) => v == null || !v.contains('@') ? 'Gültige E-Mail erforderlich' : null,
),
const SizedBox(height: 16),
TextFormField(
controller: _passwordController,
obscureText: true,
decoration: const InputDecoration(labelText: 'Passwort', prefixIcon: Icon(Icons.lock_outline)),
validator: (v) => v == null || v.isEmpty ? 'Passwort erforderlich' : null,
),
const SizedBox(height: 32),
BlocBuilder<AuthBloc, AuthState>(
builder: (context, state) {
if (state is AuthLoading) {
return CircularProgressIndicator(color: AppTheme.primaryEmerald);
}
return SizedBox(
width: double.infinity,
height: 48,
child: ElevatedButton(
onPressed: _onLogin,
style: ElevatedButton.styleFrom(
backgroundColor: AppTheme.primaryEmerald,
foregroundColor: Colors.black,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
),
child: const Text('Anmelden', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15)),
),
);
},
),
],
),
),
),
),
),
);
}
}
@@ -0,0 +1,96 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../core/theme/app_theme.dart';
import '../bloc/auth_bloc.dart';
/// User registration screen widget.
class RegisterScreen extends StatefulWidget {
const RegisterScreen({super.key});
@override
State<RegisterScreen> createState() => _RegisterScreenState();
}
class _RegisterScreenState extends State<RegisterScreen> {
final _formKey = GlobalKey<FormState>();
final _nameController = TextEditingController();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
void _submit() {
if (_formKey.currentState?.validate() ?? false) {
context.read<AuthBloc>().add(RegisterRequested(
_emailController.text.trim(),
_passwordController.text.trim(),
_nameController.text.trim(),
));
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Konto Registrieren')),
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Container(
constraints: const BoxConstraints(maxWidth: 420),
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: AppTheme.cardSurface,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: AppTheme.glassBorder),
),
child: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.person_add_outlined, size: 48, color: AppTheme.primaryEmerald),
const SizedBox(height: 16),
const Text(
'Neues Konto erstellen',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 24),
TextFormField(
controller: _nameController,
decoration: const InputDecoration(labelText: 'Vollständiger Name', prefixIcon: Icon(Icons.person_outline)),
validator: (v) => v == null || v.isEmpty ? 'Name erforderlich' : null,
),
const SizedBox(height: 14),
TextFormField(
controller: _emailController,
decoration: const InputDecoration(labelText: 'E-Mail', prefixIcon: Icon(Icons.email_outlined)),
validator: (v) => v == null || !v.contains('@') ? 'Gültige E-Mail erforderlich' : null,
),
const SizedBox(height: 14),
TextFormField(
controller: _passwordController,
obscureText: true,
decoration: const InputDecoration(labelText: 'Passwort', prefixIcon: Icon(Icons.lock_outline)),
validator: (v) => v == null || v.length < 6 ? 'Mind. 6 Zeichen' : null,
),
const SizedBox(height: 24),
SizedBox(
width: double.infinity,
height: 48,
child: ElevatedButton(
onPressed: _submit,
style: ElevatedButton.styleFrom(
backgroundColor: AppTheme.primaryEmerald,
foregroundColor: Colors.black,
),
child: const Text('Registrieren', style: TextStyle(fontWeight: FontWeight.bold)),
),
),
],
),
),
),
),
),
);
}
}
@@ -0,0 +1,52 @@
import 'package:flutter/material.dart';
/// Password reset request dialog widget.
class ForgotPasswordDialog extends StatefulWidget {
const ForgotPasswordDialog({super.key});
@override
State<ForgotPasswordDialog> createState() => _ForgotPasswordDialogState();
}
class _ForgotPasswordDialogState extends State<ForgotPasswordDialog> {
final _emailController = TextEditingController();
bool _sent = false;
@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Passwort zurücksetzen'),
content: _sent
? const Text('Ein Link zum Zurücksetzen des Passworts wurde an Ihre E-Mail gesendet.')
: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('Geben Sie Ihre E-Mail-Adresse ein, um einen Zurücksetzungslink zu erhalten:'),
const SizedBox(height: 16),
TextField(
controller: _emailController,
decoration: const InputDecoration(
labelText: 'E-Mail-Adresse',
prefixIcon: Icon(Icons.email_outlined),
),
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(_sent ? 'Schließen' : 'Abbrechen'),
),
if (!_sent)
ElevatedButton(
onPressed: () {
if (_emailController.text.contains('@')) {
setState(() => _sent = true);
}
},
child: const Text('Link Anfordern'),
),
],
);
}
}