倒计时到日期时间,停止一段时间并在一段时间后再次计数
Countdown to datetime, stop for a period and after period, count again
在周四、周五、周六、周日下午 6 点至晚上 10 点,应用允许用户某些权限。
我想显示从任何日期到每个星期四至星期日(下午 6 点至晚上 10 点)这段时间的倒计时
例如,如果是星期一,计时器将倒计时到星期四下午 6 点。如果是星期五早上 8 点,则倒计时到星期五下午 6 点。在任何时候,计时器都应该显示一次计时。
如果在周四至周日(下午 6 点至晚上 10 点)内,计时器将消失。仅在周四至周日再次出现 22:00:01。
我该如何实现?此外,对于日期列表,是否有一种方法可以确保每个星期四 - 星期日(下午 6 点)倒计时,而不是像看到的那样对日期进行硬编码。没有使用包。我被困在下面的代码中。
以下是我截断的代码:
List<DateTime> date =
[DateTime.parse("2021-08-26 18:00:00Z"),
DateTime.parse("2021-08-27 18:00:00Z"),
DateTime.parse("2021-08-28 18:00:00Z"),
DateTime.parse("2021-08-29 18:00:00Z"),];
@override
Widget build(BuildContext context){
return Scaffold(
body: Center(child: ListView.builder(
itemCount: date.length,
itemBuilder: (context, index){
countTime = CountDown().timeLeft(date[0], 'Completed');
return Text(
countTime,
style(TextStyle(fontSize:25.0));
...
感谢您的帮助。
编辑如下:
编辑以显示倒计时中增加的天数
小部件错误。
remainingTimeToText 上的代码被截断
String remainingTimeToText(Duration duration) {
DateTime now = DateTime.now();
int days = duration.inDays;
int hours = duration.inHours - days * 24;
int minutes = duration.inMinutes - hours * 60;
int seconds = duration.inSeconds - hours * 3600 - minutes * 60;
if (now.weekday < 4) {
return days.toString() +
' days ' +
hours.toString() +
":" +
minutes.toString() +
":" +
seconds.toString();
} else if (now.weekday>3){return
hours.toString() +
":" +
minutes.toString() +
":" +
seconds.toString();
}
import 'package:flutter/material.dart';
//needed for using the Timer
import 'dart:async';
class TimerScreen extends StatefulWidget {
const TimerScreen({Key? key}) : super(key: key);
@override
_TimerScreenState createState() => _TimerScreenState();
}
class _TimerScreenState extends State<TimerScreen> {
//the time left till the next special time comes up
String timeLeft = "";
///takes an int when converting it to string adds
/// a 0 in fron when it is smaller than 10
///
///will look nicer when displayed on the screen
String customString(int i) {
if (i < 10) {
return "0" + i.toString();
} else {
return i.toString();
}
}
///converts a duration to a
///string: days hours:minuts:seconds
String remainingTimeToText(Duration duration) {
print(duration);
int days = duration.inDays;
int hours = duration.inHours - days * 24;
int minutes = duration.inMinutes - hours * 60 - days * 24 * 60;
int seconds =
duration.inSeconds - hours * 3600 - minutes * 60 - days * 86400;
if (days > 0) {
String s = " days ";
if (days == 1) {
s = " day ";
}
return days.toString() +
s +
customString(hours) +
":" +
customString(minutes) +
":" +
customString(seconds);
} else {
return customString(hours) +
":" +
customString(minutes) +
":" +
customString(seconds);
}
}
///given a list of ints and another int value
///
///returs true it the value is contained in the list
///
///else returns false
bool listContainsValue(List<int> list, int value) {
for (var element in list) {
if (element == value) {
print(true);
return true;
}
}
print(false);
return false;
}
///returns the time remaining till the next
///special time takes place as a string: hours:minutes:seconds
///
///if specialTime is currently active
///it returns an empty string
///
///specialTimes have to be given by entering an int list with all special
///weekdays and entering the startingHour and the endingHour
timeRemaining(List<int> weekdays, int startingHour, int endingHour) {
DateTime now = DateTime.now();
bool timeIsSpecial = false;
for (var specificWeekday in weekdays) {
if (now.weekday == specificWeekday &&
(now.hour >= startingHour && now.hour < endingHour)) {
timeIsSpecial = true;
}
}
print(timeIsSpecial);
if (!timeIsSpecial) {
DateTime nextSpecialTime =
DateTime(now.year, now.month, now.day, startingHour);
while (nextSpecialTime.isBefore(now) ||
!listContainsValue(weekdays, nextSpecialTime.weekday)) {
nextSpecialTime = nextSpecialTime.add(Duration(days: 1));
}
return remainingTimeToText(nextSpecialTime.difference(now));
} else {
return "";
}
}
@override
void initState() {
Timer.periodic(Duration(seconds: 1), (timer) {
if (mounted == true) {
setState(() {
timeLeft = timeRemaining([4, 5, 6], 18, 22);
});
}
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Container(
child: Center(
child: Text(
timeLeft,
style: TextStyle(
color: Colors.black,
fontSize: 40,
),
),
),
),
);
}
}
在周四、周五、周六、周日下午 6 点至晚上 10 点,应用允许用户某些权限。
我想显示从任何日期到每个星期四至星期日(下午 6 点至晚上 10 点)这段时间的倒计时
例如,如果是星期一,计时器将倒计时到星期四下午 6 点。如果是星期五早上 8 点,则倒计时到星期五下午 6 点。在任何时候,计时器都应该显示一次计时。
如果在周四至周日(下午 6 点至晚上 10 点)内,计时器将消失。仅在周四至周日再次出现 22:00:01。
我该如何实现?此外,对于日期列表,是否有一种方法可以确保每个星期四 - 星期日(下午 6 点)倒计时,而不是像看到的那样对日期进行硬编码。没有使用包。我被困在下面的代码中。
以下是我截断的代码:
List<DateTime> date =
[DateTime.parse("2021-08-26 18:00:00Z"),
DateTime.parse("2021-08-27 18:00:00Z"),
DateTime.parse("2021-08-28 18:00:00Z"),
DateTime.parse("2021-08-29 18:00:00Z"),];
@override
Widget build(BuildContext context){
return Scaffold(
body: Center(child: ListView.builder(
itemCount: date.length,
itemBuilder: (context, index){
countTime = CountDown().timeLeft(date[0], 'Completed');
return Text(
countTime,
style(TextStyle(fontSize:25.0));
...
感谢您的帮助。
编辑如下:
编辑以显示倒计时中增加的天数 小部件错误。
remainingTimeToText 上的代码被截断
String remainingTimeToText(Duration duration) {
DateTime now = DateTime.now();
int days = duration.inDays;
int hours = duration.inHours - days * 24;
int minutes = duration.inMinutes - hours * 60;
int seconds = duration.inSeconds - hours * 3600 - minutes * 60;
if (now.weekday < 4) {
return days.toString() +
' days ' +
hours.toString() +
":" +
minutes.toString() +
":" +
seconds.toString();
} else if (now.weekday>3){return
hours.toString() +
":" +
minutes.toString() +
":" +
seconds.toString();
}
import 'package:flutter/material.dart';
//needed for using the Timer
import 'dart:async';
class TimerScreen extends StatefulWidget {
const TimerScreen({Key? key}) : super(key: key);
@override
_TimerScreenState createState() => _TimerScreenState();
}
class _TimerScreenState extends State<TimerScreen> {
//the time left till the next special time comes up
String timeLeft = "";
///takes an int when converting it to string adds
/// a 0 in fron when it is smaller than 10
///
///will look nicer when displayed on the screen
String customString(int i) {
if (i < 10) {
return "0" + i.toString();
} else {
return i.toString();
}
}
///converts a duration to a
///string: days hours:minuts:seconds
String remainingTimeToText(Duration duration) {
print(duration);
int days = duration.inDays;
int hours = duration.inHours - days * 24;
int minutes = duration.inMinutes - hours * 60 - days * 24 * 60;
int seconds =
duration.inSeconds - hours * 3600 - minutes * 60 - days * 86400;
if (days > 0) {
String s = " days ";
if (days == 1) {
s = " day ";
}
return days.toString() +
s +
customString(hours) +
":" +
customString(minutes) +
":" +
customString(seconds);
} else {
return customString(hours) +
":" +
customString(minutes) +
":" +
customString(seconds);
}
}
///given a list of ints and another int value
///
///returs true it the value is contained in the list
///
///else returns false
bool listContainsValue(List<int> list, int value) {
for (var element in list) {
if (element == value) {
print(true);
return true;
}
}
print(false);
return false;
}
///returns the time remaining till the next
///special time takes place as a string: hours:minutes:seconds
///
///if specialTime is currently active
///it returns an empty string
///
///specialTimes have to be given by entering an int list with all special
///weekdays and entering the startingHour and the endingHour
timeRemaining(List<int> weekdays, int startingHour, int endingHour) {
DateTime now = DateTime.now();
bool timeIsSpecial = false;
for (var specificWeekday in weekdays) {
if (now.weekday == specificWeekday &&
(now.hour >= startingHour && now.hour < endingHour)) {
timeIsSpecial = true;
}
}
print(timeIsSpecial);
if (!timeIsSpecial) {
DateTime nextSpecialTime =
DateTime(now.year, now.month, now.day, startingHour);
while (nextSpecialTime.isBefore(now) ||
!listContainsValue(weekdays, nextSpecialTime.weekday)) {
nextSpecialTime = nextSpecialTime.add(Duration(days: 1));
}
return remainingTimeToText(nextSpecialTime.difference(now));
} else {
return "";
}
}
@override
void initState() {
Timer.periodic(Duration(seconds: 1), (timer) {
if (mounted == true) {
setState(() {
timeLeft = timeRemaining([4, 5, 6], 18, 22);
});
}
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Container(
child: Center(
child: Text(
timeLeft,
style: TextStyle(
color: Colors.black,
fontSize: 40,
),
),
),
),
);
}
}