如何在不同屏幕尺寸上测试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 orinal size after the test end
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
// TODO: do something
});
}