在小部件测试期间检查颜色?

Check for color during widget test?

目标是验证RaisedButton.icon

的颜色

在我的小部件测试期间,我能够查找带有 find.text 的文本以及带有 find.byIcon 的图标。没有用于查找颜色的内置方法。

如何做相当于 find.color 的事情?

我的代码示例是

RaisedButton.icon(
        color: Color(_isAttendingEvent ? 0xFFD9FFB3 : 0xFFE3FFC7),
        icon: Icon(_isAttendingEvent ? Icons.star : Icons.star_border),
        label: Text(
          _isAttendingEvent ? 'Attending' : 'Attend',
        ),
      ),

我正在尝试确定是否存在 color: Color(0xFFD9FFB3)color: Color(0xFFE3FFC7)

而且我不确定这是否可行

更新你的 flutter 版本。它现在正在工作,我可以在小部件测试中访问颜色。我当前的 flutter 版本是 "Flutter 1.15.18 • channel dev".

之前它不适用于版本 "Flutter v1.12.13+hotfix.5"

希望这会有所帮助。更多详情 here

我不确定 RaisedButton.icon,但如果它只是一个图标,您可以尝试使用:

expect((tester.firstWidget(find.byType(Icon)) as Icon).color, Color(0xFFE3FFC7));

如果它不是出现在屏幕上的第一个图标小部件,您可以通过以下方式指定它的索引:

expect((tester.widget(find.byType(Icon).at(/*index*/)) as Icon).color, Color(0xFFE3FFC7));

注意:要查找小部件颜色,您需要将该小部件转换为包含颜色的东西 属性

例如:

要查找 Material 颜色,您可以使用:

expect((tester.firstWidget(find.byType(Material)) as Material).color, Colors.black);

要查找具有 BoxDecoration 颜色的 Container:

expect(((tester.firstWidget(find.byType(Container)) as Container).decoration 
as BoxDecoration).color, Colors.black);

查找文本颜色:

expect(((tester.firstWidget(find.text('text')) as Text).style).color, Colors.black);

找到应用栏Material。颜色

 final appbar = tester.widget<AppBar>(find.byKey(Key("appbar")));
 expect(appbar.backgroundColor,Colors.white);

查找文本颜色Material

final text = tester.widget<Text>(find.text("text"));
  expect(text.style.color, Colors.grey);
  expect(text.style.fontSize, 15);