【问题标题】:Flutter Timer with Hrs, Mins, Sec - SourceFlutter Timer with Hrs, Mins, Sec - Source
【发布时间】:2020-10-18 11:15:37
【问题描述】:

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

我只需要这部分

【问题讨论】:

  • 您做了什么,还是希望我们为您编码?如果是第一个,请向我们展示您迄今为止取得的成就,以便我们为您提供帮助,如果您想要第二个,这可能不是问的地方

标签: flutter


【解决方案1】:

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

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();
          }
        });
      },
    );
  }
}

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

【讨论】:

    猜你喜欢
    • 2012-09-04
    • 2016-09-24
    • 2015-07-28
    • 2016-09-25
    • 2015-07-02
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    相关资源
    最近更新 更多