42 lines
1.4 KiB
Dart
42 lines
1.4 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
|
|
/// 全局用户快照;积分等用 [ValueNotifier] 驱动 UI。
|
|
class UserState {
|
|
UserState._();
|
|
|
|
static final ValueNotifier<String?> userId = ValueNotifier<String?>(null);
|
|
static final ValueNotifier<int> credits = ValueNotifier<int>(0);
|
|
static final ValueNotifier<String?> avatar = ValueNotifier<String?>(null);
|
|
static final ValueNotifier<String?> userName = ValueNotifier<String?>(null);
|
|
static final ValueNotifier<String?> countryCode = ValueNotifier<String?>(null);
|
|
|
|
static void setUserId(String? id) => userId.value = id;
|
|
static void setCredits(int v) => credits.value = v;
|
|
static void setAvatar(String? v) => avatar.value = v;
|
|
static void setUserName(String? v) => userName.value = v;
|
|
static void setCountryCode(String? v) => countryCode.value = v;
|
|
|
|
static void applyLogin({
|
|
String? userId,
|
|
int? credits,
|
|
String? avatar,
|
|
String? userName,
|
|
}) {
|
|
if (userId != null) UserState.userId.value = userId;
|
|
if (credits != null) UserState.credits.value = credits;
|
|
if (avatar != null) {
|
|
final t = avatar.trim();
|
|
UserState.avatar.value = t.isEmpty ? null : t;
|
|
}
|
|
if (userName != null) UserState.userName.value = userName;
|
|
}
|
|
|
|
static void clear() {
|
|
userId.value = null;
|
|
credits.value = 0;
|
|
avatar.value = null;
|
|
userName.value = null;
|
|
countryCode.value = null;
|
|
}
|
|
}
|