首先-存在错误:您必须使用
data.remaining.inMilliseconds而不是
data.remaining.inSeconds
对于格式化,最好使用Duration而不是DateTime。但是您可以更改现有代码,例如:
var format = new DateFormat("mm:ss");var dateString = '${data.remaining.inHours}:${format.format(DateTime.fromMillisecondsSinceEpoch(data.remaining.inMilliseconds))};print(dateString);
UPD 小工具,用于显示剩余时间
int estimateTs = DateTime(2018, 11, 5, 7, 15, 30).millisecondsSinceEpoch; // set needed date....StreamBuilder(stream: Stream.periodic(Duration(seconds: 1), (i) => i),builder: (BuildContext context, AsyncSnapshot<int> snapshot) { DateFormat format = DateFormat("mm:ss"); int now = DateTime .now() .millisecondsSinceEpoch; Duration remaining = Duration(milliseconds: estimateTs - now); var dateString = '${remaining.inHours}:${format.format( DateTime.fromMillisecondsSinceEpoch(remaining.inMilliseconds))}'; print(dateString); return Container(color: Colors.greenAccent.withOpacity(0.3), alignment: Alignment.center, child: Text(dateString),);});
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)