gpt4 book ai didi

flutter - Flutter/Dart:无法调用窗口小部件方法

转载 作者:行者123 更新时间:2023-12-03 04:00:07 26 4
gpt4 key购买 nike

我有一个计时器控件,所选的绿色按钮时,它调用打开了一个AlertDialog询问用户,如果他们想阻止他们的锻炼 Activity completeActivity()。如果单击"is",则应调用另一个对话框以评估疼痛程度。问题是根本没有调用第二个对话框,而是仅在第一个对话框之后导航到另一个页面。

如果我将代码行移动到屏幕导航到另一页的后面,并且该方法在同一类中,则只能弹出“疼痛评分”对话框。我需要在单独的类(class)中进行“疼痛评分”对话框。我怀疑这与返回值是一个对话框有关

计时器小部件:

class TimeScreen extends StatefulWidget {
@override
_TimeScreenState createState() => _TimeScreenState();
}

class _TimeScreenState extends State<TimeScreen> {
var duration;

@override
Widget build(BuildContext context) {
var timerService = TimerService.of(context);
return new Container(
padding: EdgeInsets.all(20.0),
child: new Column(
children: <Widget>[
AnimatedBuilder(
animation: timerService,
builder: (context, child) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('${timerService.currentDuration.toString().substring(0,7)}',style: new TextStyle(fontSize: 25.0)),
SizedBox(height: 20.0),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FloatingActionButton(
heroTag: "btn1",
backgroundColor: Colors.red,
onPressed: !timerService.isRunning ? timerService.start : timerService.stop,
child: Icon(!timerService.isRunning ? Icons.play_arrow : Icons.pause)),
SizedBox(width: 20.0),
FloatingActionButton(
heroTag: "btn2",
backgroundColor: Colors.green,
onPressed: () {
timerService.stop();
completeActivity(context, timerService);
},
child: Icon(Icons.check)),
],
)]);
},
),
]),
);
}

completeActivity(BuildContext context, TimerService timerService) {
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: () { User.getCurrentUser().getCurrentActivity().setDuration(timerService.currentDuration);
print("Final Time ${User.getCurrentUser().getCurrentActivity().getDuration()}");
User.getCurrentUser().setCurrentActivity(null);
timerService.reset();
Navigator.push(context, MaterialPageRoute(builder: (context) => FrontPage()));
RatePain();
},
child:
new Text('Yes', style: new TextStyle(fontSize: 18.0)),
),
new FlatButton(
onPressed: () {
Navigator.pop(context);
timerService.start();
}, // this line dismisses the dialog
child: new Text('No', style: new TextStyle(fontSize: 18.0)),
)
],
),
) ??
false;
}

疼痛等级小部件:
class RatePain extends StatefulWidget {
@override
_RatePainState createState() => _RatePainState();
}

class _RatePainState extends State<RatePain> {

@override
Widget build(BuildContext context) {
return showDialog(
context: context,
barrierDismissible: false, // set to false if you want to force a rating
builder: (context) => (
new RatingDialog(
icon: Icon(
Icons.sentiment_satisfied,
color: Colors.grey,
size: 100,
),
title: "How much pain are you in?",
description:
"Tap a star to set your pain rating after this exercise."+
"\n1 = No pain"+
"\n10 = Extreme pain",
submitButton: "SUBMIT",
accentColor: Colors.blueAccent,
onSubmitPressed: (int rating) {
print("onSubmitPressed: rating = $rating");
User.getCurrentUser().getCurrentActivity().getStatistics().setPainRating(rating);
},
)));
}

在第一个对话框上选择"is"后,应该弹出另一个用于评估疼痛程度的弹出窗口。

最佳答案

用户提交评级时,您只需要导航到另一页,即可确保一切完成后导航到另一页。

class RatePain extends StatefulWidget {
@override
_RatePainState createState() => _RatePainState();
}

class _RatePainState extends State<RatePain> {

@override
Widget build(BuildContext context) {
return showDialog(
context: context,
barrierDismissible: false, // set to false if you want to force a rating
builder: (context) => (
new RatingDialog(
icon: Icon(
Icons.sentiment_satisfied,
color: Colors.grey,
size: 100,
),
title: "How much pain are you in?",
description:
"Tap a star to set your pain rating after this exercise."+
"\n1 = No pain"+
"\n10 = Extreme pain",
submitButton: "SUBMIT",
accentColor: Colors.blueAccent,
onSubmitPressed: (int rating) {
print("onSubmitPressed: rating = $rating");
User.getCurrentUser().getCurrentActivity().getStatistics().setPainRating(rating);
Navigator.push(context, MaterialPageRoute(builder: (context) => FrontPage()));
},
)));
}

关于flutter - Flutter/Dart:无法调用窗口小部件方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57932331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com