目标是验证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)
我不确定这是否可能
答案 0 :(得分:1)
我不确定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));
例如:
要查找材料颜色,可以使用:
expect((tester.firstWidget(find.byType(Material)) as Material).color, Colors.black);
要找到具有BoxDecoration颜色的容器:
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);
答案 1 :(得分:0)
更新您的Flutter版本。现在正在工作,我可以在小部件测试中访问颜色。我当前的Flutter版本是“ Flutter 1.15.18•channel dev”。
之前,它不适用于“ Flutter v1.12.13 + hotfix.5”版本
希望这会有所帮助。更多详细信息here