Blame view

lib/pages/login/loginpage/time_widget.dart 4.21 KB
17a80689   liangchengyou   feat:倒计时功能开发
1
  import 'package:flutter/cupertino.dart';
41b60caf   liangchengyou   feat:倒计时组件功能完善
2
  import 'package:flutter/material.dart';
17a80689   liangchengyou   feat:倒计时功能开发
3
  import 'package:flutter_bloc/flutter_bloc.dart';
f0d56772   liangchengyou   feat:更新尺寸适配
4
  import 'package:flutter_screenutil/flutter_screenutil.dart';
17a80689   liangchengyou   feat:倒计时功能开发
5
6
7
  import 'package:wow_english/common/blocs/timerbloc/timer_bloc.dart';
  import 'package:wow_english/common/extension/string_extension.dart';
  import 'package:wow_english/common/widgets/timer_ticker.dart';
65e33ae8   liangchengyou   feat:更新代码
8
  import 'package:wow_english/pages/login/forgetpwd/bloc/forget_pwd_home_bloc.dart';
39e06486   liangchengyou   feat:获取验证码逻辑处理
9
  import 'package:wow_english/pages/login/loginpage/bloc/login_bloc.dart';
17a80689   liangchengyou   feat:倒计时功能开发
10
  
e1f36554   liangchengyou   feat:调整状态监听逻辑
11
  class TimerWidget extends StatelessWidget {
65e33ae8   liangchengyou   feat:更新代码
12
    const TimerWidget({super.key, required this.canSendSms,this.sendSmsEvent,this.pageType = 0});
17a80689   liangchengyou   feat:倒计时功能开发
13
  
65e33ae8   liangchengyou   feat:更新代码
14
    final int pageType;
17a80689   liangchengyou   feat:倒计时功能开发
15
    final bool canSendSms;
39e06486   liangchengyou   feat:获取验证码逻辑处理
16
    final Function()? sendSmsEvent;
17a80689   liangchengyou   feat:倒计时功能开发
17
18
  
    @override
e1f36554   liangchengyou   feat:调整状态监听逻辑
19
20
21
    Widget build(BuildContext context) {
      return BlocProvider(
        create: (_) => TimerBloc(ticker: const TimerTicker()),
39e06486   liangchengyou   feat:获取验证码逻辑处理
22
        child: TimerWidgetView(
65e33ae8   liangchengyou   feat:更新代码
23
          pageType: pageType,
39e06486   liangchengyou   feat:获取验证码逻辑处理
24
25
26
          canSendSms: canSendSms,
          sendSmsEvent: sendSmsEvent,
        ),
e1f36554   liangchengyou   feat:调整状态监听逻辑
27
      );
17a80689   liangchengyou   feat:倒计时功能开发
28
29
30
    }
  }
  
