How to Test Flutter Widgets on Different Screen Sizes
In this article
Testing Flutter widgets on different screen sizes means wrapping the widget under test in a sized container or overriding the test surface size, then pumping the widget and asserting that nothing overflows, clips or disappears. In widget tests you do this with tester.view.physicalSize and tester.view.devicePixelRatio (or the older tester.binding.window API on very old SDKs), and you reset them with addTearDown(tester.view.reset). For visual checks, run the app on a resizable desktop window or an emulator and change the display size, or use a widget previewer. This guide covers the automated approach first, because it is the one that catches regressions.
What You'll Need
- A Flutter project with at least one widget test (a
test/folder and theflutter_testdependency, which is added by default). - A working Flutter SDK and a terminal where
flutter testruns. - Optional but useful: an emulator or a desktop target (Windows, macOS, Linux) so you can resize the window by hand.
- Familiarity with
WidgetTester,pumpWidgetandfindmatchers.
Step-by-Step
- Decide what "different screen sizes" means for your widget. Pick a small phone (roughly 320x568 logical pixels), a common phone (390x844), a tablet (768x1024) and, if you support it, a desktop window (1280x800). These are logical pixels, not device pixels, so they map cleanly onto the test surface.
- Create a helper that sets the surface size. In your test file, write a function that takes a
WidgetTesterand aSize, setstester.view.physicalSizeto the size multiplied by the device pixel ratio, and setstester.view.devicePixelRatioto 1.0 so the numbers stay readable. RegisteraddTearDown(tester.view.reset)inside the helper so the next test is not affected. - Pump the widget inside a realistic shell. Wrap your widget in a
MaterialApp(orCupertinoApp) and aScaffoldif it expects one. A widget that renders fine alone can still overflow once it sits inside an app bar and a navigation bar, so test it where it will actually live. - Run the same assertions at each size. Use a loop over your list of sizes: set the size, pump the widget, then check for the things that break first —
expect(tester.takeException(), isNull)catches overflow errors, and finders confirm that key text or buttons are still present.find.text('Sign in')plusexpect(..., findsOneWidget)is enough for a smoke test. - Check that nothing overflows. Flutter reports a RenderFlex overflow as an exception during layout, so
tester.takeException()returning aFlutterErroris your signal. If you want the test to fail loudly with the message, assert that the exception is null and print it when it is not. - Assert visibility, not just existence. A widget can exist in the tree but be off-screen. Use
tester.getRect(finder)and compare against the surface size, or usefind.byWidgetPredicatewith a hit-test check, to confirm the element is actually within the visible area. - Test scrolling behaviour separately. On a short screen a
Columnmay need to become aListView. Pump at the smallest size, then calltester.dragortester.scrollUntilVisibleto prove the content is reachable rather than clipped. - Run the suite. Execute
flutter testin the terminal. Every size variant runs in the same process, so failures name the exact test and the overflow message points at the offending widget. - Do a manual pass for visual polish. Run the app on a desktop target and drag the window edges, or use an emulator and change its resolution in the device settings. Widget tests catch layout errors, not ugly spacing, so the two methods complement each other.
- Keep the size list in one place. Put the sizes in a shared constant file so new tests reuse them. When you add a breakpoint to the app, add the matching size to the list in the same change.
Common Mistakes
- Forgetting to reset the view. Without
tester.view.resetin a teardown, the size leaks into later tests and produces confusing failures that only appear when the whole file runs. - Setting physical size without the pixel ratio. The two work together; setting only one gives a surface that is not the size you intended.
- Testing the widget bare. Skipping
MaterialApporScaffoldhides overflows caused by inherited padding, safe areas or app bars. - Assuming a passing test means a good layout. A widget can fit at 320 pixels wide and still look cramped. Automated tests prove it does not break, not that it looks right.
- Ignoring text scaling. A large system font size breaks layouts as effectively as a narrow screen. Test with a raised
textScaleras well as a small width. - Only testing one orientation. Swapping width and height is a one-line change and catches a surprising number of issues.
Tips
- Name tests after the size, for example
renders without overflow at 320x568, so a failure tells you where to look. - Test the extremes first. If a layout survives the smallest and largest sizes, the middle usually takes care of itself.
- Use
MediaQueryoverrides as a second lever when you want to test safe-area insets or text scaling without changing the surface. - Keep widget tests fast by testing one screen per test group rather than the whole app flow at every size.
- For golden tests, generate the reference images per size and store them in separate folders; a single golden compared across sizes will always fail.
Troubleshooting
The test still reports the old size. Check that the teardown runs and that you are not setting the size after pumpWidget. Set the view first, then pump.
Overflow errors appear only in the full suite. This is almost always a leaked view size from an earlier test. Add addTearDown(tester.view.reset) to every helper that changes the view.
The widget is found but the test fails on a tap. The element is probably outside the visible area. Scroll it into view with scrollUntilVisible before tapping, or assert the rect first to confirm the diagnosis.
Golden tests differ between machines. Font rendering varies by platform. Run golden tests on one platform and one CI image, and regenerate references deliberately rather than accepting whatever the local machine produces.
Manual resizing does not change the layout. Some widgets cache their size. Trigger a rebuild by rotating the window or restarting the app, and confirm you are resizing the app window rather than the emulator frame.
Frequently Asked Questions
Can I test several screen sizes in one test?
Yes. Loop over the sizes, set the view, pump and assert, then reset at the end. Separate tests per size are easier to read when a failure occurs.
Do I need a physical device?
No. Widget tests run on the host machine at any surface size you choose. A device or desktop window is only needed for visual checks.
What is the difference between logical and physical pixels here?
The test view takes physical pixels and divides by the device pixel ratio to get the logical size the layout uses. Setting the ratio to 1.0 keeps the two identical and the numbers easy to reason about.
Will this catch text overflow?
Yes, if the text is laid out inside the pumped widget. Text that overflows its box raises an exception during layout, which tester.takeException() returns.
Should I test every breakpoint?
Test the smallest and largest sizes you support plus each breakpoint where the layout changes. Testing every pixel width adds time without adding coverage.