OscarGil03 před 2 měsíci
rodič
revize
bc279f38eb

+ 31 - 0
lib/mvvm/viewmodels/login_view_model.dart

@@ -1,4 +1,5 @@
 import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
 import '../../core/services/session_storage.dart';
 import '../../core/models/models.dart';
 import '../../core/services/services.dart';
@@ -93,4 +94,34 @@ class LoginViewModel extends ChangeNotifier {
     _obscureText = !_obscureText;
     notifyListeners();
   }
+
+  Future<void> showExitConfirmationDialog(context) async {
+    final bool? confirm = await showDialog(
+      context: context,
+      builder: (ctx) {
+        return AlertDialog(
+          title: const Text('¿Cerrar sesión?'),
+          content: const Text('¿Estás seguro de que quieres cerrar la sesión?'),
+          actions: [
+            TextButton(
+              onPressed: () => Navigator.of(ctx).pop(false),
+              child:
+                  const Text('Cancelar', style: TextStyle(color: Colors.red)),
+            ),
+            TextButton(
+              onPressed: () async {
+                Provider.of<LoginViewModel>(context, listen: false).logOut();
+                Navigator.of(ctx).pop(true);
+              },
+              child: const Text('Aceptar'),
+            ),
+          ],
+        );
+      },
+    );
+
+    if (confirm == true) {
+      Navigator.of(context).pushNamedAndRemoveUntil('login', (route) => false);
+    }
+  }
 }

+ 10 - 0
lib/mvvm/views/home/home_screen.dart

@@ -30,6 +30,7 @@ class Formulario extends State<HomeScreen> {
   @override
   Widget build(BuildContext context) {
     final mesaViewModel = Provider.of<MesaViewModel>(context);
+    final loginViewModel = Provider.of<LoginViewModel>(context);
     var _selectedIndex;
     return Scaffold(
       backgroundColor: Colors.grey.shade200,
@@ -45,6 +46,10 @@ class Formulario extends State<HomeScreen> {
               setState(() {
                 _selectedIndex = index;
               });
+
+              if (index == 3) {
+                loginViewModel.showExitConfirmationDialog(context);
+              }
             },
             labelType: NavigationRailLabelType.all,
             destinations: const [
@@ -66,6 +71,11 @@ class Formulario extends State<HomeScreen> {
                 selectedIcon: Icon(Icons.settings_rounded, color: Colors.white),
                 label: Text('Ajustes'),
               ),
+              NavigationRailDestination(
+                icon: Icon(Icons.logout),
+                selectedIcon: Icon(Icons.logout, color: Colors.white),
+                label: Text('Cerrar Sesión'),
+              ),
             ],
           ),
           Expanded(