mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-09 11:39:46 +08:00
feat: add logout button
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import 'package:flutter/gestures.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:ui/activity.dart';
|
import 'package:ui/activity.dart';
|
||||||
import 'package:ui/login_page.dart';
|
import 'package:ui/login_page.dart';
|
||||||
import 'package:ui/navdrawer.dart';
|
import 'package:ui/navdrawer.dart';
|
||||||
@@ -48,7 +48,17 @@ class MyApp extends StatelessWidget {
|
|||||||
actions: [
|
actions: [
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () => context.go(SystemSettingsPage.route),
|
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(
|
body: Center(
|
||||||
@@ -106,21 +116,6 @@ class MyApp extends StatelessWidget {
|
|||||||
child: MaterialApp.router(
|
child: MaterialApp.router(
|
||||||
title: 'Polaris',
|
title: 'Polaris',
|
||||||
theme: ThemeData(
|
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(
|
colorScheme: ColorScheme.fromSeed(
|
||||||
seedColor: Colors.blue, brightness: Brightness.dark),
|
seedColor: Colors.blue, brightness: Brightness.dark),
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:ui/activity.dart';
|
import 'package:ui/activity.dart';
|
||||||
import 'package:ui/search.dart';
|
|
||||||
import 'package:ui/system_settings.dart';
|
import 'package:ui/system_settings.dart';
|
||||||
import 'package:ui/weclome.dart';
|
import 'package:ui/weclome.dart';
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'package:dio/dio.dart';
|
|||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
import 'package:quiver/strings.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
class APIs {
|
class APIs {
|
||||||
@@ -40,6 +41,7 @@ class APIs {
|
|||||||
|
|
||||||
static Dio? dio1;
|
static Dio? dio1;
|
||||||
static Map<String, String> authHeaders = {};
|
static Map<String, String> authHeaders = {};
|
||||||
|
static bool isLoggedIn = false;
|
||||||
|
|
||||||
static Future<Dio> getDio() async {
|
static Future<Dio> getDio() async {
|
||||||
if (dio1 != null) {
|
if (dio1 != null) {
|
||||||
@@ -47,8 +49,10 @@ class APIs {
|
|||||||
}
|
}
|
||||||
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
var token = prefs.getString("token");
|
var token = prefs.getString("token");
|
||||||
|
if (isNotBlank(token)) {
|
||||||
authHeaders["Authorization"] = "Bearer $token";
|
authHeaders["Authorization"] = "Bearer $token";
|
||||||
|
isLoggedIn = true;
|
||||||
|
}
|
||||||
|
|
||||||
var dio = Dio();
|
var dio = Dio();
|
||||||
dio.interceptors.add(InterceptorsWrapper(
|
dio.interceptors.add(InterceptorsWrapper(
|
||||||
|
|||||||
Reference in New Issue
Block a user