Blame view

lib/pages/lessons/bloc/lesson_bloc.dart 1.56 KB
60e47f7c   liangchengyou   feat:课程选择功能
1
  import 'package:flutter/cupertino.dart';
60e47f7c   liangchengyou   feat:课程选择功能
2
  import 'package:flutter_bloc/flutter_bloc.dart';
842b7132   liangchengyou   feat:磨耳朵/练习页面调整
3
  import 'package:flutter_easyloading/flutter_easyloading.dart';
13e6d11d   liangchengyou   feat:首页课程模块接口
4
5
  import 'package:wow_english/common/request/dao/home_dao.dart';
  import 'package:wow_english/common/request/exception.dart';
bcd47f52   Key   fixed: 接口list范型支持
6
  import 'package:wow_english/models/course_module_entity.dart';
13e6d11d   liangchengyou   feat:首页课程模块接口
7
  import 'package:wow_english/utils/loading.dart';
60e47f7c   liangchengyou   feat:课程选择功能
8
9
10
11
12
  
  part 'lesson_event.dart';
  part 'lesson_state.dart';
  
  class LessonBloc extends Bloc<LessonEvent, LessonState> {
8988aa69   liangchengyou   feat:首页+课程列表数据获取
13
  
60e47f7c   liangchengyou   feat:课程选择功能
14
15
16
17
18
19
20
21
    final int pageIndex;
  
    final PageController pageController;
  
    int _currentPageIndex = 0;
  
    int get currentPageIndex => _currentPageIndex;
  
bcd47f52   Key   fixed: 接口list范型支持
22
    List<CourseModuleEntity?> _listData = [];
993c1a04   liangchengyou   feat:添加数据模型
23
  
bcd47f52   Key   fixed: 接口list范型支持
24
25
    List<CourseModuleEntity?> get listData => _listData;
  
8988aa69   liangchengyou   feat:首页+课程列表数据获取
26
    LessonBloc(this.pageIndex,this.pageController) : super(LessonInitial()) {
60e47f7c   liangchengyou   feat:课程选择功能
27
28
      _currentPageIndex = pageIndex;
      on<PageViewChangeIndexEvent>(_pageIndexChange);
842b7132   liangchengyou   feat:磨耳朵/练习页面调整
29
      on<RequestDataEvent>(_requestData);
60e47f7c   liangchengyou   feat:课程选择功能
30
31
    }
  
842b7132   liangchengyou   feat:磨耳朵/练习页面调整
32
33
34
35
36
37
38
  
    void _pageIndexChange(PageViewChangeIndexEvent event,Emitter<LessonState> emitter) async {
      _currentPageIndex = event.index;
      emitter(PageIndexChangeState());
    }
  
    void _requestData(RequestDataEvent event, Emitter<LessonState> emitter) async {
13e6d11d   liangchengyou   feat:首页课程模块接口
39
40
      try {
        await loading(() async {
8988aa69   liangchengyou   feat:首页+课程列表数据获取
41
          _listData = await HomeDao.courseModule()??[];
842b7132   liangchengyou   feat:磨耳朵/练习页面调整
42
          emitter(LessonDataLoadState());
13e6d11d   liangchengyou   feat:首页课程模块接口
43
44
45
        });
      } catch (e) {
        if (e is ApiException) {
842b7132   liangchengyou   feat:磨耳朵/练习页面调整
46
          EasyLoading.showToast(e.message??'请求失败,请检查网络连接');
13e6d11d   liangchengyou   feat:首页课程模块接口
47
48
49
50
        }
      }
    }
  
60e47f7c   liangchengyou   feat:课程选择功能
51
52
53
54
55
56
    @override
    Future<void> close() {
      pageController.dispose();
      return super.close();
    }
  }