Skip to content

Commit

Permalink
Merge pull request #22 from NewtonMutugi/main
Browse files Browse the repository at this point in the history
feat: added logout functionality
  • Loading branch information
IamMuuo authored Oct 10, 2023
2 parents 5e2295c + d00b51a commit 0be6e8f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lib/models/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,19 @@ class User {
debugPrint("Error: ${e.toString()}");
}
}

// Logout a user
Future<void> logout() async {
try {
// Close the Hive box
await appDB.close();
// Delete the Hive box directory to remove all data
await Hive.deleteBoxFromDisk(dbName);
// Clear the user instance
// ignore: cast_from_null_always_fails
user = null as User;
} catch (e) {
debugPrint("Error during logout: ${e.toString()}");
}
}
}
13 changes: 11 additions & 2 deletions lib/pages/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../constants/common.dart';
import 'intro_page.dart';

class SettingsPage extends StatelessWidget {
const SettingsPage({super.key});

Expand Down Expand Up @@ -175,7 +178,13 @@ class SettingsPage extends StatelessWidget {
Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
child: ElevatedButton(
onPressed: () {},
// log out the use
onPressed: () {
user.logout();
Get.offAll(const IntroPage());
Get.snackbar(
"Logout status", "You have successfully logged out!");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
elevation: 0,
Expand All @@ -187,7 +196,7 @@ class SettingsPage extends StatelessWidget {
),
),
child: const Text(
'Clear App Data',
'Log out',
style: TextStyle(
fontWeight: FontWeight.w500,
),
Expand Down

0 comments on commit 0be6e8f

Please sign in to comment.