上次按下浮动操作按钮时更改屏幕

时间:2019-08-09 16:58:56

标签: flutter dart

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

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

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

}

1 个答案:

答案 0 :(得分:0)

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

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

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具有(主要)构建方法中的上下文。