第二次提交
This commit is contained in:
@@ -1,17 +1,16 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../domain/entities/user_entity.dart';
|
||||
import 'app_state.dart';
|
||||
import 'app_user_state.dart';
|
||||
|
||||
class AppCubit extends Cubit<AppState> {
|
||||
AppCubit() : super(const AppState());
|
||||
class AppUserCubit extends Cubit<AppUserState> {
|
||||
AppUserCubit() : super(const AppUserState());
|
||||
|
||||
// 传入实体对象,而不是零散的 id
|
||||
void setAuth(UserEntity user) {
|
||||
emit(state.copyWith(user));
|
||||
}
|
||||
|
||||
void clearAuth() {
|
||||
emit(const AppState());
|
||||
emit(const AppUserState());
|
||||
}
|
||||
}
|
||||
@@ -2,16 +2,16 @@ import 'package:equatable/equatable.dart';
|
||||
|
||||
import '../domain/entities/user_entity.dart';
|
||||
|
||||
class AppState extends Equatable {
|
||||
class AppUserState extends Equatable {
|
||||
final UserEntity? user; // 直接存实体,包含 userId, username,token 等所有信息
|
||||
|
||||
const AppState({this.user});
|
||||
const AppUserState({this.user});
|
||||
|
||||
// 辅助属性:判断是否登录
|
||||
bool get isLoggedIn => user != null;
|
||||
|
||||
AppState copyWith(UserEntity? user) {
|
||||
return AppState(user: user ?? this.user);
|
||||
AppUserState copyWith(UserEntity? user) {
|
||||
return AppUserState(user: user ?? this.user);
|
||||
}
|
||||
|
||||
// 必须重写 props,UI 才会只在数据真正变化时刷新
|
||||
Reference in New Issue
Block a user