Commit fcc3a982c1465ab7d5cb1d1133fd0d2799f22d9e
1 parent
e8d9f472
feat:全局缓存bloc
Showing
6 changed files
with
54 additions
and
1 deletions
lib/app/app.dart
| ... | ... | @@ -4,6 +4,7 @@ import 'package:flutter_easyloading/flutter_easyloading.dart'; |
| 4 | 4 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
| 5 | 5 | import 'package:responsive_framework/breakpoint.dart'; |
| 6 | 6 | import 'package:responsive_framework/responsive_breakpoints.dart'; |
| 7 | +import 'package:wow_english/common/blocs/cachebloc/cache_bloc.dart'; | |
| 7 | 8 | import 'package:wow_english/common/widgets/hide_keyboard_widget.dart'; |
| 8 | 9 | import 'package:wow_english/pages/tab/blocs/tab_bloc.dart'; |
| 9 | 10 | import 'package:wow_english/route/route.dart'; |
| ... | ... | @@ -17,7 +18,8 @@ class App extends StatelessWidget { |
| 17 | 18 | designSize: const Size(667, 375), |
| 18 | 19 | builder: (_,__) => MultiBlocProvider( |
| 19 | 20 | providers: [ |
| 20 | - BlocProvider<TabBloc>(create: (_)=> TabBloc()) | |
| 21 | + BlocProvider<TabBloc>(create: (_)=> TabBloc()), | |
| 22 | + BlocProvider<CacheBloc>(create: (_) => CacheBloc()) | |
| 21 | 23 | ], |
| 22 | 24 | child: HideKeyboard( |
| 23 | 25 | child: MaterialApp( | ... | ... |
lib/common/blocs/cachebloc/cache_bloc.dart
0 → 100644
| 1 | +import 'package:flutter/cupertino.dart'; | |
| 2 | +import 'package:flutter_bloc/flutter_bloc.dart'; | |
| 3 | +import 'package:wow_english/models/user_entity.dart'; | |
| 4 | + | |
| 5 | +part 'cache_event.dart'; | |
| 6 | +part 'cache_state.dart'; | |
| 7 | + | |
| 8 | +class CacheBloc extends Bloc<CacheEvent, CacheState> { | |
| 9 | + UserEntity? _userEntity; | |
| 10 | + | |
| 11 | + UserEntity? get userEntity => _userEntity; | |
| 12 | + | |
| 13 | + CacheBloc() : super(CacheInitial()) { | |
| 14 | + on<UserInfoChangeEvent>(_userInfoChange); | |
| 15 | + } | |
| 16 | + | |
| 17 | + void _userInfoChange(UserInfoChangeEvent event,Emitter<CacheState> emitter) async { | |
| 18 | + _userEntity = event.userEntity; | |
| 19 | + emitter(UserInfoChangeState()); | |
| 20 | + } | |
| 21 | + | |
| 22 | + void _userInfoClear(UserInfoClearEvent event,Emitter<CacheState> emitter) async { | |
| 23 | + _userEntity = null; | |
| 24 | + emitter(UserInfoClearState()); | |
| 25 | + } | |
| 26 | +} | ... | ... |
lib/common/blocs/cachebloc/cache_event.dart
0 → 100644
lib/common/blocs/cachebloc/cache_state.dart
0 → 100644
lib/pages/login/loginpage/bloc/login_bloc.dart
lib/pages/login/loginpage/login_page.dart
| ... | ... | @@ -2,6 +2,7 @@ import 'package:flutter/gestures.dart'; |
| 2 | 2 | import 'package:flutter/material.dart'; |
| 3 | 3 | import 'package:flutter_bloc/flutter_bloc.dart'; |
| 4 | 4 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
| 5 | +import 'package:wow_english/common/blocs/cachebloc/cache_bloc.dart'; | |
| 5 | 6 | import 'package:wow_english/common/extension/string_extension.dart'; |
| 6 | 7 | import 'package:wow_english/common/widgets/textfield_customer_widget.dart'; |
| 7 | 8 | import 'package:wow_english/pages/login/loginpage/time_widget.dart'; |
| ... | ... | @@ -29,6 +30,7 @@ class _LoginPageView extends StatelessWidget { |
| 29 | 30 | if (state is LoginResultChangeState) { |
| 30 | 31 | Navigator.of(context).pushNamed(AppRouteName.home); |
| 31 | 32 | } |
| 33 | + context.read<CacheBloc>().add(UserInfoChangeEvent(null)); | |
| 32 | 34 | }, |
| 33 | 35 | child: _buildLoginViewWidget(), |
| 34 | 36 | ); | ... | ... |