e1f36554   liangchengyou   feat:调整状态监听逻辑
31
  class TimerWidgetView extends StatelessWidget {
e1f36554   liangchengyou   feat:调整状态监听逻辑
32
    final bool canSendSms;
65e33ae8   liangchengyou   feat:更新代码
33
    final int pageType;
7912b3f7   liangchengyou   feat:更新代码
34
    final Function()? sendSmsEvent;
65e33ae8   liangchengyou   feat:更新代码
35
    const TimerWidgetView({super.key, required this.canSendSms,this.sendSmsEvent,this.pageType = 0});
17a80689   liangchengyou   feat:倒计时功能开发
36
  
17a80689   liangchengyou   feat:倒计时功能开发
37
38
    @override
    Widget build(BuildContext context) {
39e06486   liangchengyou   feat:获取验证码逻辑处理
39
40
      return MultiBlocListener(
        listeners: [
c9df43c8   Key   feat: 修改个人信息、接口
41
          if(pageType == 0) ...[//登录
65e33ae8   liangchengyou   feat:更新代码
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
            BlocListener<TimerBloc,TimerState>(
                listener: (context, s) {
                  if (s is FinishedState) {
                    ///重置计时器
                    context.read<TimerBloc>().add(ResetEvent());
                  }
                }),
            BlocListener<LoginBloc,LoginState>(
                listener: (context, s) {
                  if (s is SmsCodeRequestState) {
                    final bloc = BlocProvider.of<TimerBloc>(context);
                    ///开始倒计时
                    bloc.add(StartEvent(duration: bloc.state.duration));
                  }
                }),
          ] else if (pageType == 1) ...[//忘记密码
            BlocListener<TimerBloc,TimerState>(
                listener: (context, s) {
                  if (s is FinishedState) {
                    ///重置计时器
                    context.read<TimerBloc>().add(ResetEvent());
                  }
                }),
            BlocListener<ForgetPwdHomeBloc,ForgetPwdHomeState>(
                listener: (context, s) {
                  if (s is SendSmsCodeState) {
                    final bloc = BlocProvider.of<TimerBloc>(context);
                    ///开始倒计时
                    bloc.add(StartEvent(duration: bloc.state.duration));
                  }
                }),
          ]
39e06486   liangchengyou   feat:获取验证码逻辑处理
74
        ],
41b60caf   liangchengyou   feat:倒计时组件功能完善
75
        child: _buildCountdownWidget(),
17a80689   liangchengyou   feat:倒计时功能开发
76
77
78
      );
    }
  
41b60caf   liangchengyou   feat:倒计时组件功能完善
79
    Widget _buildCountdownWidget() => BlocBuilder<TimerBloc,TimerState>(
17a80689   liangchengyou   feat:倒计时功能开发
80
81
82
      buildWhen: (prev, state) => prev.runtimeType != state.runtimeType,
      builder: (context,state) {
        final bloc = BlocProvider.of<TimerBloc>(context);
17a80689   liangchengyou   feat:倒计时功能开发
83
84
        return GestureDetector(
          onTap: () {
41b60caf   liangchengyou   feat:倒计时组件功能完善
85
            if (canSendSms && !bloc.isCountTimer ) {
39e06486   liangchengyou   feat:获取验证码逻辑处理
86
              sendSmsEvent?.call();
17a80689   liangchengyou   feat:倒计时功能开发
87
88
89
90
91
92
            }
          },
          child: Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(
41b60caf   liangchengyou   feat:倒计时组件功能完善
93
                      canSendSms && !bloc.isCountTimer ? 'securitycode'.assetPng:'securitycode_dis'.assetPng
17a80689   liangchengyou   feat:倒计时功能开发
94
95
96
97
                  ),
                  fit: BoxFit.fill
              ),
            ),
f0d56772   liangchengyou   feat:更新尺寸适配
98
            padding: EdgeInsets.symmetric(horizontal:12.w,vertical: 15.h),
41b60caf   liangchengyou   feat:倒计时组件功能完善
99
            child: Row(
17a80689   liangchengyou   feat:倒计时功能开发
100
              children: [
17a80689   liangchengyou   feat:倒计时功能开发
101
                if (state is RunningState)...[
41b60caf   liangchengyou   feat:倒计时组件功能完善
102
103
                  const TimerText()
                ] else ...[
f0d56772   liangchengyou   feat:更新尺寸适配
104
105
106
107
                  Text('获取验证码',
                    style: TextStyle(
                        fontSize: 12.sp
                    ),)
17a80689   liangchengyou   feat:倒计时功能开发
108
109
110
111
112
113
114
                ]
              ],
            ),
          ),
        );
      },
    );
41b60caf   liangchengyou   feat:倒计时组件功能完善
115
116
117
118
119
120
121
122
123
124
125
  }
  
  class TimerText extends StatelessWidget {
    const TimerText({super.key});
  
    @override
    Widget build(BuildContext context) {
      final duration = context.select((TimerBloc bloc) => bloc.state.duration);
      final secondsStr = duration.toString().padLeft(2, '0');
      return Text(
        '${secondsStr}s后再次获取',
f0d56772   liangchengyou   feat:更新尺寸适配
126
127
128
        style: TextStyle(
          fontSize: 12.sp
        ),
41b60caf   liangchengyou   feat:倒计时组件功能完善
129
130
131
      );
    }
  }