RenderFlex 溢出错误仅出现在小部件测试中,如果我运行应用程序,一切正常

2024-02-28

可以在这里找到一个最小的可重现示例:https://github.com/HerrNiklasRaab/repro-widget-test-overflow https://github.com/HerrNiklasRaab/repro-widget-test-overflow

我当前的应用程序如下所示:

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
      home: Scaffold(
    body: DashboardNewsItem(),
  )));
}

class DashboardNewsItem extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.green,
      width: 165,
      height: 100,
      child: Row(
        children: <Widget>[
          Text(
            "Zu Instagram",
          ),
          Icon(Icons.arrow_forward)
        ],
      ),
    );
  }
}

If I run this on the device it looks like the following:

一旦我使用以下小部件测试运行此命令:

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:test_ble/main.dart';

void main() {
  testWidgets('Counter increments smoke test', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(
        home: Scaffold(
      body: DashboardNewsItem(),
    )));
  });
}

我得到这个异常:

Counter increments smoke test:

══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
The following assertion was thrown during layout:
A RenderFlex overflowed by 27 pixels on the right.

The relevant error-causing widget was:
  Row file:///Users/niklasraab/GitHub/test_ble/lib/main.dart:19:14

The overflowing RenderFlex has an orientation of Axis.horizontal.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and
black striped pattern. This is usually caused by the contents being too big for the RenderFlex.
Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the
RenderFlex to fit within the available space instead of being sized to their natural size.
This is considered an error condition because it indicates that there is content that cannot be
seen. If the content is legitimately bigger than the available space, consider clipping it with a
ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex,
like a ListView.
The specific RenderFlex in question is: RenderFlex#abc37 OVERFLOWING:
  creator: Row ← ColoredBox ← ConstrainedBox ← Container ← DashboardNewsItem ← _BodyBuilder ←
    MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ← AnimatedBuilder ←
    DefaultTextStyle ← AnimatedDefaultTextStyle ← ⋯
  parentData: <none> (can use size)
  constraints: BoxConstraints(w=165.0, h=100.0)
  size: Size(165.0, 100.0)
  direction: horizontal
  mainAxisAlignment: start
  mainAxisSize: max
  crossAxisAlignment: center
  textDirection: ltr
  verticalDirection: down
◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤
════════════════════════════════════════════════════════════════════════════════════════════════════
ERROR: Test failed. See exception logs above.
The test description was: Counter increments smoke test

当然,我可以将文本包装在灵活的内部,如下所示:

class DashboardNewsItem extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.green,
      width: 165,
      height: 100,
      child: Row(
        children: <Widget>[
          Flexible(
            child: Text(
              "Zu Instagram",
            ),
          ),
          Icon(Icons.arrow_forward)
        ],
      ),
    );
  }
}

但我不需要这样做,因为文本在水平轴上有足够的空间。那么有人可以向我解释这种有线行为吗?


如果直接在模拟器上运行测试,它就会通过。

flutter run -d emulator test\widget_test.dart

我亲自直接在我的设备上运行并测试通过。

问题是它不会在所有设备上传递。在某些设备上,165 个逻辑像素的宽度可能不足以包含TextIcon。对于 Flutter 提供的默认测试环境来说可能就是这样。一般来说,让您的小部件尽可能具有响应能力是个好主意。因此,更好的实现是删除width and height约束条件,约束的大小Row相反,并使用padding.

class DashboardNewsItem extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.green,
      padding: const EdgeInsets.symmetric(
          horizontal: 24.0, vertical: 16.0), // or any other value
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text(
            "Zu Instagram",
          ),
          Icon(Icons.arrow_forward)
        ],
      ),
    );
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

RenderFlex 溢出错误仅出现在小部件测试中,如果我运行应用程序,一切正常 的相关文章

