首页 > 解决方案 > Flutter Timer with Hrs, Mins, Sec - Source

问题描述

我只是想为此启动拍卖应用程序我需要一个计时器我在 YouTube 中搜索得到了链接 https://www.youtube.com/watch?v=Rkkb8P9LsPw&t=121s但它很奇怪我只需要这个内容有人可以帮助这个代码在这里https://github.com/tadaspetra/bookclub/tree/INTEGRATION_BUILD_0_1/book_club

我一个人需要这部分

在此处输入图像描述

标签: flutter

解决方案


如果没有看到您的代码,很难准确理解您要查找的内容,但是下面的示例可以用作基本倒计时计时器的起点:

import 'dart:async';

import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  DateTime _now;
  DateTime _auction;
  Timer _timer;

  @override
  Widget build(BuildContext context) {
    // Calculates the difference between the auction date time and the current date time.
    final difference = _auction.difference(_now);

    return Scaffold(
      body: Center(
        child: Card(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text('${difference.inHours} hours'),
              Text('${difference.inMinutes.remainder(60)} minutes'),
              Text('${difference.inSeconds.remainder(60)} seconds'),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    // Cancels the timer when the page is disposed.
    _timer.cancel();

    super.dispose();
  }

  @override
  void initState() {
    super.initState();

    // Sets the current date time.
    _now = DateTime.now();
    // Sets the date time of the auction.
    _auction = _now.add(Duration(days: 1));

    // Creates a timer that fires every second.
    _timer = Timer.periodic(
      Duration(
        seconds: 1,
      ),
      (timer) {
        setState(() {
          // Updates the current date time.
          _now = DateTime.now();

          // If the auction has now taken place, then cancels the timer.
          if (_auction.isBefore(_now)) {
            timer.cancel();
          }
        });
      },
    );
  }
}

此答案从以下问题中获得灵感,我建议您查看它们以获取有关您的问题的更多信息:


推荐阅读