import 'package:flutter/foundation.dart'; /// 全局用户快照;积分等用 [ValueNotifier] 驱动 UI。 class UserState { UserState._(); static final ValueNotifier userId = ValueNotifier(null); static final ValueNotifier credits = ValueNotifier(0); static final ValueNotifier avatar = ValueNotifier(null); static final ValueNotifier userName = ValueNotifier(null); static final ValueNotifier countryCode = ValueNotifier(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; } }