130 lines
3.3 KiB
Dart
130 lines
3.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:yumi/ui_kit/components/text/sc_text.dart';
|
|
|
|
class RoomRewardCountdownTimer extends StatefulWidget {
|
|
final DateTime targetDate;
|
|
|
|
const RoomRewardCountdownTimer({super.key, required this.targetDate});
|
|
|
|
@override
|
|
State<RoomRewardCountdownTimer> createState() =>
|
|
_RoomRewardCountdownTimerState();
|
|
}
|
|
|
|
class _RoomRewardCountdownTimerState extends State<RoomRewardCountdownTimer> {
|
|
Timer? _timer;
|
|
|
|
// 剩余时间
|
|
Duration _remainingTime = Duration.zero;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_startTimer();
|
|
}
|
|
|
|
void _startTimer() {
|
|
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
|
|
setState(() {
|
|
_remainingTime = widget.targetDate.difference(DateTime.now());
|
|
// 如果倒计时结束
|
|
if (_remainingTime.isNegative) {
|
|
_remainingTime = Duration.zero;
|
|
_timer?.cancel();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_timer?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
// 格式化数字为两位数
|
|
String _formatNumber(int number) {
|
|
return number.toString().padLeft(2, '0');
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// 计算天、时、分、秒
|
|
int days = _remainingTime.inDays;
|
|
int hours = _remainingTime.inHours.remainder(24);
|
|
int minutes = _remainingTime.inMinutes.remainder(60);
|
|
int seconds = _remainingTime.inSeconds.remainder(60);
|
|
return Center(
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
SizedBox(width: 10.w),
|
|
_TimeBox(
|
|
value: days < 10 ? "$days" : _formatNumber(days),
|
|
label: 'D',
|
|
),
|
|
SizedBox(width: 5.w,),
|
|
_TimeBox(value: _formatNumber(hours), label: ''),
|
|
_buildMaohao(),
|
|
_TimeBox(value: _formatNumber(minutes), label: ''),
|
|
_buildMaohao(),
|
|
_TimeBox(value: _formatNumber(seconds), label: ''),
|
|
SizedBox(width: 10.w),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
_buildMaohao(){
|
|
return text(
|
|
":",
|
|
fontSize: 24.sp,
|
|
fontWeight: FontWeight.bold,
|
|
textColor: Colors.white,
|
|
);
|
|
}
|
|
}
|
|
|
|
class _TimeBox extends StatelessWidget {
|
|
final String value;
|
|
final String label;
|
|
|
|
const _TimeBox({required this.value, required this.label});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Container(
|
|
width: 70.w,
|
|
height: 70.w,
|
|
decoration: BoxDecoration(
|
|
image: DecorationImage(
|
|
image: AssetImage(
|
|
"sc_images/room/sc_icon_room_reward_countdown_item_bg.png",
|
|
),
|
|
fit: BoxFit.fill,
|
|
),
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
text(
|
|
value,
|
|
fontSize: 20.sp,
|
|
fontWeight: FontWeight.bold,
|
|
textColor: Colors.white,
|
|
),
|
|
text(
|
|
label,
|
|
fontSize: 20.sp,
|
|
fontWeight: FontWeight.bold,
|
|
textColor: Colors.white,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|