随机推荐

  • SkipList 与 Dictionary

    我最近一直在阅读有关跳过列表的内容 我有一个 Web 应用程序 它对静态数据集执行相当复杂的 Sql 查询 我想实现一个缓存系统 生成 sql 查询的 md5 哈希值 然后返回查询的缓存数据集 如果集合中存在该数据集 哪种算法更好 字典还是
  • 如何在android中列表视图为空时显示消息

    我是 Android 方面的新手 我正在创建一个应用程序 其中包含将动态填充的列表视图 我的要求是当列表为空时 我想显示一条消息 我不想仅仅为了显示此消息而创建其他视图 有什么好的方法可以做到这一点吗 有什么建议么 ListActivity
  • 语法错误:插入“enum Identifier”,插入“EnumBody”,插入“}”

    我编写了一个枚举类型 当我运行为其创建的 JUnit 测试时 该类型会出现以下语法错误 java lang Error Unresolved compilation problems Syntax error insert enum Ide
  • 需要 python lxml 语法帮助来解析 html

    我是 python 的新手 我需要一些有关使用 lxml 查找和迭代 html 标签的语法的帮助 以下是我正在处理的用例 HTML 文件的格式相当好 但并不完美 屏幕上有多个表格 其中一个包含一组搜索结果 每个表格包含页眉和页脚 每个结果行
  • 每天第一次调用网络服务很慢

    在构建此 Web 服务和调用它的应用程序时 我们注意到每天对该 Web 服务的第一次调用非常慢 有时甚至会超时 然而 此后的每一次通话都效果很好 有人能解释一下为什么会这样以及我们如何摆脱这种痛苦吗 提前致谢 如果是 ASP NET Web
  • 更改seaborn箱线图线彩虹颜色

    I found this beautiful graph online apparently made with plotly and wanted to recreate it with seaborn 到目前为止 这是我的代码 impo
  • AngularJS 实现模板本地化

    我想实现视图的本地化 也应该包括正文 我之前通过加载 JSON 文件并通过键进行迭代来完成此操作 键是类名 比我简单地将键的值分配给 元素与类 语言文件 JSON Header Title My Title Header Text Lore
  • Jersey 2 + HK2 - @ApplicationScoped 不工作

    我有课 ApplicationScoped public class Service private Map
  • (bool) 可靠地转换为 0 或 1 吗? [复制]

    这个问题在这里已经有答案了 来自一些reading https stackoverflow com questions 6627178 c99 why are false and true defined as 0 and 1 and no
  • iOS 中可用的路径目录

    NSSearchPathDirectory 这些常量指定各种目录的位置 enum NSApplicationDirectory 1 NSDemoApplicationDirectory NSDeveloperApplicationDirec
  • 将Angular2项目集成到Tomcat服务器中

    我为我的项目开发了一个 Spring maven Rest api 对于客户端 我使用 Angular2 和 typescript 作为 Angular 的新手 参考 Angular 网站进行开发 使用 npm 和 lite server
  • C# Winform 网格渲染在 Windows 7 上缓慢

    我注意到 C winform datagrid 在我的 windows 7 64 位机器上非常慢 对于具有 1000 行 足够的列 文本以适合屏幕宽度的标准网格 我看到滚动时出现明显的渲染延迟 即滚动 滚动条移动滞后约 0 5 秒而不是平滑
  • 当超过 6 个参数时 Observable.forkJoin 返回错误类型

    我遇到 Observable forkJoin 的问题 它推断出错误的返回类型 然后在传递超过 6 个参数时导致错误 Observable forkJoin service getType1 service getType2 service
  • 如何在 shell 脚本中即时解释变量?

    我正在使用 JQ 在 shell 脚本中读取 JSON 在这里 我无法动态解释 shell 脚本中的变量 HOME HOST PEMFILE JSON 文件 script install HOME lib install sh HOST P
  • 新的默认VB.NET项目立即报错

    我刚刚在 Mac 上安装了 Mono 版本 2 10 8 和 MonoDevelop 2 8 6 5 当我创建一个新项目 文件 gt 新解决方案 gt VBNet gt ASP NET gt Web 应用程序 时 创建后出现错误 尝试加载项
  • C 函数 fwrite() 不写入文件

    我正在尝试编写结构tempGroupFile into GroupFile fwrite 写入时返回1 但实际上文件中没有写入数据GroupFile 功能printRec 在屏幕上打印出结构 data是结构变量 文件GroupFile这些操
  • Android 上的 HTML5

    根据 http developer android com sdk android 2 0 highlights html http developer android com sdk android 2 0 highlights html
  • 如何在关闭钩子中获取返回码

    我需要根据我的应用程序结果修改JVM返回代码 但显式调用 System exit code 是有风险的 因为应用程序很复杂 并且很难识别正在运行的线程的结束 所以我想出了在 JVM 退出之前使用 shutdown hook 来修改返回代码
  • 无法将 tomsfastmath 链接到 libtomcrypt

    我正在用 c 编写一个安全的即时消息程序 使用 libtomcrypt C 库来实现 RSA 和 SPRNG 函数 我将 libtomcrypt 编译为静态库 并且能够链接到它并运行 sprng 函数并查看和使用它生成的随机数据 我遇到的问
  • RenderFlex 溢出错误仅出现在小部件测试中,如果我运行应用程序,一切正常

    可以在这里找到一个最小的可重现示例 https github com HerrNiklasRaab repro widget test overflow https github com HerrNiklasRaab repro widge