-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathdevice_preview.dart
More file actions
66 lines (58 loc) · 1.82 KB
/
device_preview.dart
File metadata and controls
66 lines (58 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import 'package:device_frame/device_frame.dart';
import 'package:flutter/material.dart';
class DevicePreview extends StatelessWidget {
final Widget child;
final DeviceInfo deviceInfo;
final Orientation deviceOrientation;
final bool showDeviceFrame;
const DevicePreview({
required this.child,
required this.deviceInfo,
required this.deviceOrientation,
required this.showDeviceFrame,
super.key,
});
@override
Widget build(BuildContext context) {
final mediaQuery = _mediaQueryData(context);
return Padding(
padding: EdgeInsets.only(
top: 20 + mediaQuery.viewPadding.top,
right: 20 + mediaQuery.viewPadding.right,
left: 20 + mediaQuery.viewPadding.left,
bottom: 20,
),
child: FittedBox(
child: DeviceFrame(
orientation: deviceOrientation,
device: deviceInfo,
isFrameVisible: showDeviceFrame,
screen: MediaQuery(
data: mediaQuery,
child: ColoredBox(
color: Theme.of(context).scaffoldBackgroundColor,
child: child,
),
),
),
),
);
}
MediaQueryData _mediaQueryData(BuildContext context) {
final mediaQuery = MediaQuery.of(context);
final isRotated = deviceInfo.isLandscape(deviceOrientation);
final padding = isRotated
? (deviceInfo.rotatedSafeAreas ?? deviceInfo.safeAreas)
: deviceInfo.safeAreas;
final screenSize = deviceInfo.screenSize;
final width = isRotated ? screenSize.height : screenSize.width;
final height = isRotated ? screenSize.width : screenSize.height;
return mediaQuery.copyWith(
size: Size(width, height),
padding: padding,
viewInsets: EdgeInsets.zero,
viewPadding: padding,
devicePixelRatio: deviceInfo.pixelRatio,
);
}
}