Skip to content

Commit 984a34b

Browse files
committed
Max Width Scale #2
*Scale max width height proportionally too.
1 parent 34f1f40 commit 984a34b

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

lib/responsive_framework.dart

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,21 @@ class _ResponsiveWrapperState extends State<ResponsiveWrapper>
165165

166166
/// Get screen height calculations.
167167
double screenHeight = 1;
168-
double getScreenHeight() => windowHeight;
168+
double getScreenHeight() {
169+
// Check if screenWidth exceeds maxWidth.
170+
if (widget.maxWidth != null) if (windowWidth > widget.maxWidth) {
171+
// Check if there is an active breakpoint with scale set to true.
172+
if (activeBreakpoint != null &&
173+
activeBreakpoint.breakpoint > widget.maxWidth &&
174+
activeBreakpoint.scale) {
175+
// Scale screen height by the amount the width was scaled.
176+
return windowHeight / (screenWidth / widget.maxWidth);
177+
}
178+
}
179+
180+
// Return default window height as height.
181+
return windowHeight;
182+
}
169183

170184
ResponsiveBreakpoint activeBreakpoint;
171185

@@ -198,6 +212,10 @@ class _ResponsiveWrapperState extends State<ResponsiveWrapper>
198212
if (activeBreakpoint.scale == false)
199213
return screenWidth / activeBreakpoint.scaleFactor;
200214

215+
// Screen is larger than max width. Scale from max width.
216+
if (widget.maxWidth != null) if (activeBreakpoint.breakpoint >
217+
widget.maxWidth) return widget.maxWidth / activeBreakpoint.scaleFactor;
218+
201219
// Return width from breakpoint with scale factor applied.
202220
return activeBreakpoint.breakpoint / activeBreakpoint.scaleFactor;
203221
}
@@ -241,6 +259,13 @@ class _ResponsiveWrapperState extends State<ResponsiveWrapper>
241259
if (activeBreakpoint.scale == false)
242260
return screenHeight / activeBreakpoint.scaleFactor;
243261

262+
// Screen is larger than max width. Calculate height
263+
// from max width.
264+
if (widget.maxWidth != null) if (activeBreakpoint.breakpoint >
265+
widget.maxWidth) {
266+
return screenHeight / activeBreakpoint.scaleFactor;
267+
}
268+
244269
// Find width adjustment scale to proportionally scale height.
245270
// If screenWidth is scaled 1.5x larger than the breakpoint,
246271
// decrease screenHeight by 33.33% to proportionally scale content.
@@ -260,6 +285,7 @@ class _ResponsiveWrapperState extends State<ResponsiveWrapper>
260285
/// Default fullscreen enabled.
261286
get fullscreen => widget.maxWidth == null;
262287

288+
/// Calculate updated dimensions.
263289
void setDimensions() {
264290
devicePixelRatio = getDevicePixelRatio();
265291
windowWidth = getWindowWidth();
@@ -299,15 +325,13 @@ class _ResponsiveWrapperState extends State<ResponsiveWrapper>
299325

300326
@override
301327
void didChangeMetrics() {
302-
setDimensions();
303-
setState(() {});
304-
}
305-
306-
@override
307-
void didUpdateWidget(ResponsiveWrapper oldWidget) {
308-
super.didUpdateWidget(oldWidget);
309-
setDimensions();
310-
setState(() {});
328+
super.didChangeMetrics();
329+
// Required MediaQueryData is only updated
330+
// on the next frame.
331+
WidgetsBinding.instance.addPostFrameCallback((_) {
332+
setDimensions();
333+
setState(() {});
334+
});
311335
}
312336

313337
@override
@@ -483,5 +507,6 @@ class ResponsiveBreakpoint {
483507
{@required this.breakpoint,
484508
this.scale = false,
485509
this.scaleFactor = 1,
486-
this.name});
510+
this.name})
511+
: assert(breakpoint != null);
487512
}

pubspec.lock

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,6 @@ packages:
9595
url: "https://pub.dartlang.org"
9696
source: hosted
9797
version: "1.6.4"
98-
pedantic:
99-
dependency: transitive
100-
description:
101-
name: pedantic
102-
url: "https://pub.dartlang.org"
103-
source: hosted
104-
version: "1.8.0+1"
10598
petitparser:
10699
dependency: transitive
107100
description:
@@ -162,7 +155,7 @@ packages:
162155
name: test_api
163156
url: "https://pub.dartlang.org"
164157
source: hosted
165-
version: "0.2.11"
158+
version: "0.2.14"
166159
typed_data:
167160
dependency: transitive
168161
description:

0 commit comments

Comments
 (0)