Run your tests on multiple screen size

Flutter tips Published on

Developing Flutter apps means they should look good and work correctly on all sorts of devices.

Prevent Overflows for the Screen Sizes

One big challenge is making sure your layouts don't break or overflow on different screen sizes and densities. Widget tests are great for checking this automatically.

How it Works

You can set up your widget tests to run the same test logic but with different screen configurations. This way, you can catch layout issues early for phones, tablets, or even custom sizes you define.

This approach lets you run a test with every screen setup you list.

Defining Screen Sizes

First, you can create a simple class to hold screen details like name, width, height, and pixel density.

class ScreenSize {
  final String name;
  final double width;
  final double height;
  final double pixelDensity;

  const ScreenSize(this.name, this.width, this.height, this.pixelDensity);

  
  String toString() => name;
}

const iphone8Plus = ScreenSize('iPhone 8 Plus', 414, 736, 3);
const iphone11Max = ScreenSize('iPhone 11 Max', 414, 896, 3);
const samsungGalaxyS = ScreenSize('Android Samsung Galaxy S', 480, 800, 1);

final basicPhones = <ScreenSize>[iphone8Plus, iphone11Max];

Testing with Multiple Sizes

You can use ValueVariant in your testWidgets function along with a helper to set the screen size before testing your widget.

extension ScreenSizeManager on WidgetTester {
  Future<void> setScreenSize(ScreenSize screenSize) async {
    return _setScreenSize(
      width: screenSize.width,
      height: screenSize.height,
      pixelDensity: screenSize.pixelDensity,
    );
  }
}

testWidgets(
  'click on button => shows an anchored widget overlay',
  (WidgetTester tester) async {
    await tester.setScreenSize(screenSizeVariants.currentValue!); // Set the current screen size
    
    await tester.pumpWidget(const MyAppWithCircleAnchored());
    expect(find.byType(AnchoredHelper), findsNothing);
    
    await tester.tap(find.byType(OutlinedButton).first);
    await tester.pump(const Duration(seconds: 2));
    expect(find.byType(AnchoredHelper), findsOneWidget);
  },
  variant: ValueVariant<ScreenSize>(basicPhones), // Run the test for each screen in the list
);
Results

The test runner will show that your test is run for each screen size defined in your basicPhones list, giving you confidence that your UI holds up across common devices.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

Why should I test my Flutter app on different screen sizes?

Testing on different screen sizes helps you find and fix layout issues, like elements overflowing or not fitting correctly, ensuring your app looks and works well on many devices.

How can I run widget tests on different screen sizes in Flutter?

You can use `testWidgets` with `ValueVariant` and define a list of screen configurations. A helper function can then set the test environment's screen size for each configuration before the test runs.

Can I define my own custom screen sizes for testing?

Yes, you can create custom screen size configurations by specifying the width, height, and pixel density you want to test against.

Read more
You may also be interested in
Format JSON with dart  blog card image
Format JSON with dart
Published on 2025-05-12T12:24:09.317Z
Run your tests with multiple variants  blog card image
Run your tests with multiple variants
Published on 2025-05-12T12:23:50.718Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved