Blame view

lib/pages/lessons/bloc/lesson_bloc.dart 1.59 KB
993c1a04   liangchengyou   feat:添加数据模型
1
2
  import 'dart:convert';
  
60e47f7c   liangchengyou   feat:课程选择功能
3
  import 'package:flutter/cupertino.dart';
993c1a04   liangchengyou   feat:添加数据模型
4
  import 'package:flutter/foundation.dart';
60e47f7c   liangchengyou   feat:课程选择功能
5
  import 'package:flutter_bloc/flutter_bloc.dart';
13e6d11d   liangchengyou   feat:首页课程模块接口
6
7
  import 'package:wow_english/common/request/dao/home_dao.dart';
  import 'package:wow_english/common/request/exception.dart';
993c1a04   liangchengyou   feat:添加数据模型
8
  import 'package:wow_english/models/lesson_entity.dart';
13e6d11d   liangchengyou   feat:首页课程模块接口
9
  import 'package:wow_english/utils/loading.dart';
60e47f7c   liangchengyou   feat:课程选择功能
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  
  part 'lesson_event.dart';
  part 'lesson_state.dart';
  
  class LessonBloc extends Bloc<LessonEvent, LessonState> {
  
    final int pageIndex;
  
    final PageController pageController;
  
    int _currentPageIndex = 0;
  
    int get currentPageIndex => _currentPageIndex;
  
993c1a04   liangchengyou   feat:添加数据模型
24
25
26
    List<LessonEntity?> _listData = [];
    List<LessonEntity?> get listData => _listData;
  
60e47f7c   liangchengyou   feat:课程选择功能
27
28
29
30
31
    LessonBloc(this.pageIndex,this.pageController) : super(LessonInitial()) {
      _currentPageIndex = pageIndex;
      on<PageViewChangeIndexEvent>(_pageIndexChange);
    }
  
13e6d11d   liangchengyou   feat:首页课程模块接口
32
33
34
    Future<void> requestData() async {
      try {
        await loading(() async {
993c1a04   liangchengyou   feat:添加数据模型
35
36
37
38
39
40
41
          List list = await HomeDao.courseModule()??[];
          // if (list.isNotEmpty) {
          //   for (dynamic jsonStr in list) {
          //     LessonEntity? data = LessonEntity.fromJson(jsonStr);
          //     _listData.add(data);
          //   }
          // }
13e6d11d   liangchengyou   feat:首页课程模块接口
42
43
44
45
46
47
48
49
50
          emit(LessonDataLoadState());
        });
      } catch (e) {
        if (e is ApiException) {
  
        }
      }
    }
  
60e47f7c   liangchengyou   feat:课程选择功能
51
52
53
54
55
56
57
58
59
60
61
    void _pageIndexChange(PageViewChangeIndexEvent event,Emitter<LessonState> emitter) async {
      _currentPageIndex = event.index;
      emitter(PageIndexChangeState());
    }
  
    @override
    Future<void> close() {
      pageController.dispose();
      return super.close();
    }
  }