颤动:保持时间
Flutter: Keeping Time
尝试显示一个静态计时器,它根据当前东部时间每周重置一次,例如屏幕显示一个计时器,Days : Hours : Minutes : seconds
(示例:7: 00 : 00 : 00
),倒计时并在每个星期天结束或星期一开始。我有一个大致的概念,它涉及 DateTime
、Timer
和 Stopwatch
,但很难将它们放在一起。
创建一个 StatefulWidget
,它在您的 Timer
更新时获取并存储当前时间。在build
方法中,计算并格式化时间差:
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Week Countdown',
home: TestPage(),
);
}
}
class TestPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Week Countdown')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
'Time until next week:',
style: Theme.of(context).textTheme.headline,
),
WeekCountdown()
],
),
),
);
}
}
class WeekCountdown extends StatefulWidget {
@override
State<StatefulWidget> createState() => _WeekCountdownState();
}
class _WeekCountdownState extends State<WeekCountdown> {
Timer _timer;
DateTime _currentTime;
@override
void initState() {
super.initState();
_currentTime = DateTime.now();
_timer = Timer.periodic(Duration(seconds: 1), _onTimeChange);
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
void _onTimeChange(Timer timer) {
setState(() {
_currentTime = DateTime.now();
});
}
@override
Widget build(BuildContext context) {
final startOfNextWeek = calculateStartOfNextWeek(_currentTime);
final remaining = startOfNextWeek.difference(_currentTime);
final days = remaining.inDays;
final hours = remaining.inHours - remaining.inDays * 24;
final minutes = remaining.inMinutes - remaining.inHours * 60;
final seconds = remaining.inSeconds - remaining.inMinutes * 60;
final formattedRemaining = '$days : $hours : $minutes : $seconds';
return Text(formattedRemaining);
}
}
DateTime calculateStartOfNextWeek(DateTime time) {
final daysUntilNextWeek = 8 - time.weekday;
return DateTime(time.year, time.month, time.day + daysUntilNextWeek);
}
尝试显示一个静态计时器,它根据当前东部时间每周重置一次,例如屏幕显示一个计时器,Days : Hours : Minutes : seconds
(示例:7: 00 : 00 : 00
),倒计时并在每个星期天结束或星期一开始。我有一个大致的概念,它涉及 DateTime
、Timer
和 Stopwatch
,但很难将它们放在一起。
创建一个 StatefulWidget
,它在您的 Timer
更新时获取并存储当前时间。在build
方法中,计算并格式化时间差:
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Week Countdown',
home: TestPage(),
);
}
}
class TestPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Week Countdown')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
'Time until next week:',
style: Theme.of(context).textTheme.headline,
),
WeekCountdown()
],
),
),
);
}
}
class WeekCountdown extends StatefulWidget {
@override
State<StatefulWidget> createState() => _WeekCountdownState();
}
class _WeekCountdownState extends State<WeekCountdown> {
Timer _timer;
DateTime _currentTime;
@override
void initState() {
super.initState();
_currentTime = DateTime.now();
_timer = Timer.periodic(Duration(seconds: 1), _onTimeChange);
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
void _onTimeChange(Timer timer) {
setState(() {
_currentTime = DateTime.now();
});
}
@override
Widget build(BuildContext context) {
final startOfNextWeek = calculateStartOfNextWeek(_currentTime);
final remaining = startOfNextWeek.difference(_currentTime);
final days = remaining.inDays;
final hours = remaining.inHours - remaining.inDays * 24;
final minutes = remaining.inMinutes - remaining.inHours * 60;
final seconds = remaining.inSeconds - remaining.inMinutes * 60;
final formattedRemaining = '$days : $hours : $minutes : $seconds';
return Text(formattedRemaining);
}
}
DateTime calculateStartOfNextWeek(DateTime time) {
final daysUntilNextWeek = 8 - time.weekday;
return DateTime(time.year, time.month, time.day + daysUntilNextWeek);
}