簡體   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