home_page.dart 6.44 KB
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wow_english/app/app.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_entity.dart';
import 'package:wow_english/pages/home/widgets/home_bouns_item.dart';
import 'package:wow_english/pages/home/widgets/home_tab_header_widget.dart';
import 'package:wow_english/pages/home/widgets/home_vidoe_item.dart';
import 'package:wow_english/route/route.dart';
import 'package:wow_english/utils/toast_util.dart';

import 'bloc/home_bloc.dart';

class HomePage extends StatelessWidget {
  const HomePage({super.key, this.moduleId});

  /// 模块id
  final String? moduleId;

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => HomeBloc(moduleId)..add(RequestDataEvent()),
      child: _HomePageView(),
    );
  }
}

class _HomePageView extends StatelessWidget {
  void _headerActionEvent(HeaderActionType type) {
    if (type == HeaderActionType.video) {
      pushNamed(AppRouteName.reAfter);
    } else if (type == HeaderActionType.phase) {
      pushNamed(AppRouteName.lesson);
    } else if (type == HeaderActionType.listen) {
      pushNamed(AppRouteName.listen);
    } else if (type == HeaderActionType.shop) {
      pushNamed(AppRouteName.shop);
    } else if (type == HeaderActionType.user) {
      if(UserUtil.token.isEmpty) {
        pushNamed(AppRouteName.login);
      } else {
        pushNamed(AppRouteName.user);
      }
    } else {

    }
  }

  @override
  Widget build(BuildContext context) {
    final bloc = BlocProvider.of<HomeBloc>(context);
    return BlocListener<HomeBloc, HomeState>(
      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});
        }
      },
      child: _homeView(),
    );
  }

  Widget _homeView() => BlocBuilder<HomeBloc, HomeState>(builder: (context, state) {
    final bloc = BlocProvider.of<HomeBloc>(context);
    return Scaffold(
      body: Container(
        color: Colors.white,
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              HomeTabHeaderWidget(
                actionTap: (HeaderActionType type) {
                  _headerActionEvent(type);
                },
              ),
              Expanded(
                  child: ListView.builder(
                      itemCount: bloc.modelData?.totalCourseLesson,
                      scrollDirection: Axis.horizontal,
                      itemBuilder: (BuildContext context, int index) {
                        CourseCourseLessons? data = bloc.modelData?.courseLessons?[index];
                        if (data?.courseType == 5) {
                          //彩蛋
                          return GestureDetector(
                            onTap: () {
                              if (data!.lock!) {
                                showToast('当前课程暂未解锁');
                                return;
                              }
                              bloc.add(RequestVideoLessonEvent(data.id!,data.courseType!));
                            },
                            child: HomeBoundsItem(
                              imageUrl: data?.coverUrl,
                            ),
                          );
                        } else {
                          return GestureDetector(
                            onTap: () {
                              if (data!.lock!) {
                                showToast('当前课程暂未解锁');
                                return;
                              }
                              if (data.courseType == 4) {//绘本
                                Navigator.of(context).pushNamed(AppRouteName.reading, arguments: {'courseLessonId':data.id!});
                                return;
                              }

                              if (data.courseType == 3) {//练习
                                Navigator.of(context).pushNamed(AppRouteName.topicPic,arguments: {'courseLessonId':data.id!});
                                return;
                              }

                              //儿歌/看视频
                              bloc.add(RequestVideoLessonEvent(data.id!,data.courseType!));
                            },
                            child: HomeVideoItem(
                              lessons: data,
                            ),
                          );
                        }
                      })),
              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: Colors.blue,
                          borderRadius: BorderRadius.circular(14.5.r),
                        ),
                        padding: EdgeInsets.symmetric(vertical: 8.h, horizontal: 24.w),
                        child: Text(
                          '${(bloc.modelData?.nowCourseLesson)}/${bloc.modelData?.totalCourseLesson}',
                          style: TextStyle(color: Colors.white, fontSize: 12.sp),
                        ),
                      ),
                      Image.asset(
                        'blue-positive'.assetPng,
                        height: 47.h,
                        width: 80.w,
                        // color: Colors.red,
                      ),
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  });
}