Little Monkey
Little Monkey

Reputation: 6157

How to test container's width/height size

I'm trying to test the width and height of a container. I'm trying to do something like this:

expect(
        find.byWidgetPredicate((Widget widget) =>
            widget is Container  && widget.width == 48),
        findsOneWidget);

but. unfortunately, if I type widget.width, it will say that the getter width isn't defined for the class Container.

Upvotes: 2

Views: 1378

Answers (2)

Hugo Passos
Hugo Passos

Reputation: 8447

There's no such property in Container. Instead, it holds a BoxConstraints with the minimum and maximum width of the Container. Assuming that minWidth and maxWidth are equal in your case, consider using:

expect(
  find.byWidgetPredicate((Widget widget) {
    if (widget is Container) {
      BoxConstraints width = widget.constraints.widthConstraints();
      return (width.minWidth == width.maxWidth) && (width.minWidth == 48);
    } else {
      return false;
    }
  }),
  findsOneWidget,
);

For the height:

expect(
  find.byWidgetPredicate((Widget widget) {
    if (widget is Container) {
      BoxConstraints height = widget.constraints.heightConstraints();
      return (height.minHeight == height.maxHeight) && (height.minHeight == 48);
    } else {
      return false;
    }
  }),
  findsOneWidget,
);

Upvotes: 5

Sami Kanafani
Sami Kanafani

Reputation: 15751

try:

widget.constraints.widthConstraints();

Upvotes: 1

Related Questions