示例工程 ch04-layout
ch04-layout/test/layout_test.dart
import 'package:ch04_layout/main.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
/// 把测试窗口设成指定逻辑尺寸。
void _setSize(WidgetTester tester, Size size) {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
}
void main() {
group('响应式布局', () {
testWidgets('宽屏(1280)用 Row 两栏', (WidgetTester tester) async {
_setSize(tester, const Size(1280, 900));
await tester.pumpWidget(const LayoutApp());
await tester.pumpAndSettle();
expect(find.byKey(const Key('wide-row')), findsOneWidget);
expect(find.byKey(const Key('narrow-column')), findsNothing);
expect(tester.takeException(), isNull);
});
testWidgets('窄屏(420)用 Column 单栏', (WidgetTester tester) async {
_setSize(tester, const Size(420, 900));
await tester.pumpWidget(const LayoutApp());
await tester.pumpAndSettle();
expect(find.byKey(const Key('narrow-column')), findsOneWidget);
expect(find.byKey(const Key('wide-row')), findsNothing);
expect(tester.takeException(), isNull);
});
testWidgets('窗口从小拉到大:两种布局都能正常重排', (WidgetTester tester) async {
_setSize(tester, const Size(420, 900));
await tester.pumpWidget(const LayoutApp());
await tester.pumpAndSettle();
expect(find.byKey(const Key('narrow-column')), findsOneWidget);
tester.view.physicalSize = const Size(1280, 900);
await tester.pumpAndSettle();
expect(find.byKey(const Key('wide-row')), findsOneWidget);
expect(tester.takeException(), isNull);
});
});
group('约束系统', () {
testWidgets('LayoutBuilder 读到的约束等于父级给的最大宽度', (WidgetTester tester) async {
_setSize(tester, const Size(1000, 800));
await tester.pumpWidget(const LayoutApp());
await tester.pumpAndSettle();
final Text report = tester.widget<Text>(find.byKey(const Key('constraint-report')));
// LayoutBuilder 在 Scaffold.body 里,拿到的是整个 body 的紧约束盒:
// 宽度 0..1000(松约束),高度 0..744(AppBar 占掉 56)。
expect(report.data, contains('0..1000'));
expect(report.data, contains('0..744'));
expect(report.data, contains('tight(width): false'));
expect(report.data, contains('tight(height): false'));
});
testWidgets('Expanded 的 flex 按 1:2 分配剩余宽度', (WidgetTester tester) async {
_setSize(tester, const Size(1280, 900));
await tester.pumpWidget(const LayoutApp());
await tester.pumpAndSettle();
final Size a = tester.getSize(find.byKey(const Key('flex-a')));
final Size b = tester.getSize(find.byKey(const Key('flex-b')));
final Size fixed = tester.getSize(find.byKey(const Key('fixed-box')));
expect(fixed.width, 64);
// flex 2 的宽度约等于 flex 1 的两倍
expect(b.width / a.width, closeTo(2.0, 0.02));
// 三者加两个 8px 间距正好等于所在 Row 的宽度
final double rowWidth = tester
.getSize(find.ancestor(of: find.byKey(const Key('flex-a')), matching: find.byType(Row)).first)
.width;
expect(a.width + b.width + fixed.width + 16, closeTo(rowWidth, 0.5));
});
testWidgets('AspectRatio 反推高度', (WidgetTester tester) async {
_setSize(tester, const Size(800, 900));
await tester.pumpWidget(const LayoutApp());
await tester.pumpAndSettle();
final Size size = tester.getSize(find.byKey(const Key('aspect-16-9')));
expect(size.height / size.width, closeTo(9 / 16, 0.01));
});
testWidgets('Stack 的 Positioned 贴住右上角', (WidgetTester tester) async {
_setSize(tester, const Size(1280, 900));
await tester.pumpWidget(const LayoutApp());
await tester.pumpAndSettle();
final Rect badge = tester.getRect(find.byKey(const Key('stack-badge')));
final Rect stack =
tester.getRect(find.ancestor(of: find.byKey(const Key('stack-badge')), matching: find.byType(Stack)).first);
expect(stack.right - badge.right, closeTo(8, 0.5));
expect(badge.top - stack.top, closeTo(8, 0.5));
});
testWidgets('Wrap 在窄宽度下折成多行', (WidgetTester tester) async {
_setSize(tester, const Size(420, 900));
await tester.pumpWidget(const LayoutApp());
await tester.pumpAndSettle();
final Finder chipFinder = find.descendant(
of: find.byKey(const Key('wrap')),
matching: find.byType(Chip),
);
expect(chipFinder, findsNWidgets(10));
// 第一行最后一个 chip 与第二行第一个 chip 的 y 坐标不同 → 确实换行了
final double firstY = tester.getTopLeft(chipFinder.at(0)).dy;
final double lastY = tester.getTopLeft(chipFinder.at(9)).dy;
expect(lastY, greaterThan(firstY));
expect(tester.takeException(), isNull);
});
});
group('典型约束错误(这些代码在真实项目里一定会遇到)', () {
testWidgets('无界高度里用 Expanded 会抛 unbounded height', (WidgetTester tester) async {
// 一次布局失败会连锁产生多条错误,直接抓 FlutterError.onError 拿到第一条的原文。
final errors = <FlutterErrorDetails>[];
final void Function(FlutterErrorDetails)? previous = FlutterError.onError;
FlutterError.onError = errors.add;
addTearDown(() => FlutterError.onError = previous);
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: SingleChildScrollView(
child: Column(
children: <Widget>[Expanded(child: Text('boom'))],
),
),
),
),
);
FlutterError.onError = previous;
expect(errors, isNotEmpty);
expect(errors.first.exception.toString(),
contains('incoming height constraints are unbounded'));
});
testWidgets('Row 里放超宽文本会 overflow,但加 Flexible 就好了', (WidgetTester tester) async {
_setSize(tester, const Size(200, 400));
// 失败版本:Text 不换行也不收缩
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: Row(children: <Widget>[Text('这是一段很长很长很长很长很长很长的文本')]),
),
),
);
expect(tester.takeException().toString(), contains('overflowed'));
// 修复版本:包一层 Flexible,允许子级在约束内换行
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: Row(children: <Widget>[Flexible(child: Text('这是一段很长很长很长很长很长很长的文本'))]),
),
),
);
expect(tester.takeException(), isNull);
});
});
}