mirror of
https://github.com/simon-ding/polaris.git
synced 2026-03-16 07:20:52 +08:00
feat: change logout button
This commit is contained in:
@@ -50,18 +50,41 @@ class MyApp extends StatelessWidget {
|
||||
IconButton(
|
||||
onPressed: () => context.go(SystemSettingsPage.route),
|
||||
icon: const Icon(Icons.settings)),
|
||||
APIs.isLoggedIn
|
||||
? IconButton(
|
||||
onPressed: () async {
|
||||
final SharedPreferences prefs =
|
||||
await SharedPreferences.getInstance();
|
||||
await prefs.remove('token');
|
||||
if (context.mounted) {
|
||||
context.go(LoginScreen.route);
|
||||
}
|
||||
},
|
||||
icon: const Icon(Icons.exit_to_app))
|
||||
: Container()
|
||||
FutureBuilder(
|
||||
future: APIs.isLoggedIn(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData && snapshot.data == true) {
|
||||
return MenuAnchor(
|
||||
menuChildren: [
|
||||
MenuItemButton(
|
||||
leadingIcon: const Icon(Icons.exit_to_app),
|
||||
child: const Text("登出"),
|
||||
onPressed: () async {
|
||||
final SharedPreferences prefs =
|
||||
await SharedPreferences.getInstance();
|
||||
await prefs.remove('token');
|
||||
if (context.mounted) {
|
||||
context.go(LoginScreen.route);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
builder: (context, controller, child) {
|
||||
return TextButton(
|
||||
onPressed: () {
|
||||
if (controller.isOpen) {
|
||||
controller.close();
|
||||
} else {
|
||||
controller.open();
|
||||
}
|
||||
},
|
||||
child: const Icon(Icons.account_circle),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
return Container();
|
||||
})
|
||||
],
|
||||
),
|
||||
body: Center(
|
||||
@@ -73,7 +96,11 @@ class MyApp extends StatelessWidget {
|
||||
child: NavDrawer(),
|
||||
),
|
||||
const VerticalDivider(thickness: 1, width: 1),
|
||||
Flexible(flex: 7, child: Padding(padding: const EdgeInsets.all(20),child: child), )
|
||||
Flexible(
|
||||
flex: 7,
|
||||
child:
|
||||
Padding(padding: const EdgeInsets.all(20), child: child),
|
||||
)
|
||||
]))),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -44,22 +44,29 @@ class APIs {
|
||||
|
||||
static Dio? gDio;
|
||||
static Map<String, String> authHeaders = {};
|
||||
static bool isLoggedIn = false;
|
||||
|
||||
static Future<Dio> getDio() async {
|
||||
if (gDio != null) {
|
||||
return gDio!;
|
||||
}
|
||||
static Future<bool> isLoggedIn() async {
|
||||
return isNotBlank(await getToken());
|
||||
}
|
||||
|
||||
static Future<String> getToken() async {
|
||||
var token = authHeaders["Authorization"];
|
||||
if (isBlank(token)) {
|
||||
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var t = prefs.getString("token");
|
||||
if (isNotBlank(t)) {
|
||||
authHeaders["Authorization"] = t!;
|
||||
isLoggedIn = true;
|
||||
token = t;
|
||||
}
|
||||
}
|
||||
return token ?? "";
|
||||
}
|
||||
|
||||
static Future<Dio> getDio() async {
|
||||
if (gDio != null) {
|
||||
return gDio!;
|
||||
}
|
||||
var token = await getToken();
|
||||
|
||||
var dio = Dio();
|
||||
dio.interceptors.add(InterceptorsWrapper(
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:ui/providers/APIs.dart';
|
||||
import 'package:ui/providers/server_response.dart';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user