上次按下浮动操作按钮时切换屏幕 [英] Changing Screen when Floating Action Button is pressed for the last time

查看:40
本文介绍了上次按下浮动操作按钮时切换屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每按一次浮动操作按钮,就会调用nextExercise().在那里,我设法更新了Ex地图并进行了下一张地图. 从"ex1"到"ex2"等 当我到达"ex9"(最后一页)时,我想更改屏幕,因为练习已经完成.

Each time the Floating Action Button is pressed the nextExercise() is called. There I managed to update the Ex Map and go the next one. Starting from 'ex1' to 'ex2' etc. When I reach the 'ex9' (last page) I would like to change a screen because the exercises will have been completed.

我尝试了很多事情.还尝试了答案中的建议,但找不到解决方案,请帮忙! 下面的新代码:

I tried a lot of things. Also tried the suggestions from the answers but I can not find the solution, please help! New Code below:

class DynamicWorkoutStart extends StatefulWidget {
  @override
  _DynamicWorkoutStartState createState() => _DynamicWorkoutStartState();
}

class _DynamicWorkoutStartState extends State<DynamicWorkoutStart> {
  VideoPlayerController _videoPlayerController1;

  ChewieController _chewieController;

  var ex = {
    'ex1': {
      'title': 'HIGH-KNEE SKIP',
      'videoNr': '1',
      'description1': '- Heel should not touch the ground',
      'description2': ''
    },
    'ex2': {
      'title': 'OVER-UNDERS',
      'videoNr': '2',
      'description1': '- Flip your Hips!',
      'description2': ''
    },
    'ex3': {
      'title': 'WALKING HAMSTRING',
      'videoNr': '3',
      'description1': '- Point your Toe upwards the Head.',
      'description2': '- Keep you back flat!'
    },
    'ex4': {
      'title': 'QUAD STRETCH WITH LEAN',
      'videoNr': '4',
      'description1': '- Keep your Abs tight.',
      'description2': ''
    },
    'ex5': {
      'title': 'FRANKENSTEIN KICKS',
      'videoNr': '5',
      'description1': '- Keep your Knee straight.',
      'description2': ''
    },
    'ex6': {
      'title': 'ADDUCTOR STRETCH',
      'videoNr': '6',
      'description1': '- Keep your back straight.',
      'description2': ''
    },
    'ex7': {
      'title': 'HIPFLEXOR STRETCH',
      'videoNr': '7',
      'description1': '- Rotate towrds lead leg.',
      'description2': '- Keep your Hips straight.'
    },
    'ex8': {
      'title': 'HIGH SKIP INTO DEEP SQUAT',
      'videoNr': '8',
      'description1': '- 3 high Skips and then Deep Squat.',
      'description2': '- Get your food over the fence.'
    },
    'ex9': {
      'title': 'QUICKLINE INTO STICK',
      'videoNr': '9',
      'description1': '- Go over the line as fast as you can!',
      'description2': '- 30sec x 3 sets per leg.'
    },
  };

  @override
  void initState() {
    super.initState();
    _videoPlayerController1 = VideoPlayerController.asset(
        'assets/videos/${ex['ex1']['videoNr']}.m4v');

    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController1,
      aspectRatio: 16 / 9,
      showControls: false,
      autoPlay: true,
      looping: true,
    );
  }

  @override
  void dispose() {
    _videoPlayerController1.dispose();
    _chewieController.dispose();
    super.dispose();
  }

  nextExercise(BuildContext context) {
int _curr;
int _next;
setState(() {
  for (_curr = 1; _curr <= 8; _curr++) {
    _next = _curr + 1;

    if (ex['ex$_curr'] != null) {
      ex['ex$_curr'] = ex['ex$_next'];
    }
  }

  if (_curr >= 9) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => FinishDynamicWorkout(),
      ),
    );
  }

  _chewieController.dispose();

  _chewieController = ChewieController(
    videoPlayerController: _videoPlayerController1 =
        VideoPlayerController.asset(
            'assets/videos/${ex['ex1']['videoNr']}.m4v'),
    aspectRatio: 16 / 9,
    showControls: false,
    autoPlay: true,
    looping: true,
  );
});

}

推荐答案

setState()用于设置最终更新小部件的屏幕状态.您必须在setState()外部进行屏幕导航.

setState() is used to set the screen state which ultimately updates the widget. You'll have to take out the screen navigation outside setState().

此外,nextExcercise()没有上下文".您必须通过Widget构建方法传递上下文. 页面中的示例:

Besides, nextExcercise() doesn't have 'context'. You'll have to pass the context from the Widget build method. Example from this page :

class FirstRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Route'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Open route'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondRoute()),
            );
          },
        ),
      ),
    );
  }
}

在这里,Navigator.push具有(主要)构建方法中的上下文.

Over here, the Navigator.push has the context from the (main) build method.

这篇关于上次按下浮动操作按钮时切换屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