英文:
Send progress on button pressed instead slider change
问题
我正在使用Flutter游戏模板创建一个测试,如果用户点击特定按钮,它会注册正确答案并将用户转到显示得分和祝贺的屏幕。
目前,当我将滑块更改为按钮时,在main.dart中遇到以下问题:
[![在此输入图像描述][1]][1]
我用于游戏会话屏幕的代码:
Consumer<LevelState>(
builder: (context, levelState, child) =>
FilledButton(
onPressed: () =>
GoRouter.of(context).go('/play/won'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed)) return Color(0xFF55A318);
return Color(0XFFE4DBC8);
},
),
),
child: Text('Ostrich bias ${widget.level.difficulty}', style: TextStyle(color: Color(0xFF000000))),
),
)
[![在此输入图像描述][2]][2]
滑块部分:
Text('将滑块拖动到${widget.level.difficulty}%或更高!'),
Consumer<LevelState>(
builder: (context, levelState, child) => Slider(
label: '级别进度',
autofocus: true,
value: levelState.progress / 100,
onChanged: (value) =>
levelState.setProgress((value * 100).round()),
onChangeEnd: (value) => levelState.evaluate(),
),
),
英文:
I am using flutter game template to create a test, where if user clicks on specific button, it register the right answer and forward the user to the screen with the score and congratulations.
For now, when I change slider to button, I face this issue in main.dart:
My code for play session screen
Consumer<LevelState>(
builder: (context, levelState, child) =>
FilledButton(
onPressed: () =>
GoRouter.of(context).go('/play/won'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed)) return Color(0xFF55A318);
return Color(0XFFE4DBC8);
},
),
),
child: Text('Ostrich bias ${widget.level.difficulty}', style: TextStyle(color: Color(0xFF000000))),
),
Slider part
Text('Drag the slider to ${widget.level.difficulty}%'
' or above!'),
Consumer<LevelState>(
builder: (context, levelState, child) => Slider(
label: 'Level Progress',
autofocus: true,
value: levelState.progress / 100,
onChanged: (value) =>
levelState.setProgress((value * 100).round()),
onChangeEnd: (value) => levelState.evaluate(),
),
),
答案1
得分: 1
- 在
/won
页面的GoRoute
定义内部,您尝试访问extra
参数:
final map = state.extra as Map<String, dynamic>
。
但是,请注意,在其中一个按钮中,您导航到该路由时 没有 提供extra
参数。涉及到的代码行是:
GoRouter.of(context).go('/play/won')
。
当GoRouter
导航到该页面并尝试访问extra
参数时,您将收到一个崩溃,因为您试图将一个不存在的(也就是null
)值强制转换为一个映射(1.)。
- 解决方法:
您需要实现处理GoRoute
定义中不存在值的逻辑。在您的情况下,您可以首先检查是否可以成功执行强制转换:
if (state.extra is Map<String, dynamic) { /*处理缺少的分数等*/ }
。
另一种方法是验证只有当用户拥有您需要的数据(例如分数等)时,才能转到/won
页面。
一般来说:
FilledButton(
onPressed: () {
/// 验证玩家获胜的游戏条件是否有效。
if (player.hasScore) {
/// 只有在这种情况下,我们才能导航到获胜页面。
GoRouter.of(context).go('/play/won', extra: {"score": score});
/// ^^^^^ 始终传递这个。
}
},
...
)
英文:
1. Inside the GoRoute
definition for the /won
page, you attempt to access the extra
parameter:
final map = state.extra as Map<String, dynamic
.
However, note that in one of your buttons, you navigate to that route without providing an extra
parameter. The line in question is:
GoRouter.of(context).go('/play/won')
When GoRouter
navigates to the page, and attempts to access the extra
parameter, you will receive a crash because you are attempting to cast a non-existent (AKA null
) value to a map (1.).
2. Solution:
You will have to implement logic for handling a non-existent value in your GoRoute
definition. In your scenario, you can first check if the cast can succeed:
if (state.extra is Map<String, dynamic) { /*handle missing score, etc.*/ }
Another approach would be to validate that the user can only go to the /won
page if they have the data that you need (a score, etc.).
In general terms:
FilledButton(
onPressed: () {
/// Verify that the player conditions for winning the game as valid.
if (player.hasScore) {
/// Only then, can we navigate to the win page.
GoRouter.of(context).go('/play/won', extra: {"score": score});
/// ^^^^^ always pass this.
}
},
...
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论