简体   繁体   中英

Flutter/Dart: Unable to call widget method

I have a timer widget, that when the green button is selected, it calls completeActivity() which opens up an AlertDialog asking the user if they wish to stop their exercise activity. If "yes" is clicked it should call another Dialog for rating pain. This issue is that the 2nd dialog isn't being called at all, and only navigates to another page after the first dialog.

I can only get the Pain Rating dialog to popup if I move the line of code after where the screen navigates to another page AND if that method is in the same class. I am needing the Pain Rating dialog in a separate class. I suspect it is something to do with the return values being a dialog

timer widget:

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

pain rating widget:

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

After "yes" on the first dialog is selected, another popup for rating pain should popup.

You just need to navigate to another page when a user submits the ratings in that way you make sure to navigate to another page after everything has finished.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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