From c07c4e97b9fb858f2cba92ed622312a7b69bb1a2 Mon Sep 17 00:00:00 2001 From: Mochamad ongki ramadani Date: Tue, 17 Feb 2026 15:48:32 +0700 Subject: [PATCH] iki ada ucapan selamat datang --- .../authentication_bloc.dart | 7 ++ .../authentication_event.dart | 4 + lib/screens/home/views/home_screen.dart | 81 +++++++++++++------ 3 files changed, 69 insertions(+), 23 deletions(-) diff --git a/lib/blocs/authentication_bloc/authentication_bloc.dart b/lib/blocs/authentication_bloc/authentication_bloc.dart index 74d02c6..7929c1e 100644 --- a/lib/blocs/authentication_bloc/authentication_bloc.dart +++ b/lib/blocs/authentication_bloc/authentication_bloc.dart @@ -26,6 +26,13 @@ class AuthenticationBloc extends Bloc emit(AuthenticationState.unauthenticated()); } }); + on((event, emit) async { + try { + await userRepository.logOut(); + } catch (_) { + // ignore errors here; stream will update state accordingly + } + }); } @override Future close() { diff --git a/lib/blocs/authentication_bloc/authentication_event.dart b/lib/blocs/authentication_bloc/authentication_event.dart index 1b96112..6972fc6 100644 --- a/lib/blocs/authentication_bloc/authentication_event.dart +++ b/lib/blocs/authentication_bloc/authentication_event.dart @@ -11,4 +11,8 @@ class AuthenticationUserChanged extends AuthenticationEvent { final MyUser? user; const AuthenticationUserChanged(this.user); +} + +class AuthenticationLogoutRequested extends AuthenticationEvent { + const AuthenticationLogoutRequested(); } \ No newline at end of file diff --git a/lib/screens/home/views/home_screen.dart b/lib/screens/home/views/home_screen.dart index 8a9f75e..b7a85e1 100644 --- a/lib/screens/home/views/home_screen.dart +++ b/lib/screens/home/views/home_screen.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; import 'package:fl_chart/fl_chart.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import '../../../blocs/authentication_bloc/authentication_bloc.dart'; import '../../monitoring/evaporasi_monitoring_screen.dart'; import '../../monitoring/wind_speed_monitoring_screen.dart'; import '../../monitoring/air_quality_monitoring_screen.dart'; @@ -21,24 +23,39 @@ class HomeScreen extends StatelessWidget { child: ListView( padding: EdgeInsets.zero, children: [ - // 🔹 HEADER DENGAN AKUN INFO - UserAccountsDrawerHeader( - accountName: const Text( - "Admin Klimatologi", - style: TextStyle(fontWeight: FontWeight.bold), - ), - accountEmail: const Text("admin@klimatologi.com"), - currentAccountPicture: CircleAvatar( - backgroundColor: Colors.blue, - child: const Text( - "A", - style: TextStyle( - fontSize: 24, - fontWeight: FontWeight.bold, - color: Colors.white, + // 🔹 HEADER DENGAN AKUN INFO (dynamic dari AuthenticationBloc) + BlocBuilder( + builder: (context, state) { + final user = state.user; + final displayName = (user != null && user.name.isNotEmpty) + ? user.name + : 'Pengguna'; + final email = (user != null && user.email.isNotEmpty) + ? user.email + : 'Tidak ada email'; + final initials = (displayName.isNotEmpty) + ? displayName.trim().split(' ').map((e) => e.isNotEmpty ? e[0] : '').take(2).join() + : 'U'; + + return UserAccountsDrawerHeader( + accountName: Text( + displayName, + style: const TextStyle(fontWeight: FontWeight.bold), ), - ), - ), + accountEmail: Text(email), + currentAccountPicture: CircleAvatar( + backgroundColor: Colors.blue, + child: Text( + initials.toUpperCase(), + style: const TextStyle( + fontSize: 24, + fontWeight: FontWeight.bold, + color: Colors.white, + ), + ), + ), + ); + }, ), // 🔹 DASHBOARD MENU @@ -80,6 +97,17 @@ class HomeScreen extends StatelessWidget { "Kualitas Udara", Icons.cloud, ), + const Divider(), + ListTile( + leading: const Icon(Icons.logout), + title: const Text('Logout'), + onTap: () async { + Navigator.pop(context); + // trigger logout via bloc + final bloc = context.read(); + bloc.add(const AuthenticationLogoutRequested()); + }, + ), ], ), ), @@ -92,12 +120,19 @@ class HomeScreen extends StatelessWidget { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - const Text( - "Selamat Datang Admin Klimatologi", - style: TextStyle( - fontSize: 22, - fontWeight: FontWeight.bold, - ), + BlocBuilder( + builder: (context, state) { + final name = state.user?.name.isNotEmpty == true + ? state.user!.name + : 'Pengguna'; + return Text( + 'Selamat Datang $name', + style: const TextStyle( + fontSize: 22, + fontWeight: FontWeight.bold, + ), + ); + }, ), const SizedBox(height: 20),