我需要测试中的屏幕看起来与物理设备(或模拟器)上的相同。我该怎么做?在我的情况下,设备 ID Iphone SE。
我编写了一个将屏幕截图保存到磁盘的测试:
testWidgets('test', (WidgetTester tester) async {
final AutomatedTestWidgetsFlutterBinding binding = tester.binding;
binding.renderView.configuration = TestViewConfiguration(size: Size(640, 1136));
var widget = Scaffold(
appBar: AppBar(title: Text('title'),),
body: Column(children: <Widget>[
RaisedButton(
child: Text('button'),
onPressed: () {},)
],),
);
var key = new GlobalKey();
await tester.pumpWidget(
MaterialApp(home: RepaintBoundary(key: key, child: widget),),
);
await tester.pumpAndSettle();
await tester.runAsync(() async {
RenderRepaintBoundary boundary = key.currentContext.findRenderObject();
var image = await boundary.toImage();
var byteData = await image.toByteData(format: ImageByteFormat.png);
var pngBytes = byteData.buffer.asUint8List();
await File('screen.png').writeAsBytes(pngBytes);
});
});
如果使用带有 devicePixelRatio 的 ViewConfiguration 而不是 TestViewConfiguration,则忽略 devicePixelRatio
如果包装 MaterialApp,MediaQuery 也不起作用
appbar 和 button 少于模拟器