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

标签 flutter dart

可在此处找到最小的可重现示例: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)
        ],
      ),
    );
  }
}

如果我在设备上运行它,它看起来如下所示: enter image description here

一旦我使用以下小部件测试运行它:

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

当然我可以像这样将 Text 包裹在 Flexible 中:

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 提供的默认测试环境来说,这可能是正确的。通常让您的小部件尽可能响应是个好主意。因此,更好的实现是删除 widthheight 约束,改为约束 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)
        ],
      ),
    );
  }
}

关于flutter - RenderFlex 溢出错误只出现在小部件测试中,如果我运行应用程序一切正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61305860/

相关文章:

charts - 格式化Charts_flutter时间序列图中的时间标签以包括hh:mm:ss

flutter - 具有 3 个值的 Flutter 中的多个复选框

api - 取回不记名 token postman flutter 腾

flutter - 如何在flutter中的另一个tabview中制作tabview?

flutter - 如何删除为endDrawer自动添加的appbar中的尾随汉堡包

react-native - Flutter apk/ipa 大小 vs React Native apk/ipa 大小

dart - 保留或恢复页面状态的抽屉导航

php - Dart list 和PhP

typescript - 从 typescript 到 Dart 书写转译器时,人们面临哪些技术障碍?

android-studio - 当尝试 flutter 的 Widget 时,抛出 NoSuchMethodError 说 The method '_debugTypesAreRight' was called on null