Search code examples
flutterflutter-testflutter-constraint

How to tests the constraints of a specific widget?


I have a widget (MyWidget) in a screen (MyScreen) and I would like to write a test that verifies that specific constraints are applied to MyWidgets.

How can I do that?


Solution

  • You can use tester.renderObject() to obtain the RenderObject and get the constraints from it:

    await tester.pumpWidget(const MyScreen());
    
    final renderObject = tester.renderObject(find.byType(MyWidget));
    final constraintsMatcher = BoxConstraints(maxHeight: 200); // For example, it can be any matcher.
    expect(renderObject.constraints, constraintsMatcher);