发送按钮按下时的进度,而不是滑块更改时。

huangapple go评论91阅读模式
英文:

Send progress on button pressed instead slider change

问题

我正在使用Flutter游戏模板创建一个测试,如果用户点击特定按钮,它会注册正确答案并将用户转到显示得分和祝贺的屏幕。

目前,当我将滑块更改为按钮时,在main.dart中遇到以下问题:

  1. [![在此输入图像描述][1]][1]

我用于游戏会话屏幕的代码:

  1. Consumer<LevelState>(
  2. builder: (context, levelState, child) =>
  3. FilledButton(
  4. onPressed: () =>
  5. GoRouter.of(context).go('/play/won'),
  6. style: ButtonStyle(
  7. backgroundColor: MaterialStateProperty.resolveWith<Color>(
  8. (Set<MaterialState> states) {
  9. if (states.contains(MaterialState.pressed)) return Color(0xFF55A318);
  10. return Color(0XFFE4DBC8);
  11. },
  12. ),
  13. ),
  14. child: Text('Ostrich bias ${widget.level.difficulty}', style: TextStyle(color: Color(0xFF000000))),
  15. ),
  16. )
  17. [![在此输入图像描述][2]][2]

在此输入图像描述

滑块部分:

  1. Text('将滑块拖动到${widget.level.difficulty}%或更高!'),
  2. Consumer<LevelState>(
  3. builder: (context, levelState, child) => Slider(
  4. label: '级别进度',
  5. autofocus: true,
  6. value: levelState.progress / 100,
  7. onChanged: (value) =>
  8. levelState.setProgress((value * 100).round()),
  9. onChangeEnd: (value) => levelState.evaluate(),
  10. ),
  11. ),
英文:

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

  1. Consumer&lt;LevelState&gt;(
  2. builder: (context, levelState, child) =&gt;
  3. FilledButton(
  4. onPressed: () =&gt;
  5. GoRouter.of(context).go(&#39;/play/won&#39;),
  6. style: ButtonStyle(
  7. backgroundColor: MaterialStateProperty.resolveWith&lt;Color&gt;(
  8. (Set&lt;MaterialState&gt; states) {
  9. if (states.contains(MaterialState.pressed)) return Color(0xFF55A318);
  10. return Color(0XFFE4DBC8);
  11. },
  12. ),
  13. ),
  14. child: Text(&#39;Ostrich bias ${widget.level.difficulty}&#39;, style: TextStyle(color: Color(0xFF000000))),
  15. ),

发送按钮按下时的进度,而不是滑块更改时。

enter image description here

Slider part

  1. Text(&#39;Drag the slider to ${widget.level.difficulty}%&#39;
  2. &#39; or above!&#39;),
  3. Consumer&lt;LevelState&gt;(
  4. builder: (context, levelState, child) =&gt; Slider(
  5. label: &#39;Level Progress&#39;,
  6. autofocus: true,
  7. value: levelState.progress / 100,
  8. onChanged: (value) =&gt;
  9. levelState.setProgress((value * 100).round()),
  10. onChangeEnd: (value) =&gt; levelState.evaluate(),
  11. ),
  12. ),

答案1

得分: 1

  1. /won页面的GoRoute定义内部,您尝试访问extra参数:

final map = state.extra as Map&lt;String, dynamic>

但是,请注意,在其中一个按钮中,您导航到该路由时 没有 提供extra参数。涉及到的代码行是:

GoRouter.of(context).go(&#39;/play/won&#39;)

GoRouter导航到该页面并尝试访问extra参数时,您将收到一个崩溃,因为您试图将一个不存在的(也就是null)值强制转换为一个映射(1.)。

  1. 解决方法:

您需要实现处理GoRoute定义中不存在值的逻辑。在您的情况下,您可以首先检查是否可以成功执行强制转换:

if (state.extra is Map&lt;String, dynamic) { /*处理缺少的分数等*/ }

另一种方法是验证只有当用户拥有您需要的数据(例如分数等)时,才能转到/won页面。

一般来说:

  1. FilledButton(
  2. onPressed: () {
  3. /// 验证玩家获胜的游戏条件是否有效。
  4. if (player.hasScore) {
  5. /// 只有在这种情况下,我们才能导航到获胜页面。
  6. GoRouter.of(context).go(&#39;/play/won&#39;, extra: {&quot;score&quot;: score});
  7. /// ^^^^^ 始终传递这个。
  8. }
  9. },
  10. ...
  11. )
英文:

1. Inside the GoRoute definition for the /won page, you attempt to access the extra parameter:

final map = state.extra as Map&lt;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(&#39;/play/won&#39;)

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&lt;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:

  1. FilledButton(
  2. onPressed: () {
  3. /// Verify that the player conditions for winning the game as valid.
  4. if (player.hasScore) {
  5. /// Only then, can we navigate to the win page.
  6. GoRouter.of(context).go(&#39;/play/won&#39;, extra: {&quot;score&quot;: score});
  7. /// ^^^^^ always pass this.
  8. }
  9. },
  10. ...
  11. )

huangapple
  • 本文由 发表于 2023年7月17日 22:01:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定