time_widget.dart
2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
74
75
76
77
78
79
80
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
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';
class TimerWidget extends StatefulWidget {
const TimerWidget({super.key, required this.canSendSms});
final bool canSendSms;
@override
State<StatefulWidget> createState() {
return _TimerWidgetState();
}
}
class _TimerWidgetState extends State<TimerWidget> {
late bool sendSmsIng;
@override
void initState() {
super.initState();
sendSmsIng = false;
}
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => TimerBloc(ticker: const TimerTicker()),
child: _buildCountdownWidget(),
);
}
Widget _buildCountdownWidget() => BlocBuilder<TimerBloc,TimerState>(
buildWhen: (prev, state) => prev.runtimeType != state.runtimeType,
builder: (context,state) {
final bloc = BlocProvider.of<TimerBloc>(context);
final duration = bloc.state.duration;
final secondsStr = (duration % 60).floor().toString().padLeft(2, '0');
if (state is TimerInitial) {
sendSmsIng = true;
} else {
sendSmsIng = false;
}
return GestureDetector(
onTap: () {
if (widget.canSendSms && !sendSmsIng) {
bloc.add(ResetEvent());
bloc.add(StartEvent(duration: bloc.state.duration));
}
},
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
widget.canSendSms?'securitycode'.assetPng:'securitycode_dis'.assetPng
),
fit: BoxFit.fill
),
),
padding: const EdgeInsets.symmetric(horizontal:12.0,vertical: 15.0),
child: Row(
children: [
if (state is TimerInitial)...[
const Text('获取验证码')
],
if (state is RunningState)...[
Text('${secondsStr}s倒计时')
],
if (state is FinishedState) ...[
const Text('获取验证码')
]
],
),
),
);
},
);
}