Check for color during widget test?

Issue

The goal is to verify the color of a RaisedButton.icon

During my widget tests, I have the ability to look for text with find.text as well as icons with find.byIcon. There is no built in method for finding color.

How does one do the equivalent of find.color?

And example of my code is

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

I’m trying to determine whether there is color: Color(0xFFD9FFB3) or color: Color(0xFFE3FFC7)

And I’m not sure if this is possible

Solution

I am not sure with RaisedButton.icon, but if it is just an icon you can try using:

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

If it not the first icon widget that appears on your screen, you can specific an index of it by:

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

Note: To find a widget colour, you need to cast that widget to be something that contain a color property

For example:

To find Material color you can use:

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

To find Container with BoxDecoration color:

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

To find Text color:

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

Answered By – Net Natnicha

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *