自动倒计时重置

问题描述 投票:0回答:1

我希望当倒计时达到 0:00 时,它应该自动重新启动,并且不应在初始状态下重新启动,请帮助我如何解决这个问题,我对此感到厌倦,请任何人现在如何解决这个问题..... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... ...................................................... …………

import 'package:flutter/material.dart';
import 'package:slide_countdown/slide_countdown.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: ExampleSlideCountdown(),
    );
  }
}

class ExampleSlideCountdown extends StatefulWidget {
  const ExampleSlideCountdown({Key? key}) : super(key: key);

  @override
  _ExampleSlideCountdownState createState() => _ExampleSlideCountdownState();
}

class _ExampleSlideCountdownState extends State<ExampleSlideCountdown> {
  late Duration _countdownDuration;
  final Duration _initialDuration = const Duration(minutes: 0, seconds: 10);

  @override
  void initState() {
    super.initState();
    _countdownDuration = _initialDuration;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Countdown Timer'),
      ),
      body: SizedBox.expand(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Padding(
              padding: EdgeInsets.only(top: 20, bottom: 10),
              child: Text('Countdown Timer'),
            ),
            Container(
              height: 200,
              width: 200,
              child: SlideCountdown(
                duration: _countdownDuration,
                style: const TextStyle(fontSize: 40),
                onDone: () {
                  setState(() {
                    _countdownDuration = _initialDuration;
                  });
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}

                                                                                                  
flutter solution
1个回答
0
投票

为“SlideCountdown”添加“key”,如下所示:

SlideCountdown(
              key: UniqueKey(),
                duration: _countdownDuration,
                style: const TextStyle(fontSize: 40),
                onDone: () {
                  setState(() {
                    _countdownDuration = _initialDuration;
                  });
                },
              ),

到达0后将重新开始倒计时

© www.soinside.com 2019 - 2024. All rights reserved.