12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import 'dart:async';
- import 'package:flutter/material.dart';
- ///倒计时文本
- class CountDownText extends StatefulWidget {
- final TextStyle? textStyle;
- final CountDownTextController controller;
- const CountDownText({super.key, required this.controller, this.textStyle});
- @override
- State<CountDownText> createState() => _CountDownTextState();
- }
- class _CountDownTextState extends State<CountDownText> {
- @override
- void initState() {
- super.initState();
- widget.controller.countText.addListener(_onUpdateCountText);
- }
- void _onUpdateCountText() {
- setState(() {});
- }
- @override
- void dispose() {
- super.dispose();
- widget.controller.countText.removeListener(_onUpdateCountText);
- widget.controller.stopCount();
- }
- @override
- Widget build(BuildContext context) {
- return Text(
- widget.controller.countText.value,
- style: widget.textStyle,
- );
- }
- }
- class CountDownTextController {
- ValueNotifier<String> countText = ValueNotifier("");
- void Function(int tick, bool complete)? onCount;
- Timer? _countTimer;
- CountDownTextController(this.onCount);
- bool get isCounting => _countTimer != null;
- void startCount(int time) {
- if (isCounting) {
- return;
- }
- if (time <= 0) {
- onCount?.call(0, true);
- return;
- }
- _countTimer = Timer.periodic(const Duration(seconds: 1), (tick) {
- int left = time - tick.tick;
- bool compelte = left <= 0;
- if (compelte) {
- _timeUp();
- } else {
- countText.value = "$left";
- }
- onCount?.call(tick.tick, compelte);
- });
- countText.value = "$time";
- }
- void stopCount() {
- _countTimer?.cancel();
- _countTimer = null;
- countText.value = "";
- }
- void _timeUp() {
- stopCount();
- }
- }
|