我有一个stopwatch
窗口小部件,该窗口小部件从页面上的Detailed Activity
页面调用,该窗口在页面加载时开始。如何使它在后台连续运行,以便当我从Detailed Activity
导航到Home
页面时,可以显示同一秒表的时间?
此刻,Home
页显示上次保存的时间(到变量中)。取而代之的是,我希望这能继续每秒增加两个屏幕之间的切换,反之亦然。我尝试在startWatch()
页面中添加initState()
和Home
方法,但不知道在导航至Detailed Activity
页面时如何传递秒表值>
为进一步说明这一点,我的想法是在Detailed Activity
页上,例如00:15:00->将其传递到Home
页并调用initState()以再次自动启动计时器。这行得通吗?
Timer.dart
class NewStopWatch extends StatefulWidget {
@override
_NewStopWatchState createState() => new _NewStopWatchState();
}
class _NewStopWatchState extends State<NewStopWatch> {
static _NewStopWatchState stopwatch;
Stopwatch watch = new Stopwatch();
Timer timer;
bool startStop = true;
static String elapsedTime = '';
String duration;
updateTime(Timer timer) {
if (watch.isRunning) {
setState(() {
elapsedTime = transformMilliSeconds(watch.elapsedMilliseconds);
User.getCurrentUser().getCurrentActivity().setElapsedTime(elapsedTime);
});
}
}
@override
Widget build(BuildContext context) {
return new Container(
padding: EdgeInsets.all(20.0),
child: new Column(
children: <Widget>[
new Text(elapsedTime, style: new TextStyle(fontSize: 25.0)),
SizedBox(height: 20.0),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FloatingActionButton(
heroTag: "btn1",
backgroundColor: Colors.red,
onPressed: () => startOrStop(),
child: Icon(Icons.pause)),
SizedBox(width: 20.0),
FloatingActionButton(
heroTag: "btn2",
backgroundColor: Colors.green,
onPressed: () => completeActivity(),
child: Icon(Icons.check)),
],
)
],
));
}
void initState() {
super.initState();
WidgetsBinding.instance
.addPostFrameCallback((_) => startWatch());
}
startOrStop() {
if(startStop) {
startWatch();
} else {
stopWatch();
}
}
startWatch() {
setState(() {
startStop = false;
watch.start();
timer = Timer.periodic(Duration(milliseconds: 100), updateTime);
});
}
stopWatch() {
setState(() {
startStop = true;
watch.stop();
setTime();
});
}
resetWatch() {
watch.reset();
setTime();
}
setTime() {
var timeSoFar = watch.elapsedMilliseconds;
setState(() {
elapsedTime = transformMilliSeconds(timeSoFar);
});
}
completeActivity() { //do I call activity.stopActivity()?
return showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('Complete Activity?',
style: new TextStyle(color: Colors.black, fontSize: 20.0)),
actions: <Widget>[
new FlatButton(
onPressed: () {
duration = elapsedTime;
print("Current activitiy: ${User.getCurrentUser().getCurrentActivity()}");
// User.getCurrentUser().completeActivity();
User.getCurrentUser().addPastActivity(User.getCurrentUser().getCurrentActivity());
User.getCurrentUser().getCurrentActivity().setStatus(ActivityStatus.completed);
User.getCurrentUser().setCurrentActivity(null);
Navigator.push(context, MaterialPageRoute(builder: (context) => FrontPage()));
// Navigator.popUntil(context, ModalRoute.withName("/"),);
},
child:
new Text('Yes', style: new TextStyle(fontSize: 18.0)),
),
new FlatButton(
onPressed: () => Navigator.pop(context), // this line dismisses the dialog
child: new Text('No', style: new TextStyle(fontSize: 18.0)),
)
],
),
) ??
false;
}
transformMilliSeconds(int milliseconds) {
int hundreds = (milliseconds / 10).truncate();
int seconds = (hundreds / 100).truncate();
int minutes = (seconds / 60).truncate();
int hours = (minutes / 60).truncate();
String hoursStr = (hours % 60).toString().padLeft(2, '0');
String minutesStr = (minutes % 60).toString().padLeft(2, '0');
String secondsStr = (seconds % 60).toString().padLeft(2, '0');
return "$hoursStr:$minutesStr:$secondsStr";
}
static String getElapsedTime() {
return elapsedTime;
}
}
答案 0 :(得分:0)
由于您已经发布了很多代码,因此对于此解决方案是否可行,我尚不清楚100%,但是它缺少您实际显示NewStopWatch的部分,但我希望此解决方案有帮助:
在您的NewStopWath类的init方法中将秒表和计时器作为参数传递,并从您的主页(也希望它们显示在该页面上)传递它们……我想它是FrontPage。
像这样:
import tkinter as tk
# colors for certain characters
colormap = {'b':'blue', 'c':'cyan', 'g':'green', 'm':'magenta', 'o':'orange', 'p':'pink', 'r':'red', 'y':'yellow'}
def on_key(event):
if event.char in colormap:
# attach a tag (the character itself) if the character is in the colormap
event.widget.tag_add(event.char, 'insert-1c')
root = tk.Tk()
text = tk.Text(root)
text.pack()
text.bind('<KeyRelease>', on_key) # on_key() will be executed when a key is pressed inside Text widget
# setup colors for certain characters using tag_config(...)
for c in colormap:
text.tag_config(c, foreground=colormap[c])
root.mainloop()
请确保检查是否应删除timer = Timer.periodic(Duration(毫秒:100),updateTime);从startWatch() 也许您不需要将它作为参数传递