繁体   English   中英

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

[英]Flutter/Dart: Unable to call widget method

我有一个计时器小部件,当选择绿色按钮时,它将调用completeActivity() ,这将打开一个AlertDialog,询问用户是否希望停止其锻炼活动。 如果单击“是”,则应调用另一个对话框以评估疼痛程度。 问题是根本没有调用第二个对话框,而是仅在第一个对话框之后导航到另一个页面。

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

计时器小部件:

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);
        },
    )));
  }

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

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

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()));
        },
    )));
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM