Blame view

lib/pages/lessons/bloc/lesson_bloc.dart 1.55 KB
60e47f7c   liangchengyou   feat:课程选择功能
1
  import 'package:flutter/cupertino.dart';
60e47f7c   liangchengyou   feat:课程选择功能
2
  import 'package:flutter_bloc/flutter_bloc.dart';
bcd47f52   Key   fixed: 接口list范型支持
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> {
60e47f7c   liangchengyou   feat:课程选择功能
13
14
15
16
17
18
19
20
    final int pageIndex;
  
    final PageController pageController;
  
    int _currentPageIndex = 0;
  
    int get currentPageIndex => _currentPageIndex;
  
bcd47f52   Key   fixed: 接口list范型支持
21
    List<CourseModuleEntity?> _listData = [];
993c1a04   liangchengyou   feat:添加数据模型
22
  
bcd47f52   Key   fixed: 接口list范型支持
23
24
25
    List<CourseModuleEntity?> get listData => _listData;
  
    LessonBloc(this.pageIndex, this.pageController) : super(LessonInitial()) {
60e47f7c   liangchengyou   feat:课程选择功能
26
27
28
29
      _currentPageIndex = pageIndex;
      on<PageViewChangeIndexEvent>(_pageIndexChange);
    }
  
13e6d11d   liangchengyou   feat:首页课程模块接口
30
31
32
    Future<void> requestData() async {
      try {
        await loading(() async {
bcd47f52   Key   fixed: 接口list范型支持
33
34
35
36
37
38
39
          var list = await HomeDao.courseModule();
          if (list?.isNotEmpty == true) {
            _listData = list!;
            emit(LessonDataLoadState());
          } else {
            EasyLoading.showToast("数据获取失败!");
          }
13e6d11d   liangchengyou   feat:首页课程模块接口
40
41
42
        });
      } catch (e) {
        if (e is ApiException) {
13e6d11d   liangchengyou   feat:首页课程模块接口
43
44
45
46
        }
      }
    }
  
bcd47f52   Key   fixed: 接口list范型支持
47
    void _pageIndexChange(PageViewChangeIndexEvent event, Emitter<LessonState> emitter) async {
60e47f7c   liangchengyou   feat:课程选择功能
48
49
50
51
52
53
54
55
56
57
      _currentPageIndex = event.index;
      emitter(PageIndexChangeState());
    }
  
    @override
    Future<void> close() {
      pageController.dispose();
      return super.close();
    }
  }