【问题标题】:How to access the text in the textspan under Flexible - flutter integration tests如何在Flexible-flutter集成测试下访问textspan中的文本
【发布时间】:2022-01-20 19:58:27
【问题描述】:

我正在尝试从下面的小部件中获取文本(卡路里)。


return Column(
      children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Flexible(
              child: AutoSizeText.rich(
                TextSpan(
                  children: <TextSpan>[
                    TextSpan(
                      text: NumberFormat.decimalPattern(locale).format(
                        calories!.round(),
                      ),
                    ),
                    // reduce spacing between amount and unit as defined in design
                    TextSpan(text: ' ', style: TextStyle(letterSpacing: -5.0)),
                    TextSpan(
                      text: _translate(AppLocalization.Caloriesunitkcal),
                      style: Theme.of(context)
                          .textTheme
                          .bodyText1!
                          .copyWith(color: caloriesTextColor),
                    )
                  ],
                ),
                maxLines: 1,
                minFontSize: AdTheme.smallestUsedTextSize,
                group: _group1,
                style: Theme.of(context)
                    .textTheme
                    .headline3!
                    .copyWith(color: caloriesTextColor),
              ),
            ),
          ],
        ),
        AutoSizeText(
          description!,
          maxLines: 1,
          textAlign: TextAlign.center,
          minFontSize: AdTheme.smallestUsedTextSize,
          group: _group2,
          style: Theme.of(context).textTheme.bodyText1,
        )
      ],
    );
  }

我尝试获取 textspan 的解决方案如下:


 var firstCell = find
          .descendant(
        of: find.byType(Flexible),
        matching: find.byType(TextSpan),
      )
          .evaluate()
          .whereType<Text>()
          .first;
      print(firstCell.data);


但是这个解决方案不允许我获取这个文本跨度中的文本。

如果有人对此有一些见解并曾处理过类似类型的数据,请提供帮助。

【问题讨论】:

    标签: flutter dart testing text textspan


    【解决方案1】:

    您的小部件应该对 UI 状态做出反应,我建议您将这些变量放在小部件本身之外,如果您在小部件之外拥有 UIModel,则声明式 UI(例如 Flutter、Compose 或 SwiftUI)的目标是您不需要需要直接从小部件访问数据,因为您使用 UI 模型在屏幕上准确绘制您想要的内容,所以我会考虑这样的事情:

    var calories = NumberFormat.decimalPattern(locale).format(calories!.round());
    
    return Column(
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Flexible(
                  child: AutoSizeText.rich(
                    TextSpan(
                      children: <TextSpan>[
                        TextSpan(
                          text: caloriesText,
                          ),
                        ),
                        // reduce spacing between amount and unit as defined in design
                        TextSpan(text: ' ', style: TextStyle(letterSpacing: -5.0)),
                        TextSpan(
                          text: _translate(AppLocalization.Caloriesunitkcal),
                          style: Theme.of(context)
                              .textTheme
                              .bodyText1!
                              .copyWith(color: caloriesTextColor),
                        )
                      ],
                    ),
                    maxLines: 1,
                    minFontSize: AdTheme.smallestUsedTextSize,
                    group: _group1,
                    style: Theme.of(context)
                        .textTheme
                        .headline3!
                        .copyWith(color: caloriesTextColor),
                  ),
                ),
              ],
            ),
            AutoSizeText(
              description!,
              maxLines: 1,
              textAlign: TextAlign.center,
              minFontSize: AdTheme.smallestUsedTextSize,
              group: _group2,
              style: Theme.of(context).textTheme.bodyText1,
            )
          ],
        );
      }
    

    希望有意义

    【讨论】:

    • 感谢您的回复。但是,我的查询更多与集成测试有关,其中我试图从文本跨度中获取文本,而我提到的解决方案不起作用。
    • 你可以给那个特定的小部件添加一个键,然后使用 find.byKey,你试过了吗?
    • 键没用 :(
    猜你喜欢
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    相关资源
    最近更新 更多