如何在不同屏幕尺寸上测试 Flutter 小部件?
问题描述:
我有一个 Flutter 小部件,它根据屏幕大小显示额外的数据.有谁知道在多种不同屏幕尺寸上测试这个小部件的方法吗?
I have a Flutter widget which shows extra data depending on the screen size. Does anyone know a way of testing this widget on multiple different screen sizes?
我已经浏览了 widget_tester 源代码,但找不到任何东西.
I've had a look through the widget_tester source code but can't find anything.
答
您可以使用 WidgetTester
以下代码将在屏幕尺寸为 42x42 的情况下运行测试
The following code will run a test with a screen size of 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
});
}