问题
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.
Here is my code : 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);
});
},
),
],
),
),
],
),
);
}
)
);
}
}
回答1:
You need to use a Timer like this.
void startTimer() {
// Start the periodic timer which prints something every 1 seconds
timer= new Timer.periodic(new Duration(seconds: 1), (time) {
print('Something');
});
}
You can read more. By the way, there is an inspiring package, you can try flutter_countdown_timer.
回答2:
Answer on the behalf of the author of the question:
I have made a countdown timer with flutter_countdown_timer's package. Like this :
CountdownTimer(
endTime: DateTime(2020, 10, 22, 12, 48, 00).millisecondsSinceEpoch,
textStyle: TextStyle(fontSize: 30, color: Colors.pink),
),
the endTime time is the date of the end of the countdown.
来源:https://stackoverflow.com/questions/64451276/date-countdown-timer