Dart,如果 else 语句,有没有更好的方法来选择下一个地图项目?



我是Dart和编程的新手,我正在尝试制作我的第一个应用程序! 我在屏幕末尾有一个浮动操作按钮,每次按下按钮时,我都希望我的变量根据提供的下一个地图项目进行更改。 我使用了多个 if 语句,但我认为这不是实现结果的最佳方法。 此外,当最终选择最后一个地图项目时,我想过渡到另一个页面。我已经编写了代码,但我确实在 If 语句中犯了错误。

提前谢谢你!

我确信我可以使用计数器 var 以某种方式实现这一目标,但我不知道如何...... 请帮忙!

进口。。。

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() {
setState(() {
if (ex['ex1'] != null) {
ex['ex1'] = ex['ex2'];
}
if (ex['ex2'] != null) {
ex['ex2'] = ex['ex3'];
}
if (ex['ex3'] != null) {
ex['ex3'] = ex['ex4'];
}
if (ex['ex4'] != null) {
ex['ex4'] = ex['ex5'];
}
if (ex['ex5'] != null) {
ex['ex5'] = ex['ex6'];
}
if (ex['ex6'] != null) {
ex['ex6'] = ex['ex7'];
}
if (ex['ex7'] != null) {
ex['ex7'] = ex['ex8'];
}
if (ex['ex8'] != null) {
ex['ex8'] = ex['ex9'];
}
if (ex['ex9'] != null) {
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,
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Ready'),
),
body: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(height: 50.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(bottom: 50.0),
child: Text(
ex['ex1']['title'],
style: TextStyle(
fontSize: 24.0,
color: Colors.deepOrange[700],
),
),
)
],
),
Row(
children: <Widget>[
Expanded(
child: Chewie(
controller: _chewieController,
),
),
],
),
SizedBox(height: 10.0),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(15.0),
child: Text(
ex['ex1']['description1'],
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
color: Colors.grey[200],
),
),
),
],
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(15.0),
child: Text(
ex['ex1']['description2'],
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
color: Colors.grey[200],
),
),
),
],
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
nextExercise();
},
child: Icon(Icons.arrow_forward_ios),
foregroundColor: Colors.grey[200],
backgroundColor: Colors.deepOrange[700],
),
);
}
}

在 Dart 中,您可以通过两种方式打印变量。假设你有名字="约翰"。你可以写:print(name)或者你也可以写print("$name").在第二种样式中,您还可以为其添加前缀或后缀。例 :print("My name is $name.")

因此,要减少代码中的 if 语句,您可以尝试如下操作:

int _curr;
int _next ;
for (_curr = 1 ; _curr <= 8 ; _curr++) {
_next = _curr + 1 ;
if ( ex['ex$_curr'] != null ) {
ex['ex$_curr'] = ex['ex$_next'] ;
}
}

注意:此代码未经单元测试。

最新更新