Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for color during widget test?

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

like image 286
LongTSQLFiles Avatar asked Dec 05 '19 07:12

LongTSQLFiles


People also ask

Is color a widget in flutter?

The Container widget has a color property. Your custom widget can also have any property such as color or size or colorChild . When you want to access the colorChild property of a StatefulWidget you use widget.

What is a widget test?

The widget test is testing UI components, so it is also called Component Testing. It is used to test a single widget. The main goal of widget testing is to check whether the widget works as expected. A user can write test cases for every widget present in the project.


2 Answers

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);
like image 93
Net Natnicha Avatar answered Sep 23 '22 22:09

Net Natnicha


To find Appbar Material. color

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

To find Text color Material

final text = tester.widget<Text>(find.text("text"));
  expect(text.style.color, Colors.grey);
  expect(text.style.fontSize, 15);
like image 39
DEVSHK Avatar answered Sep 21 '22 22:09

DEVSHK