time_widget.dart
2.53 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
81
82
83
84
85
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 StatelessWidget {
const TimerWidget({super.key, required this.canSendSms});
final bool canSendSms;
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => TimerBloc(ticker: const TimerTicker()),
child: TimerWidgetView(canSendSms: canSendSms,),
);
}
}
class TimerWidgetView extends StatelessWidget {
final bool canSendSms;
const TimerWidgetView({super.key, required this.canSendSms});
@override
Widget build(BuildContext context) {
bool sendSmsIng = false;
return BlocListener<TimerBloc,TimerState>(
listener: (context, s) {
if (s is RunningState) {
sendSmsIng = true;
} if (s is FinishedState) {
sendSmsIng = false;
context.read<TimerBloc>().add(ResetEvent());
} else {
sendSmsIng = false;
}
},
child: _buildCountdownWidget(sendSmsIng:sendSmsIng),
);
}
Widget _buildCountdownWidget({required bool sendSmsIng}) => 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');
return GestureDetector(
onTap: () {
if (canSendSms && !sendSmsIng) {
bloc.add(ResetEvent());
}
},
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
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('获取验证码')
]
],
),
),
);
},
);
}