Skip to content

Commit 1648f5f

Browse files
committed
Add Default Name
*Add Default Name parameter to ResponsiveWrapper.
1 parent 78d89b4 commit 1648f5f

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

lib/responsive_wrapper.dart

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ResponsiveWrapper extends StatefulWidget {
5050
final List<ResponsiveBreakpoint> breakpoints;
5151
final double maxWidth;
5252
final double minWidth;
53+
final String defaultName;
5354
final bool defaultScale;
5455
final double defaultScaleFactor;
5556
final Widget background;
@@ -62,6 +63,7 @@ class ResponsiveWrapper extends StatefulWidget {
6263
this.breakpoints = const [],
6364
this.maxWidth,
6465
this.minWidth = 450,
66+
this.defaultName,
6567
this.defaultScale = false,
6668
this.defaultScaleFactor = 1,
6769
this.background,
@@ -76,6 +78,7 @@ class ResponsiveWrapper extends StatefulWidget {
7678
List<ResponsiveBreakpoint> breakpoints = const [],
7779
double maxWidth,
7880
double minWidth = 450,
81+
String defaultName,
7982
bool defaultScale = false,
8083
double defaultScaleFactor = 1,
8184
Widget background,
@@ -91,6 +94,7 @@ class ResponsiveWrapper extends StatefulWidget {
9194
breakpoints: breakpoints,
9295
maxWidth: maxWidth,
9396
minWidth: minWidth,
97+
defaultName: defaultName,
9498
defaultScale: defaultScale,
9599
defaultScaleFactor: defaultScaleFactor,
96100
background: background,
@@ -146,7 +150,7 @@ class _ResponsiveWrapperState extends State<ResponsiveWrapper>
146150
// Check if screenWidth exceeds maxWidth.
147151
if (widget.maxWidth != null) if (windowWidth > widget.maxWidth) {
148152
// Check if there is an active breakpoint with autoScale set to true.
149-
if (activeBreakpoint != null &&
153+
if (activeBreakpoint.breakpoint != null &&
150154
activeBreakpoint.breakpoint > widget.maxWidth &&
151155
activeBreakpoint.autoScale) {
152156
return widget.maxWidth + (windowWidth - activeBreakpoint.breakpoint);
@@ -165,7 +169,7 @@ class _ResponsiveWrapperState extends State<ResponsiveWrapper>
165169
// Check if screenWidth exceeds maxWidth.
166170
if (widget.maxWidth != null) if (windowWidth > widget.maxWidth) {
167171
// Check if there is an active breakpoint with autoScale set to true.
168-
if (activeBreakpoint != null &&
172+
if (activeBreakpoint.breakpoint != null &&
169173
activeBreakpoint.breakpoint > widget.maxWidth &&
170174
activeBreakpoint.autoScale) {
171175
// Scale screen height by the amount the width was scaled.
@@ -193,7 +197,7 @@ class _ResponsiveWrapperState extends State<ResponsiveWrapper>
193197
double scaledWidth = 1;
194198
double getScaledWidth() {
195199
// No breakpoint is set. Return default calculated width.
196-
if (activeBreakpoint == null) {
200+
if (activeBreakpoint.breakpoint == null) {
197201
// If widget should resize, use default screenWidth.
198202
if (widget.defaultScale == false)
199203
return screenWidth / widget.defaultScaleFactor;
@@ -230,7 +234,7 @@ class _ResponsiveWrapperState extends State<ResponsiveWrapper>
230234
/// [widget.minWidth].
231235
double scaledHeight = 1;
232236
double getScaledHeight() {
233-
if (activeBreakpoint == null) {
237+
if (activeBreakpoint.breakpoint == null) {
234238
// If widget should resize, use default screenHeight.
235239
if (widget.defaultScale == false)
236240
return screenHeight / widget.defaultScaleFactor;
@@ -259,8 +263,8 @@ class _ResponsiveWrapperState extends State<ResponsiveWrapper>
259263
}
260264

261265
double get activeScaleFactor {
262-
if (activeBreakpoint != null && activeBreakpoint.autoScale == true)
263-
return activeBreakpoint.scaleFactor;
266+
if (activeBreakpoint.breakpoint != null &&
267+
activeBreakpoint.autoScale == true) return activeBreakpoint.scaleFactor;
264268

265269
return widget.defaultScaleFactor;
266270
}
@@ -284,11 +288,11 @@ class _ResponsiveWrapperState extends State<ResponsiveWrapper>
284288
/// Active breakpoint is the first breakpoint smaller
285289
/// or equal to the [screenWidth].
286290
ResponsiveBreakpoint getActiveBreakpoint(double screenWidth) {
287-
return widget.breakpoints
288-
.firstWhere((element) => screenWidth >= element.breakpoint, orElse: () {
289-
// No breakpoint found.
290-
return null;
291-
});
291+
return widget.breakpoints.firstWhere(
292+
(element) => screenWidth >= element.breakpoint,
293+
orElse: // No breakpoint found.
294+
() => ResponsiveBreakpoint(
295+
breakpoint: null, name: widget.defaultName));
292296
}
293297

294298
@override
@@ -441,25 +445,25 @@ class ResponsiveWrapperData {
441445
@override
442446
String toString() =>
443447
"ResponsiveWrapperData(" +
444-
"Screen Width: " +
448+
"screenWidth: " +
445449
screenWidth?.toString() +
446-
", Screen Height: " +
450+
", screenHeight: " +
447451
screenHeight?.toString() +
448-
", Scaled Width: " +
452+
", scaledWidth: " +
449453
scaledWidth?.toString() +
450-
", Scaled Height: " +
454+
", scaledHeight: " +
451455
scaledHeight?.toString() +
452-
", Breakpoints: " +
456+
", breakpoints: " +
453457
breakpoints?.asMap().toString() +
454-
", Active Breakpoint: " +
458+
", activeBreakpoint: " +
455459
activeBreakpoint.toString() +
456-
", Is Mobile: " +
460+
", isMobile: " +
457461
isMobile?.toString() +
458-
", Is Phone: " +
462+
", isPhone: " +
459463
isPhone?.toString() +
460-
", Is Tablet: " +
464+
", isTablet: " +
461465
isTablet?.toString() +
462-
", Is Desktop: " +
466+
", isDesktop: " +
463467
isDesktop?.toString() +
464468
")";
465469

@@ -528,19 +532,18 @@ class ResponsiveBreakpoint {
528532
{@required this.breakpoint,
529533
this.autoScale = false,
530534
this.scaleFactor = 1,
531-
this.name})
532-
: assert(breakpoint != null);
535+
this.name});
533536

534537
@override
535538
String toString() =>
536539
"ResponsiveBreakpoint(" +
537-
"Breakpoint: " +
540+
"breakpoint: " +
538541
breakpoint.toString() +
539-
", AutoScale: " +
542+
", autoScale: " +
540543
autoScale.toString() +
541-
", Scale Factor: " +
544+
", scaleFactor: " +
542545
scaleFactor.toString() +
543-
", Name: " +
544-
name?.toString() +
546+
", name: " +
547+
name.toString() +
545548
")";
546549
}

0 commit comments

Comments
 (0)