示例工程 ch07-animation
ch07-animation/test/animation_test.dart
import 'package:ch07_animation/main.dart';
import 'package:ch07_animation/src/animation_gallery.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
Widget host(Widget child) => MaterialApp(home: Scaffold(body: SingleChildScrollView(child: child)));
/// 从 '57%' 这样的文本里取出数字。
int percentOf(WidgetTester tester, Key key) {
final String data = tester.widget<Text>(find.byKey(key)).data!;
return int.parse(data.replaceAll('%', ''));
}
void main() {
group('隐式动画 AnimatedContainer', () {
testWidgets('改目标值 → 中间帧在起止值之间 → 结束后到位', (WidgetTester tester) async {
await tester.pumpWidget(host(const ImplicitCard()));
expect(tester.getSize(find.byKey(const Key('implicit-box'))).width, 80);
await tester.tap(find.byKey(const Key('implicit-toggle')));
await tester.pump(); // 动画开始,第一帧仍是初始值
await tester.pump(const Duration(milliseconds: 200)); // 400ms 动画的中点
final Size mid = tester.getSize(find.byKey(const Key('implicit-box')));
expect(mid.width, greaterThan(80));
expect(mid.width, lessThan(220));
await tester.pumpAndSettle();
expect(tester.getSize(find.byKey(const Key('implicit-box'))).width, 220);
expect(tester.getSize(find.byKey(const Key('implicit-box'))).height, 120);
});
testWidgets('再点一次回到初始尺寸', (WidgetTester tester) async {
await tester.pumpWidget(host(const ImplicitCard()));
await tester.tap(find.byKey(const Key('implicit-toggle')));
await tester.pumpAndSettle();
expect(tester.getSize(find.byKey(const Key('implicit-box'))).width, 220);
await tester.tap(find.byKey(const Key('implicit-toggle')));
await tester.pumpAndSettle();
expect(tester.getSize(find.byKey(const Key('implicit-box'))).width, 80);
});
});
group('TweenAnimationBuilder', () {
testWidgets('数字从 0 补间到 100', (WidgetTester tester) async {
await tester.pumpWidget(host(const TweenBuilderCard()));
expect(find.text('0.0'), findsOneWidget);
await tester.tap(find.byKey(const Key('tween-100')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 400)); // 800ms 的一半
final String midText = tester.widget<Text>(find.byKey(const Key('tween-text'))).data!;
final double mid = double.parse(midText);
expect(mid, greaterThan(0));
expect(mid, lessThan(100));
await tester.pumpAndSettle();
expect(find.text('100.0'), findsOneWidget);
});
});
group('显式动画 AnimationController', () {
testWidgets('forward 推进进度,reached 100% 后停住', (WidgetTester tester) async {
await tester.pumpWidget(host(const ExplicitCard()));
expect(percentOf(tester, const Key('progress-text')), 0);
await tester.tap(find.byKey(const Key('play')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
final int mid = percentOf(tester, const Key('progress-text'));
expect(mid, inInclusiveRange(40, 60)); // 线性曲线,1s 的一半
await tester.pumpAndSettle();
expect(percentOf(tester, const Key('progress-text')), 100);
});
testWidgets('reverse 倒放回 0', (WidgetTester tester) async {
await tester.pumpWidget(host(const ExplicitCard()));
await tester.tap(find.byKey(const Key('play')));
await tester.pumpAndSettle();
expect(percentOf(tester, const Key('progress-text')), 100);
await tester.tap(find.byKey(const Key('reverse')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
expect(percentOf(tester, const Key('progress-text')), inInclusiveRange(40, 60));
await tester.pumpAndSettle();
expect(percentOf(tester, const Key('progress-text')), 0);
});
testWidgets('重置直接把进度归零', (WidgetTester tester) async {
await tester.pumpWidget(host(const ExplicitCard()));
await tester.tap(find.byKey(const Key('play')));
await tester.pump(const Duration(milliseconds: 700));
await tester.tap(find.byKey(const Key('reset')));
await tester.pump();
expect(percentOf(tester, const Key('progress-text')), 0);
});
testWidgets('动画中途卸载:dispose 释放 Ticker,不报错', (WidgetTester tester) async {
await tester.pumpWidget(host(const ExplicitCard()));
await tester.tap(find.byKey(const Key('play')));
await tester.pump(const Duration(milliseconds: 300));
// 动画还在跑就换掉整棵子树
await tester.pumpWidget(host(const Text('已卸载')));
await tester.pump(const Duration(seconds: 2));
expect(tester.takeException(), isNull);
});
});
group('AnimatedSwitcher', () {
testWidgets('切换时新旧子级同时存在,动画结束后只剩新的', (WidgetTester tester) async {
await tester.pumpWidget(host(const SwitcherCard()));
expect(find.text('😀'), findsOneWidget);
await tester.tap(find.byKey(const Key('switcher-next')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 100)); // 交叉淡入中途
expect(find.text('😀'), findsOneWidget); // 旧的还在淡出
expect(find.text('😎'), findsOneWidget); // 新的正在淡入
await tester.pumpAndSettle();
expect(find.text('😀'), findsNothing);
expect(find.text('😎'), findsOneWidget);
});
});
group('整页冒烟', () {
testWidgets('四张动画卡片都能渲染', (WidgetTester tester) async {
tester.view.physicalSize = const Size(520, 1600);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.reset);
await tester.pumpWidget(const AnimationApp());
await tester.pumpAndSettle();
expect(find.byType(ImplicitCard), findsOneWidget);
expect(find.byType(ExplicitCard), findsOneWidget);
expect(tester.takeException(), isNull);
});
});
}