section_page.dart 9.03 KB
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wow_english/common/core/user_util.dart';
import 'package:wow_english/common/extension/string_extension.dart';
import 'package:wow_english/models/course_unit_entity.dart';
import 'package:wow_english/pages/section/widgets/home_video_item.dart';
import 'package:wow_english/pages/section/widgets/section_bouns_item.dart';
import 'package:wow_english/pages/section/widgets/section_header_widget.dart';
import 'package:wow_english/route/route.dart';
import 'package:wow_english/utils/toast_util.dart';

import '../../models/course_section_entity.dart';
import 'bloc/section_bloc.dart';
import 'courese_module_model.dart';

/// 环节列表页
class SectionPage extends StatelessWidget {
  const SectionPage({super.key, required this.courseUnitEntity, required this.courseUnitDetail});

  final CourseUnitEntity courseUnitEntity;

  /// unitId
  final CourseUnitDetail courseUnitDetail;

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => SectionBloc(courseUnitEntity, courseUnitDetail)..add(RequestDataEvent()),
      child: _SectionPageView(context),
    );
  }
}

class _SectionPageView extends StatelessWidget {
  const _SectionPageView(this.context);

  final BuildContext context;

  @override
  Widget build(BuildContext context) {
    final bloc = BlocProvider.of<SectionBloc>(context);
    return BlocListener<SectionBloc, SectionState>(
      listener: (context, state) {
        if (state is RequestVideoLessonState) {
          final videoUrl = bloc.processEntity?.videos?.videoUrl ?? '';
          var title = '';
          if (state.type == 1) {
            title = 'song';
          }

          if (state.type == 2) {
            title = 'video';
          }

          if (state.type == 5) {
            title = 'bonus';
          }

          if (videoUrl.isEmpty || !videoUrl.contains('http')) {
            return;
          }
          pushNamed(AppRouteName.lookVideo, arguments: {
            'videoUrl': videoUrl,
            'title': title,
            'courseLessonId': state.courseLessonId
          }).then((value) {
            if (value != null) {
              Map<String, String> dataMap = value as Map<String, String>;
              bloc.add(RequestExitClassEvent(
                dataMap['courseLessonId']!,
                '0',
                dataMap['currentTime']!,
              ));
            }
          });
          return;
        }

        if (state is RequestEnterClassState) {
          if (state.courseType != 3 && state.courseType != 4) {
            ///视频类型
            ///获取视频课程内容
            bloc.add(RequestVideoLessonEvent(
                state.courseLessonId, state.courseType));
            return;
          }

          if (state.courseType == 4) {
            //绘本
            pushNamed(AppRouteName.reading,
                    arguments: {'courseLessonId': state.courseLessonId})
                .then((value) {
              if (value != null) {
                Map<String, String> dataMap = value as Map<String, String>;
                bloc.add(RequestExitClassEvent(
                    dataMap['courseLessonId']!, dataMap['currentStep']!, '0'));
              }
            });
            return;
          }

          if (state.courseType == 3) {
            //练习
            pushNamed(AppRouteName.topicPic,
                    arguments: {'courseLessonId': state.courseLessonId})
                .then((value) {
              if (value != null) {
                Map<String, String> dataMap = value as Map<String, String>;
                bloc.add(RequestExitClassEvent(
                    dataMap['courseLessonId']!, dataMap['currentStep']!, '0'));
              }
            });
            return;
          }
        }
      },
      child: _homeView(),
    );
  }

  Widget _homeView() =>
      BlocBuilder<SectionBloc, SectionState>(builder: (context, state) {
        final bloc = BlocProvider.of<SectionBloc>(context);
        return Scaffold(
          body: Container(
            color: Colors.white,
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  SectionHeaderWidget(
                      title: bloc.courseUnitDetail.name,
                      courseModuleCode: bloc.courseUnitEntity.courseModuleCode),
                  Expanded(
                      child: ListView.builder(
                          itemCount: bloc.courseSectionDatas?.length ?? 0,
                          scrollDirection: Axis.horizontal,
                          itemBuilder: (BuildContext context, int index) {
                            CourseSectionEntity sectionData =
                                bloc.courseSectionDatas![index];
                            if (sectionData.courseType == 5) {
                              //彩蛋
                              return GestureDetector(
                                onTap: () {
                                  if (!UserUtil.isLogined()) {
                                    pushNamed(AppRouteName.login);
                                    return;
                                  }
                                  if (sectionData.lock == true) {
                                    showToast('当前课程暂未解锁');
                                    return;
                                  }

                                  ///进入课堂
                                  bloc.add(RequestEnterClassEvent(
                                      sectionData.id.toString(), sectionData.courseType));
                                },
                                child: SectionBoundsItem(
                                  imageUrl: sectionData.coverUrl,
                                ),
                              );
                            } else {
                              return GestureDetector(
                                onTap: () {
                                  if (!UserUtil.isLogined()) {
                                    pushNamed(AppRouteName.login);
                                    return;
                                  }
                                  if (sectionData.lock == true) {
                                    showToast('当前课程暂未解锁');
                                    return;
                                  }

                                  ///进入课堂
                                  bloc.add(RequestEnterClassEvent(
                                      sectionData.id.toString(), sectionData.courseType));
                                },
                                child: SectionVideoItem(
                                  unitEntity: bloc.courseUnitEntity,
                                  lessons: sectionData,
                                ),
                              );
                            }
                          })),
                  // SafeArea(
                  //   child: Padding(
                  //     padding: EdgeInsets.symmetric(horizontal: 13.w),
                  //     child: Row(
                  //       mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  //       children: [
                  //         SizedBox(
                  //           height: 47.h,
                  //           width: 80.w,
                  //         ),
                  //         Container(
                  //           decoration: BoxDecoration(
                  //             color: CourseModuleModel(
                  //                     bloc.courseUnitEntity.courseModuleCode ??
                  //                         'Phase-1')
                  //                 .color,
                  //             borderRadius: BorderRadius.circular(14.5.r),
                  //           ),
                  //           padding: EdgeInsets.symmetric(
                  //               vertical: 8.h, horizontal: 24.w),
                  //           child: Text(
                  //             '${(bloc.courseUnitEntity.nowStep ?? 0)}/${bloc.courseUnitEntity.total ?? 0}',
                  //             style: TextStyle(
                  //                 color: Colors.white, fontSize: 12.sp),
                  //           ),
                  //         ),
                  //         Image.asset(
                  //           CourseModuleModel(
                  //                   bloc.courseUnitEntity.courseModuleCode ??
                  //                       'Phase-1')
                  //               .courseModuleLogo
                  //               .assetPng,
                  //           height: 47.h,
                  //           width: 80.w,
                  //           // color: Colors.red,
                  //         ),
                  //       ],
                  //     ),
                  //   ),
                  // )
                ],
              ),
            ),
          ),
        );
      });
}