У меня есть StatefulWidget, который создает AnimatedCrossFade
виджет в didUpdateWidget()
и сохраняет его как animation
.Вот сокращенный пример:
class BackgroundImage extends StatefulWidget {
final Color colorA;
final Color colorB;
const BackgroundImage({
this.colorA,
this.colorB,
});
}
class _BackgroundImageState extends State<BackgroundImage> {
Widget _animation;
@override
void didUpdateWidget(Widget old) {
super.didUpdateWidget(old);
_buildBackgroundA(colorA).then((backgroundA) {
_buildBackgroundB(colorB).then(backgroundB) {
print(backgroundA); // this is not null
print(backgroundB); // this is not null
_animation = AnimatedCrossFade(
duration: Duration(seconds: 15),
firstChild: backgroundA,
secondChild: backgroundB,
crossFadeState: someVarToSwitchColors,
? CrossFadeState.showFirst
: CrossFadeState.showSecond,
);
}
}
}
@override
Widget build(BuildContext context) {
return _animation != null ? _animation : Container();
}
}
_buildBackgroundA()
и _buildBackgroundB()
- это асинхронные функции, которые возвращаются Future<Widget>
.Это прекрасно работает в моем приложении - вызывается didUpdateWidget()
, появляется мой AnimatedCrossFade
и анимируется между двумя фонами.
Однако у меня возникают проблемы с поиском AnimatedCrossFade
в моем тесте.Я могу найти другие виджеты без сохранения состояния, а также найти виджет BackgroundImage
.У меня есть что-то вроде:
await tester.pump();
await tester.pumpAndSettle(Duration(minutes: 1));
expect(
find.descendant(
of: find.byType(BackgroundImage),
matching: find.byType(AnimatedCrossFade)),
findsOneWidget);
Это не удается, так как не может найти AnimatedCrossFade
.
Если я изменю свою функцию build()
на:
@override
Widget build(BuildContext context) {
return AnimatedCrossFade(...);
}
Я могу найти свой виджет.Поэтому я подозреваю, что это как-то связано с моим тестом expect
, выполняющимся до выполнения моих функций _buildBackground()
.Я пытался изменить продолжительность в моих pump
и pumpAndSettle
безрезультатно.Как заставить тест ждать больше?Что-то еще мне не хватает?
Журнал испытаний выглядит так (с моими отпечатками):
Running Flutter tests.
00:00 +0: ...d/work/.../cl00:00 +0: (setUpAll) 00:00 +0: Test background
init state called
_buildBackgroundA called
init state called
_buildBackgroundB called
...
00:00 +0 -1: Test background
Expected: exactly one matching node in the widget tree
Actual: ?:<zero widgets with type "AnimatedCrossFade" that has ancestor(s) with type "BackgroundImage" (ignoring offstage widgets)>
Which: means none were found but one was expected
This was caught by the test expectation on the following line:
...line 179
about to return from calling _buildBackgroundA
image: Image(image: MemoryImage(_Uint8ArrayView#af55b, scale: 1.0), width: 50.0, height: 200.0, fit: cover, alignment: center, this.excludeFromSemantics: false, filterQuality: low)
about to return from calling _buildBackgroundB
image: Image(image: MemoryImage(_Uint8ArrayView#79291, scale: 1.0), width: 50.0, height: 830.0, fit: cover, alignment: center, this.excludeFromSemantics: false, filterQuality: low)
...