account_cubit.dart
2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import 'package:appframe/config/locator.dart';
import 'package:appframe/config/routes.dart';
import 'package:appframe/data/repositories/phone_auth_repository.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';
class AccountState extends Equatable {
final bool loaded;
final String name;
final String phone;
final String nickname;
final String imgIcon;
const AccountState({
this.loaded = false,
this.name = '',
this.phone = '',
this.nickname = '',
this.imgIcon = '',
});
AccountState copyWith({
bool? loaded,
String? name,
String? phone,
String? nickname,
String? imgIcon,
}) {
return AccountState(
loaded: loaded ?? this.loaded,
name: name ?? this.name,
phone: phone ?? this.phone,
nickname: nickname ?? this.nickname,
imgIcon: imgIcon ?? this.imgIcon,
);
}
@override
List<Object?> get props => [
loaded,
name,
phone,
nickname,
imgIcon,
];
}
class AccountCubit extends Cubit<AccountState> {
late final PhoneAuthRepository _phoneAuthRepository;
AccountCubit(super.initialState) {
_phoneAuthRepository = getIt.get<PhoneAuthRepository>();
init();
}
Future<void> init() async {
var sharedPreferences = getIt.get<SharedPreferences>();
var userCode = sharedPreferences.getString('auth_userCode') ?? '';
try {
var result = await _phoneAuthRepository.bindCheck(userCode);
var code = result['code'];
var data = result['data'];
if (code != 0) {
return;
}
emit(
state.copyWith(
loaded: true,
name: data['name'],
phone: data['phone'],
nickname: data['nickname'],
imgIcon: data['imgIcon'],
),
);
} catch (e) {
print(e);
}
}
Future<void> goBind() async {
String? result = await router.push(
'/account/phone',
extra: {
'phone': state.phone,
},
);
if (result != null && result.isNotEmpty) {
emit(state.copyWith(phone: result));
}
}
}