如何在不同的屏幕尺寸上测试 Flutter widgets?

How to test Flutter widgets on different screen sizes?

我有一个 Flutter 小部件,它根据屏幕大小显示额外的数据。有谁知道在多种不同屏幕尺寸上测试此小部件的方法吗?

我查看了 widget_tester 源代码,但找不到任何内容。

您可以使用 WidgetTester

指定自定义表面尺寸

以下代码将 运行 屏幕尺寸为 42x42

的测试
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets("foo", (tester) async {
    tester.binding.window.physicalSizeTestValue = Size(42, 42);

    // resets the screen to its original size after the test end
    addTearDown(tester.binding.window.clearPhysicalSizeTestValue);

    // TODO: do something
  });
}

虽然@Rémi Rousselet 的回答很有帮助,但并没有完全解决我的问题。事实证明,我可以将我的待测小部件包装在 MediaQuery 小部件中并设置大小。

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  Widget makeTestableWidget({Widget child, Size size}) {
    return MaterialApp(
      home: MediaQuery(
        data: MediaQueryData(size: size),
        child: child,
      ),
    );
  }

  testWidgets("tablet", (tester) async {
    final testableWidget = makeTestableWidget(
      child: WidgetUnderTest(),
      size: Size(1024, 768),
    );

    ...
  });

  testWidgets("phone", (tester) async {
    final testableWidget = makeTestableWidget(
      child: WidgetUnderTest(),
      size: Size(375, 812),
    );

    ...
  });
}

不知道为什么,@rémi-rousselet 的解决方案对我不起作用。我必须使用 binding.window.physicalSizeTestValuebinding.window.devicePixelRatioTestValue 指定屏幕尺寸,以便输出完全确定

我为像我这样的 flutter 初学者添加了更多代码。检查这个:

void main() {

  final TestWidgetsFlutterBinding binding =
    TestWidgetsFlutterBinding.ensureInitialized();

  testWidgets("Basic layout test (mobile device)", (tester) async {
    binding.window.physicalSizeTestValue = Size(400, 200);
    binding.window.devicePixelRatioTestValue = 1.0;

    await tester.pumpWidget(new MyApp());

    expect(find.byType(MyHomePage), findsOneWidget);
    // etc.
  });
}

@rémi-rousselet 的解决方案非常有效!

此外,如果您想测试方向更改,请尝试以下操作:

const double PORTRAIT_WIDTH = 400.0;
const double PORTRAIT_HEIGHT = 800.0;
const double LANDSCAPE_WIDTH = PORTRAIT_HEIGHT;
const double LANDSCAPE_HEIGHT = PORTRAIT_WIDTH;

final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();

await binding.setSurfaceSize(Size(PORTRAIT_WIDTH, PORTRAIT_HEIGHT));
await tester.pumpWidget(MyWidget());

// test in portrait

await binding.setSurfaceSize(Size(LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT));
await tester.pumpAndSettle();

// OrientationBuilder gets triggered

// test in landscape

有一个名为 device_preview 的包可以在不同的设备上模拟您的 flutter 应用 运行。

您可以试试这个小部件来测试您的小部件实时改变屏幕大小

屏幕尺寸测试

https://pub.dev/packages/screen_size_test

预览

演示 https://dartpad.dartlang.org/43d9c47a8bf031ce3ef2f6314c9dbd52

代码示例

import 'package:screen_size_test/screen_size_test.dart';
...
MaterialApp(
  title: 'Demo',
  builder: (context, child) => ScreenSizeTest(
    child: child,
  ),
  home: Scaffold(
    body: ListView(
      children: List.generate(
          20,
          (index) => Container(
                padding: EdgeInsets.all(10),
                child: Placeholder(),
              )),
    ),
  ),
)

目前最安全的方法是使用setSurfaceSize

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets("foo", (tester) async {
    tester.binding.setSurfaceSize(Size(400, 400));

    // reset
    tester.binding.setSurfaceSize(null);

    // continue
  });
}

有关 Github 问题,请参阅 here