feat: add logout button

This commit is contained in:
Simon Ding
2024-07-14 14:09:19 +08:00
parent 050441b187
commit a7a702b5c1
3 changed files with 18 additions and 20 deletions

View File

@@ -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,

View File

@@ -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';

View File

@@ -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<String, String> authHeaders = {};
static bool isLoggedIn = false;
static Future<Dio> 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(