-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathhelpers.dart
More file actions
262 lines (218 loc) · 7.34 KB
/
Copy pathhelpers.dart
File metadata and controls
262 lines (218 loc) · 7.34 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../responsive_builder.dart';
/// Returns the [DeviceScreenType] that the application is currently running on
DeviceScreenType getDeviceType(
Size size, [
ScreenBreakpoints? breakpoint,
]) {
double deviceWidth = size.shortestSide;
if (kIsWeb || Platform.isMacOS || Platform.isWindows || Platform.isLinux) {
deviceWidth = size.width;
}
// Replaces the defaults with the user defined definitions
if (breakpoint != null) {
if (deviceWidth > breakpoint.desktop) {
return DeviceScreenType.desktop;
}
if (deviceWidth > breakpoint.tablet) {
return DeviceScreenType.tablet;
}
if (deviceWidth < breakpoint.watch) {
return DeviceScreenType.watch;
}
} else {
// If no user defined definitions are passed through use the defaults
if (deviceWidth >= ResponsiveSizingConfig.instance.breakpoints.desktop) {
return DeviceScreenType.desktop;
}
if (deviceWidth >= ResponsiveSizingConfig.instance.breakpoints.tablet) {
return DeviceScreenType.tablet;
}
if (deviceWidth < ResponsiveSizingConfig.instance.breakpoints.watch) {
return DeviceScreenType.watch;
}
}
return DeviceScreenType.mobile;
}
/// Returns the [RefindedSize] for each device that the application is currently running on
RefinedSize getRefinedSize(
Size size, {
RefinedBreakpoints? refinedBreakpoint,
bool isWebOrDesktop = kIsWeb,
}) {
DeviceScreenType deviceScreenType = getDeviceType(size);
double deviceWidth = size.shortestSide;
if (isWebOrDesktop) {
deviceWidth = size.width;
}
// Replaces the defaults with the user defined definitions
if (refinedBreakpoint != null) {
if (deviceScreenType == DeviceScreenType.desktop) {
if (deviceWidth > refinedBreakpoint.desktopExtraLarge) {
return RefinedSize.extraLarge;
}
if (deviceWidth > refinedBreakpoint.desktopLarge) {
return RefinedSize.large;
}
if (deviceWidth > refinedBreakpoint.desktopNormal) {
return RefinedSize.normal;
}
}
if (deviceScreenType == DeviceScreenType.tablet) {
if (deviceWidth > refinedBreakpoint.tabletExtraLarge) {
return RefinedSize.extraLarge;
}
if (deviceWidth > refinedBreakpoint.tabletLarge) {
return RefinedSize.large;
}
if (deviceWidth > refinedBreakpoint.tabletNormal) {
return RefinedSize.normal;
}
}
if (deviceScreenType == DeviceScreenType.mobile) {
if (deviceWidth > refinedBreakpoint.mobileExtraLarge) {
return RefinedSize.extraLarge;
}
if (deviceWidth > refinedBreakpoint.mobileLarge) {
return RefinedSize.large;
}
if (deviceWidth > refinedBreakpoint.mobileNormal) {
return RefinedSize.normal;
}
}
if (deviceScreenType == DeviceScreenType.watch) {
return RefinedSize.normal;
}
} else {
// If no user defined definitions are passed through use the defaults
// Desktop
if (deviceScreenType == DeviceScreenType.desktop) {
if (deviceWidth >=
ResponsiveSizingConfig
.instance.refinedBreakpoints.desktopExtraLarge) {
return RefinedSize.extraLarge;
}
if (deviceWidth >=
ResponsiveSizingConfig.instance.refinedBreakpoints.desktopLarge) {
return RefinedSize.large;
}
if (deviceWidth >=
ResponsiveSizingConfig.instance.refinedBreakpoints.desktopNormal) {
return RefinedSize.normal;
}
}
// Tablet
if (deviceScreenType == DeviceScreenType.tablet) {
if (deviceWidth >=
ResponsiveSizingConfig.instance.refinedBreakpoints.tabletExtraLarge) {
return RefinedSize.extraLarge;
}
if (deviceWidth >=
ResponsiveSizingConfig.instance.refinedBreakpoints.tabletLarge) {
return RefinedSize.large;
}
if (deviceWidth >=
ResponsiveSizingConfig.instance.refinedBreakpoints.tabletNormal) {
return RefinedSize.normal;
}
}
// Mobile
if (deviceScreenType == DeviceScreenType.mobile) {
if (deviceWidth >=
ResponsiveSizingConfig.instance.refinedBreakpoints.mobileExtraLarge) {
return RefinedSize.extraLarge;
}
if (deviceWidth >=
ResponsiveSizingConfig.instance.refinedBreakpoints.mobileLarge) {
return RefinedSize.large;
}
if (deviceWidth >=
ResponsiveSizingConfig.instance.refinedBreakpoints.mobileNormal) {
return RefinedSize.normal;
}
}
}
return RefinedSize.small;
}
/// Will return one of the values passed in for the device it's running on
T getValueForScreenType<T>({
required BuildContext context,
required T mobile,
T? tablet,
T? desktop,
T? watch,
}) {
var deviceScreenType = getDeviceType(MediaQuery.sizeOf(context));
// If we're at desktop size
if (deviceScreenType == DeviceScreenType.desktop) {
// If we have supplied the desktop layout then display that
if (desktop != null) return desktop;
// If no desktop layout is supplied we want to check if we have the size below it and display that
if (tablet != null) return tablet;
}
if (deviceScreenType == DeviceScreenType.tablet) {
if (tablet != null) return tablet;
}
if (deviceScreenType == DeviceScreenType.watch && watch != null) {
return watch;
}
if (deviceScreenType == DeviceScreenType.mobile) {
if (mobile != null) return mobile;
}
// If none of the layouts above are supplied we use the prefered layout based on the flag
final buildDesktopLayout = ResponsiveAppUtil.preferDesktop && desktop != null;
return buildDesktopLayout ? desktop : mobile;
}
/// Will return one of the values passed in for the refined size
T getValueForRefinedSize<T>({
required BuildContext context,
required T normal,
T? large,
T? extraLarge,
T? small,
}) {
var refinedSize = getRefinedSize(MediaQuery.sizeOf(context));
// If we're at extra large size
if (refinedSize == RefinedSize.extraLarge) {
// If we have supplied the extra large layout then display that
if (extraLarge != null) return extraLarge;
// If no extra large layout is supplied we want to check if we have the size below it and display that
if (large != null) return large;
}
if (refinedSize == RefinedSize.large) {
// If we have supplied the large layout then display that
if (large != null) return large;
// If no large layout is supplied we want to check if we have the size below it and display that
if (normal != null) return normal;
}
if (refinedSize == RefinedSize.normal) {
// If we have supplied the normal layout then display that
if (normal != null) return normal;
}
if (refinedSize == RefinedSize.small) {
// If we have supplied the small layout then display that
if (small != null) return small;
}
// If none of the layouts above are supplied or we're on the normal size layout then we show the normal layout
return normal;
}
class ScreenTypeValueBuilder<T> {
@Deprecated('Use better named global function getValueForScreenType')
T getValueForType({
required BuildContext context,
required T mobile,
T? tablet,
T? desktop,
T? watch,
}) {
return getValueForScreenType(
context: context,
mobile: mobile,
tablet: tablet,
desktop: desktop,
watch: watch,
);
}
}