view.dart 10.2 KB

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app_update/azhon_app_update.dart';
import 'package:flutter_app_update/update_model.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:wow_english/common/core/app_config_helper.dart';
import 'package:wow_english/common/extension/string_extension.dart';
import 'package:wow_english/models/app_version_entity.dart';
import 'package:wow_english/pages/home/state.dart';
import 'package:wow_english/pages/home/widgets/BaseHomeHeaderWidget.dart';
import 'package:wow_english/pages/user/bloc/user_bloc.dart';

import '../../common/core/user_util.dart';
import '../../common/dialogs/show_dialog.dart';
import '../../utils/log_util.dart';
import 'bloc.dart';
import 'event.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wow_english/route/route.dart';

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

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

class _HomePageView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiBlocListener(listeners: [
      BlocListener<UserBloc, UserState>(listener: (context, state) {
        debugPrint('WQF ModuleSelectPage BlocListener state: $state');
      }),
      BlocListener<HomeBloc, HomeState>(
        listener: (context, state) {
          Log.d("WQF HomePage listener state: $state");
          if (state is UpdateDialogState) {
            _showUpdateDialog(context, state.forceUpdate, state.appVersionEntity);
          }
        },
      ),
    ], child: _homeView());
  }

  Widget _homeView() =>
      BlocBuilder<HomeBloc, HomeState>(
          builder: (context, state) {
            return Scaffold(
              body: Container(
                color: Colors.white,
                child: Column(
                  children: [
                    const BaseHomeHeaderWidget(),
                    Expanded(
                      child: Center(
                        child: Row(
                          children: [
                            Expanded(
                              child: GestureDetector(
                                onTap: () {
                                  if (UserUtil.isLogined()) {
                                    pushNamed(AppRouteName.courseUnit);
                                  } else {
                                    pushNamed(AppRouteName.login);
                                  }
                                },
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Stack(
                                        alignment: AlignmentDirectional.center,
                                        children: [
                                          Image.asset(
                                              'bg_frame_module'.assetPng,
                                              width: 162.5.w, height: 203.5.h),
                                          Center(
                                            child: Image.asset(
                                                'pic_module_study'.assetPng,
                                                width: 140.5.w,
                                                height: 172.h),
                                          )
                                        ]),
                                    10.verticalSpace,
                                    Image.asset('label_module_study'.assetPng,
                                        width: 124.w, height: 34.h),
                                  ],
                                ),
                              ),
                            ),
                            Expanded(
                              child: BlocBuilder<UserBloc, UserState>(
                                  builder: (context, userState) {
                                    debugPrint(
                                        'WQF ModuleSelectPage BlocBuilder state: $userState');
                                    return GestureDetector(
                                        onTap: () {
                                          //如果没登录先登录
                                          if (UserUtil.isLogined()) {
                                            if (AppConfigHelper
                                                .shouldHidePay()) {
                                              pushNamed(AppRouteName.games);
                                            } else {
                                              if (UserUtil
                                                  .hasGamePermission()) {
                                                pushNamed(AppRouteName.games);
                                              } else {
                                                showTwoActionDialog(
                                                    '提示', '忽略', '去续费',
                                                    '您的课程已到期,请快快续费继续学习吧!',
                                                    leftTap: () {
                                                      popPage();
                                                    }, rightTap: () {
                                                  popPage();
                                                  pushNamed(AppRouteName.shop);
                                                });
                                              }
                                            }
                                          } else {
                                            pushNamed(AppRouteName.login);
                                          }
                                        },
                                        child: Column(
                                          mainAxisAlignment: MainAxisAlignment
                                              .center,
                                          children: [
                                            Stack(
                                                alignment: AlignmentDirectional
                                                    .center,
                                                children: [
                                                  Image.asset(
                                                      'bg_frame_module'
                                                          .assetPng,
                                                      width: 162.5.w,
                                                      height: 203.5.h),
                                                  Image.asset(
                                                      'pic_module_game'
                                                          .assetPng,
                                                      width: 140.5.w,
                                                      height: 172.h)
                                                ]),
                                            10.verticalSpace,
                                            Image.asset(
                                                'label_module_game'.assetPng,
                                                width: 124.w, height: 34.h),
                                          ],
                                        ));
                                  }),
                            ),
                          ],
                        ),
                      ),
                    )
                  ],
                ),
              ),
            );
          });


  ///Flutter侧处理升级对话框
  ///[forcedUpgrade] 是否强制升级
  _showUpdateDialog(BuildContext context, bool forcedUpgrade,
      AppVersionEntity appVersionEntity) {
    showDialog(
      context: context,
      // 当我们点击除开对话框内容以外的区域是否关闭对话需用用到barrierDismissible参数 . 这个参数默认值是true ,但不能为null .
      barrierDismissible: !forcedUpgrade,
      builder: (BuildContext context) {
        return WillPopScope(
          onWillPop: () => Future.value(!forcedUpgrade),
          child: AlertDialog(
            title: const Text('发现新版本'),
            content: Text(
                appVersionEntity.remark ??
                    '修复了一些已知问题'),
            actions: <Widget>[
              TextButton(
                child: Text(forcedUpgrade ? '退出' : '取消'),
                onPressed: () =>
                {
                  if (forcedUpgrade) {
                    AppConfigHelper.exitApp()
                  } else
                    {
                      Navigator.of(context).pop()
                    }
                },
              ),
              TextButton(
                child: const Text('升级'),
                onPressed: () async {
                  if (AppConfigHelper.isIosPlatform()) {
                    _launchAppStore("6450870731");
                    return;
                  }
                  final String? apkUrl = appVersionEntity.packageUrl;
                  if (apkUrl == null || apkUrl.isEmpty) {
                    return;
                  }
                  UpdateModel model = UpdateModel(
                    apkUrl,
                    "wowenglish.apk",
                    "ic_launcher",
                    '',
                  );
                  AzhonAppUpdate.update(model).then((value) =>
                      debugPrint('$value'));
                  if (!forcedUpgrade) {
                    Navigator.of(context).pop();
                  }
                },
              ),
            ],
          ),
        );
      },
    );
  }

  void _launchAppStore(String appId) async {
    final String url = 'https://apps.apple.com/cn/app/wow-english/id$appId';
    if (await canLaunchUrl(Uri.parse(url))) {
      await launchUrl(Uri.parse(url));
    } else {
      throw 'Could not launch $url';
    }
  }
}