日期倒数计时器 [英] Date Countdown Timer

查看:87
本文介绍了日期倒数计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行倒数计时,并将每个倒数放入ListView的ItemView中.我已经有了 Listview.buillder(),但是我不知道如何进行具有不同值的倒计时并将其放入Listview.我已经看到还有另一个类似的问题,但是我无法解决它.

I'm trying to make countdowns to date and put each countdown in an ItemView in a ListView. I have already the Listview.buillder() but I don't know how to make countdowns which have different values and put them in the Listview. I have seen that there is another similar question but I can't solve my problem with it.

这是我的代码: home_screen.dart

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../recyclerview/data.dart';
import 'package:watch/constants.dart';

int itemCount = item.length;
List<bool> selected = new List<bool>();

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  initState() {
    for (var i = 0; i < itemCount; i++) {
    selected.add(false);
    }
    super.initState();
  }
 
  Icon notFavorite = Icon(Icons.favorite_border, size: 30,);
  Icon inFavorite = Icon(Icons.favorite, size: 30,);

  @override
  Widget build(BuildContext context) {
  int estimateTs = DateTime(2021, 11, 5, 7, 15, 30).millisecondsSinceEpoch;
    return new Scaffold(
      appBar: AppBar(
         title: Text('Accueil', style: kAppBarStyle,),
          //backgroundColor: Colors.white,  
          elevation: 0,
          automaticallyImplyLeading: false,
      ),
      body:  ListView.builder(
        scrollDirection: Axis.vertical,
        physics: BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
        itemCount: itemCount,
        itemBuilder: (BuildContext context, int index) {
      return Container(
        child: new Row(
          children: <Widget>[
            //Image
            new Container(
              margin: new EdgeInsets.only(top: 5.0, left: 1.0),
              child: new CachedNetworkImage(
                imageUrl: item[index].imageURL,
                height: MediaQuery.of(context).size.width / 3,
                width: MediaQuery.of(context).size.width / 2,
                fit: BoxFit.cover,
              ),
            ),
            //Text
            Expanded(
              child: new Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Spacer(),               
                //Titre
                Container(
                  padding: const EdgeInsets.only(bottom: 75.0, top: 8.0 ),
                  child: Text(
                    item[index].title,
                    style: kItemTitle,
                  ),
                ),
                //Decription
                Container(
                  padding: const EdgeInsets.only(left: 10.0, top: 8.0),
                  child:Text(
                    item[index].description,
                    style: kItemDescription,
                  ),
                ),
                //Favoris
                Spacer(),
                GestureDetector(
                  child: Container(
                    padding: const EdgeInsets.only(right: 10.0, top: 0.0),
                    child: selected.elementAt(index) ? inFavorite : notFavorite,
                  ),
                  onTap: () {
                    setState(() {
                      selected[index] = !selected.elementAt(index);
                    });
                    },
                ),
              ],
            ),
          ),
          
        ],
      ),
    );
    }
    )
  );
}
}

推荐答案

您需要使用计时器是这样的.

void startTimer() {
  // Start the periodic timer which prints something every 1 seconds
  timer=  new Timer.periodic(new Duration(seconds: 1), (time) {
    print('Something');
   
  });
}

您可以阅读更多.顺便说一句,有一个鼓舞人心的软件包,您可以尝试 flutter_countdown_timer .

You can read more. By the way, there is an inspiring package, you can try flutter_countdown_timer.

这篇关于日期倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