Commit fcc3a982c1465ab7d5cb1d1133fd0d2799f22d9e

Authored by liangchengyou
1 parent e8d9f472

feat:全局缓存bloc

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
  1 +part of 'cache_bloc.dart';
  2 +
  3 +@immutable
  4 +abstract class CacheEvent {}
  5 +
  6 +class UserInfoChangeEvent extends CacheEvent{
  7 + UserEntity? userEntity;
  8 + UserInfoChangeEvent(this.userEntity);
  9 +}
  10 +
  11 +class UserInfoClearEvent extends CacheEvent {}
... ...
lib/common/blocs/cachebloc/cache_state.dart 0 → 100644
  1 +part of 'cache_bloc.dart';
  2 +
  3 +@immutable
  4 +abstract class CacheState {}
  5 +
  6 +class CacheInitial extends CacheState {}
  7 +
  8 +class UserInfoChangeState extends CacheState {}
  9 +
  10 +class UserInfoClearState extends CacheState {}
... ...
lib/pages/login/loginpage/bloc/login_bloc.dart
  1 +import 'dart:js';
  2 +
1 3 import 'package:flutter/cupertino.dart';
2 4 import 'package:flutter_bloc/flutter_bloc.dart';
3 5 import 'package:flutter_easyloading/flutter_easyloading.dart';
... ...
lib/pages/login/loginpage/login_page.dart
... ... @@ -2,6 +2,7 @@ import &#39;package:flutter/gestures.dart&#39;;
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 );
... ...