我想做一个测试,让我验证我的小工具是否显示我提供的AnimatedIcon,以及知道动画是否工作(以动画方式更改图标),问题是我找不到方法.

  1. 有没有类似于finder.byIcon()的方法,但它接受AnimatedIconData而不是IconData
  2. 如何验证动画正在发生?

推荐答案

1.AnimatedIconData的查找器

要获取AnimatedIconData的查找器,请使用如下所示的find.byWidgetPredicate方法(假设要搜索的AnimatedIconDataAnimatedIcons.menu_arrow):

final animatedIconFinder = find.byWidgetPredicate((widget) =>
        widget is AnimatedIcon && widget.icon == AnimatedIcons.menu_arrow); 

2.验证动画正在发生

要验证动画是否正在进行,请判断AnimatedIcon小部件的progress属性值.

假设动画是在点击屏幕上的浮动动作按钮之后发生的,下面的代码示例显示了如何在点击按钮后验证这AnimatedIcon个动画.

// Gets the [AnimatedIcon] widget
final animatedIconWidget = tester.widget(animatedIconFinder) as AnimatedIcon;

final animatedIconProgress = animatedIconWidget.progress.value;

// Verifies that [AnimatedIcon] is not animating
expect(animatedIconProgress, 0);

// Finds [FloatingActionButton]
final floatingActionButtonFinder = 
    find.widgetWithIcon(FloatingActionButton, Icons.change_circle);

// Taps [FloatingActionButton]
await tester.tap(floatingActionButtonFinder);

await tester.pumpAndSettle();

final updatedAnimatedIconProgress = animatedIconWidget.progress.value;

// Verifies that the [AnimatedIcon] has completed its animation
expect(updatedAnimatedIconProgress, 1);

使用自定义查找器

要使您的查找器更简洁、更接近find.byIcon,请执行以下操作:

  1. 通过扩展MatchFinder创建自定义查找器,并在matches方法中添加匹配逻辑(这是对find.byIcon implementation的修改).

    class _WidgetAnimatedIconFinder extends MatchFinder {
      _WidgetAnimatedIconFinder(this.icon, {super.skipOffstage});
    
      final AnimatedIconData icon;
    
      @override
      String get description => 'icon "$icon"';
    
      @override
      bool matches(Element candidate) {
        final Widget widget = candidate.widget;
        return widget is AnimatedIcon && widget.icon == icon;
      }
    }
    
  2. CommonFinders类上创建一个扩展,并添加一个使用在步骤1中创建的自定义Finder的byAnimatedIcon方法.CommonFinders类由flutter_test包提供,find常量(与find.byIcon中一样)是CommonFinders的实例.

    extension CustomFindersExtension on CommonFinders {
      Finder byAnimatedIcon(AnimatedIconData icon) =>
        _WidgetAnimatedIconFinder(icon);
    }
    
  3. 使用find.byAnimatedIcon通过提供要搜索的AnimatedIconData来查找AnimatedIcon.

    将使用find.byWidgetPredicateFinder替换为以下内容:

    final animatedIconFinder = find.byAnimatedIcon(AnimatedIcons.menu_arrow);
    

Flutter相关问答推荐

在Flutter中,CachedNetworks Image不工作(不从CachedData加载)

Flutter在创建时为提供者调用初始化函数

为什么我不能看到类音频源在flutter?'

无法将Flutter 连接到Firebase

为什么Riverpod生成器在这种情况下不生成AsyncNotiator?

BlocBuilder仅生成一次,并在后续发出时停止重建

如何删除使用isscllable时出现的左边距?

Flutter 蜂巢不能正常工作,我正在使用蜂巢在设备上存储数据,但当我关闭并重新启动应用程序时,我失go 了一切

为什么Spotify Auth API返回400,而";code_verier不正确?

Ffmpeg_kit_fltter:^6.0.3版权问题

在flutter web上渲染多个Youtube视频

如何在Flutter 安全存储器中存储对象值?

从future 列表到流列表Flutter Firebase

Select 文本时如何更改 Flutter 中的光标 colored颜色 ?

如何使Flutter 边框宽度在外面而不是在里面?

更新番茄计时器值不会触发 Flutter Riverpod 中的alert 声音

按下 Command+W 时关闭窗口

Flutter:当我通过 Transform.rotate 旋转水平 ListView 时,左右边缘被切断

在 Flutter 中更改高架按钮 OnPressed 的背景 colored颜色

为什么容器会填满整个空间?