【问题标题】:Check for color during widget test?在小部件测试期间检查颜色?
【发布时间】:2020-03-30 02:07:30
【问题描述】:

目标是验证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 dart flutter-test


    【解决方案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 颜色的 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);
    

    【讨论】:

      【解决方案2】:

      找到 Appbar Material。颜色

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

      查找文字颜色材质

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

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 2021-09-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-17
          • 2011-12-19
          • 1970-01-01
          • 2020-06-05
          相关资源
          最近更新 更多