From a7a702b5c18cdcf60449cc95b36326cca1937c6c Mon Sep 17 00:00:00 2001 From: Simon Ding Date: Sun, 14 Jul 2024 14:09:19 +0800 Subject: [PATCH] feat: add logout button --- ui/lib/main.dart | 29 ++++++++++++----------------- ui/lib/navdrawer.dart | 1 - ui/lib/providers/APIs.dart | 8 ++++++-- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/ui/lib/main.dart b/ui/lib/main.dart index 49d2c7b..4b7f32f 100644 --- a/ui/lib/main.dart +++ b/ui/lib/main.dart @@ -1,7 +1,7 @@ -import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:ui/activity.dart'; import 'package:ui/login_page.dart'; import 'package:ui/navdrawer.dart'; @@ -48,7 +48,17 @@ class MyApp extends StatelessWidget { actions: [ IconButton( onPressed: () => context.go(SystemSettingsPage.route), - icon: const Icon(Icons.settings)) + icon: const Icon(Icons.settings)), + APIs.isLoggedIn + ? IconButton( + onPressed: () async { + final SharedPreferences prefs = + await SharedPreferences.getInstance(); + await prefs.remove('token'); + context.go(LoginScreen.route); + }, + icon: const Icon(Icons.exit_to_app)) + : Container() ], ), body: Center( @@ -106,21 +116,6 @@ class MyApp extends StatelessWidget { child: MaterialApp.router( title: 'Polaris', theme: ThemeData( - // This is the theme of your application. - // - // TRY THIS: Try running your application with "flutter run". You'll see - // the application has a purple toolbar. Then, without quitting the app, - // try changing the seedColor in the colorScheme below to Colors.green - // and then invoke "hot reload" (save your changes or press the "hot - // reload" button in a Flutter-supported IDE, or press "r" if you used - // the command line to start the app). - // - // Notice that the counter didn't reset back to zero; the application - // state is not lost during the reload. To reset the state, use hot - // restart instead. - // - // This works for code too, not just values: Most code changes can be - // tested with just a hot reload. colorScheme: ColorScheme.fromSeed( seedColor: Colors.blue, brightness: Brightness.dark), useMaterial3: true, diff --git a/ui/lib/navdrawer.dart b/ui/lib/navdrawer.dart index c4f693a..5dbbfb1 100644 --- a/ui/lib/navdrawer.dart +++ b/ui/lib/navdrawer.dart @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:ui/activity.dart'; -import 'package:ui/search.dart'; import 'package:ui/system_settings.dart'; import 'package:ui/weclome.dart'; diff --git a/ui/lib/providers/APIs.dart b/ui/lib/providers/APIs.dart index 1c6fd5b..b211ee3 100644 --- a/ui/lib/providers/APIs.dart +++ b/ui/lib/providers/APIs.dart @@ -2,6 +2,7 @@ import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; +import 'package:quiver/strings.dart'; import 'package:shared_preferences/shared_preferences.dart'; class APIs { @@ -40,6 +41,7 @@ class APIs { static Dio? dio1; static Map authHeaders = {}; + static bool isLoggedIn = false; static Future getDio() async { if (dio1 != null) { @@ -47,8 +49,10 @@ class APIs { } final SharedPreferences prefs = await SharedPreferences.getInstance(); var token = prefs.getString("token"); - - authHeaders["Authorization"] = "Bearer $token"; + if (isNotBlank(token)) { + authHeaders["Authorization"] = "Bearer $token"; + isLoggedIn = true; + } var dio = Dio(); dio.interceptors.add(InterceptorsWrapper(