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

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

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&lt;LevelState&gt;(
                  builder: (context, levelState, child) =&gt;
                  FilledButton(
                    onPressed: ()  =&gt;
                    GoRouter.of(context).go(&#39;/play/won&#39;),
                  style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.resolveWith&lt;Color&gt;(
                  (Set&lt;MaterialState&gt; states) {
                  if (states.contains(MaterialState.pressed)) return Color(0xFF55A318);
                  return  Color(0XFFE4DBC8);
                 }, 
                 ),
            ),
                    
                    child: Text(&#39;Ostrich bias ${widget.level.difficulty}&#39;, style: TextStyle(color: Color(0xFF000000))),
            ),

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

enter image description here

Slider part

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

答案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页面。

一般来说:

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

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:

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(&#39;/play/won&#39;, extra: {&quot;score&quot;: score});
      ///                                  ^^^^^ always pass this.
    }
  },
  ...
)

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:

确定