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
8
|
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';
|
e1f36554
liangchengyou
feat:调整状态监听逻辑
|
9
|
class TimerWidget extends StatelessWidget {
|
17a80689
liangchengyou
feat:倒计时功能开发
|
10
11
12
13
14
|
const TimerWidget({super.key, required this.canSendSms});
final bool canSendSms;
@override
|
e1f36554
liangchengyou
feat:调整状态监听逻辑
|
15
16
17
18
19
|
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => TimerBloc(ticker: const TimerTicker()),
child: TimerWidgetView(canSendSms: canSendSms,),
);
|
17a80689
liangchengyou
feat:倒计时功能开发
|
20
21
22
|
}
}
|
e1f36554
liangchengyou
feat:调整状态监听逻辑
|
23
|
class TimerWidgetView extends StatelessWidget {
|
e1f36554
liangchengyou
feat:调整状态监听逻辑
|
24
25
26
|
final bool canSendSms;
const TimerWidgetView({super.key, required this.canSendSms});
|
17a80689
liangchengyou
feat:倒计时功能开发
|
27
|
|
17a80689
liangchengyou
feat:倒计时功能开发
|
28
29
|
@override
Widget build(BuildContext context) {
|
e1f36554
liangchengyou
feat:调整状态监听逻辑
|
30
31
|
return BlocListener<TimerBloc,TimerState>(
listener: (context, s) {
|
41b60caf
liangchengyou
feat:倒计时组件功能完善
|
32
33
|
if (s is FinishedState) {
///重置计时器
|
e1f36554
liangchengyou
feat:调整状态监听逻辑
|
34
|
context.read<TimerBloc>().add(ResetEvent());
|
e1f36554
liangchengyou
feat:调整状态监听逻辑
|
35
36
|
}
},
|
41b60caf
liangchengyou
feat:倒计时组件功能完善
|
37
|
child: _buildCountdownWidget(),
|
17a80689
liangchengyou
feat:倒计时功能开发
|
38
39
40
|
);
}
|
41b60caf
liangchengyou
feat:倒计时组件功能完善
|
41
|
Widget _buildCountdownWidget() => BlocBuilder<TimerBloc,TimerState>(
|
17a80689
liangchengyou
feat:倒计时功能开发
|
42
43
44
|
buildWhen: (prev, state) => prev.runtimeType != state.runtimeType,
builder: (context,state) {
final bloc = BlocProvider.of<TimerBloc>(context);
|
17a80689
liangchengyou
feat:倒计时功能开发
|
45
46
|
return GestureDetector(
onTap: () {
|
41b60caf
liangchengyou
feat:倒计时组件功能完善
|
47
|
if (canSendSms && !bloc.isCountTimer ) {
|
41b60caf
liangchengyou
feat:倒计时组件功能完善
|
48
|
bloc.add(StartEvent(duration: state.duration));
|
17a80689
liangchengyou
feat:倒计时功能开发
|
49
50
51
52
53
54
|
}
},
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
|
41b60caf
liangchengyou
feat:倒计时组件功能完善
|
55
|
canSendSms && !bloc.isCountTimer ? 'securitycode'.assetPng:'securitycode_dis'.assetPng
|
17a80689
liangchengyou
feat:倒计时功能开发
|
56
57
58
59
|
),
fit: BoxFit.fill
),
),
|
f0d56772
liangchengyou
feat:更新尺寸适配
|
60
|
padding: EdgeInsets.symmetric(horizontal:12.w,vertical: 15.h),
|
41b60caf
liangchengyou
feat:倒计时组件功能完善
|
61
|
child: Row(
|
17a80689
liangchengyou
feat:倒计时功能开发
|
62
|
children: [
|
17a80689
liangchengyou
feat:倒计时功能开发
|
63
|
if (state is RunningState)...[
|
41b60caf
liangchengyou
feat:倒计时组件功能完善
|
64
65
|
const TimerText()
] else ...[
|
f0d56772
liangchengyou
feat:更新尺寸适配
|
66
67
68
69
|
Text('获取验证码',
style: TextStyle(
fontSize: 12.sp
),)
|
17a80689
liangchengyou
feat:倒计时功能开发
|
70
71
72
73
74
75
76
|
]
],
),
),
);
},
);
|
41b60caf
liangchengyou
feat:倒计时组件功能完善
|
77
78
79
80
81
82
83
84
85
86
87
|
}
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:更新尺寸适配
|
88
89
90
|
style: TextStyle(
fontSize: 12.sp
),
|
41b60caf
liangchengyou
feat:倒计时组件功能完善
|
91
92
93
|
);
}
}
|