可以在这里找到一个最小的可重现示例: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)
],
),
);
}
}
但我不需要这样做,因为文本在水平轴上有足够的空间。那么有人可以向我解释这种有线行为吗?