日期倒数计时器

Date Countdown Timer

我正在尝试对日期进行倒计时,并将每个倒计时放在 ListView 的 ItemView 中。我已经有了 Listview.buillder() 但我不知道如何制作具有不同值的倒计时并将它们放在列表视图中。我看到还有另一个类似的问题,但我无法用它解决我的问题。

这是我的代码: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);
                    });
                    },
                ),
              ],
            ),
          ),
          
        ],
      ),
    );
    }
    )
  );
}
}

你需要像这样使用Timer

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

您可以阅读more. By the way, there is an inspiring package, you can try flutter_countdown_timer

代表问题作者回答:

我用flutter_countdown_timer的包做了一个倒数计时器。像这样:

CountdownTimer(
            endTime: DateTime(2020, 10, 22, 12, 48, 00).millisecondsSinceEpoch,
            textStyle: TextStyle(fontSize: 30, color: Colors.pink),
          ),

endTime时间为倒计时结束日期。