Skip to content

Commit 89154b3

Browse files
authored
[flutter_adaptive_scaffold] Fix landscape not showing in andUp (flutter#7425)
*Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.* *List which issues are fixed by this PR. You must list at least one issue.* flutter#153496
1 parent 1ab1a71 commit 89154b3

6 files changed

Lines changed: 529 additions & 93 deletions

File tree

packages/flutter_adaptive_scaffold/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.2.2
2+
3+
* Fix a bug where landscape would not show body when using `andUp`.
4+
15
## 0.2.1
26

37
* Add `Breakpoint.activeBreakpointOf(context)` to find the currently active breakpoint.

packages/flutter_adaptive_scaffold/lib/src/breakpoints.dart

Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Breakpoints {
1717
/// case that no other breakpoint is active.
1818
///
1919
/// It is active from a width of -1 dp to infinity.
20-
static const Breakpoint standard = Breakpoint(beginWidth: -1);
20+
static const Breakpoint standard = Breakpoint.standard();
2121

2222
/// A window whose width is less than 600 dp and greater than 0 dp.
2323
static const Breakpoint small = Breakpoint.small();
@@ -86,6 +86,30 @@ class Breakpoints {
8686
/// A mobile window whose width is greater than 1600 dp.
8787
static const Breakpoint extraLargeMobile =
8888
Breakpoint.extraLarge(platform: Breakpoint.mobile);
89+
90+
/// A list of all the standard breakpoints.
91+
static const List<Breakpoint> all = <Breakpoint>[
92+
smallDesktop,
93+
smallMobile,
94+
small,
95+
mediumDesktop,
96+
mediumMobile,
97+
medium,
98+
mediumLargeDesktop,
99+
mediumLargeMobile,
100+
mediumLarge,
101+
largeDesktop,
102+
largeMobile,
103+
large,
104+
extraLargeDesktop,
105+
extraLargeMobile,
106+
extraLarge,
107+
smallAndUp,
108+
mediumAndUp,
109+
mediumLargeAndUp,
110+
largeAndUp,
111+
standard,
112+
];
89113
}
90114

91115
/// A class to define the conditions that distinguish between types of
@@ -113,10 +137,19 @@ class Breakpoint {
113137
this.endWidth,
114138
this.beginHeight,
115139
this.endHeight,
116-
this.platform,
117140
this.andUp = false,
141+
this.platform,
118142
});
119143

144+
/// Returns a [Breakpoint] that can be used as a fallthrough in the
145+
/// case that no other breakpoint is active.
146+
const Breakpoint.standard({this.platform})
147+
: beginWidth = -1,
148+
endWidth = null,
149+
beginHeight = null,
150+
endHeight = null,
151+
andUp = true;
152+
120153
/// Returns a [Breakpoint] with the given constraints for a small screen.
121154
const Breakpoint.small({this.andUp = false, this.platform})
122155
: beginWidth = 0,
@@ -166,7 +199,7 @@ class Breakpoint {
166199
TargetPlatform.iOS,
167200
};
168201

169-
/// When set to true, it will include any size above the set width.
202+
/// When set to true, it will include any size above the set width and set height.
170203
final bool andUp;
171204

172205
/// The beginning width dp value. If left null then the [Breakpoint] will have
@@ -213,9 +246,9 @@ class Breakpoint {
213246

214247
final bool isHeightActive = isDesktop ||
215248
orientation == Orientation.portrait ||
216-
(orientation == Orientation.landscape &&
217-
height >= lowerBoundHeight &&
218-
height < upperBoundHeight);
249+
(orientation == Orientation.landscape && andUp
250+
? isWidthActive || height >= lowerBoundHeight
251+
: height >= lowerBoundHeight && height < upperBoundHeight);
219252

220253
return isWidthActive && isHeightActive && isRightPlatform;
221254
}
@@ -225,78 +258,40 @@ class Breakpoint {
225258
static Breakpoint? maybeActiveBreakpointFromSlotLayout(BuildContext context) {
226259
final SlotLayout? slotLayout =
227260
context.findAncestorWidgetOfExactType<SlotLayout>();
228-
Breakpoint? fallbackBreakpoint;
229-
230-
if (slotLayout != null) {
231-
for (final MapEntry<Breakpoint, SlotLayoutConfig?> config
232-
in slotLayout.config.entries) {
233-
if (config.key.isActive(context)) {
234-
if (config.key.platform != null) {
235-
return config.key;
236-
} else {
237-
fallbackBreakpoint ??= config.key;
238-
}
239-
}
240-
}
241-
}
242-
return fallbackBreakpoint;
261+
262+
return slotLayout != null
263+
? activeBreakpointIn(context, slotLayout.config.keys.toList())
264+
: null;
243265
}
244266

245267
/// Returns the default [Breakpoint] based on the [BuildContext].
246268
static Breakpoint defaultBreakpointOf(BuildContext context) {
247-
final TargetPlatform host = Theme.of(context).platform;
248-
final bool isDesktop = Breakpoint.desktop.contains(host);
249-
final bool isMobile = Breakpoint.mobile.contains(host);
250-
251-
for (final Breakpoint breakpoint in <Breakpoint>[
252-
Breakpoints.small,
253-
Breakpoints.medium,
254-
Breakpoints.mediumLarge,
255-
Breakpoints.large,
256-
Breakpoints.extraLarge,
257-
]) {
258-
if (breakpoint.isActive(context)) {
259-
if (isDesktop) {
260-
switch (breakpoint) {
261-
case Breakpoints.small:
262-
return Breakpoints.smallDesktop;
263-
case Breakpoints.medium:
264-
return Breakpoints.mediumDesktop;
265-
case Breakpoints.mediumLarge:
266-
return Breakpoints.mediumLargeDesktop;
267-
case Breakpoints.large:
268-
return Breakpoints.largeDesktop;
269-
case Breakpoints.extraLarge:
270-
return Breakpoints.extraLargeDesktop;
271-
default:
272-
return Breakpoints.standard;
273-
}
274-
} else if (isMobile) {
275-
switch (breakpoint) {
276-
case Breakpoints.small:
277-
return Breakpoints.smallMobile;
278-
case Breakpoints.medium:
279-
return Breakpoints.mediumMobile;
280-
case Breakpoints.mediumLarge:
281-
return Breakpoints.mediumLargeMobile;
282-
case Breakpoints.large:
283-
return Breakpoints.largeMobile;
284-
case Breakpoints.extraLarge:
285-
return Breakpoints.extraLargeMobile;
286-
default:
287-
return Breakpoints.standard;
288-
}
289-
} else {
290-
return breakpoint;
291-
}
292-
}
293-
}
294-
return Breakpoints.standard;
269+
return activeBreakpointIn(context, Breakpoints.all) ?? Breakpoints.standard;
295270
}
296271

297272
/// Returns the currently active [Breakpoint].
298273
static Breakpoint activeBreakpointOf(BuildContext context) {
299274
return maybeActiveBreakpointFromSlotLayout(context) ??
300275
defaultBreakpointOf(context);
301276
}
277+
278+
/// Returns the currently active [Breakpoint] based on the [BuildContext] and
279+
/// a list of [Breakpoint]s.
280+
static Breakpoint? activeBreakpointIn(
281+
BuildContext context, List<Breakpoint> breakpoints) {
282+
Breakpoint? currentBreakpoint;
283+
284+
for (final Breakpoint breakpoint in breakpoints) {
285+
if (breakpoint.isActive(context)) {
286+
if (breakpoint.platform != null) {
287+
// Prioritize platform-specific breakpoints.
288+
return breakpoint;
289+
} else {
290+
// Fallback to non-platform-specific.
291+
currentBreakpoint = breakpoint;
292+
}
293+
}
294+
}
295+
return currentBreakpoint;
296+
}
302297
}

packages/flutter_adaptive_scaffold/lib/src/slot_layout.dart

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,11 @@ class SlotLayout extends StatefulWidget {
1919
/// be chosen from the config under the context's conditions.
2020
static SlotLayoutConfig? pickWidget(
2121
BuildContext context, Map<Breakpoint, SlotLayoutConfig?> config) {
22-
SlotLayoutConfig? chosenWidget;
23-
24-
for (final Breakpoint breakpoint in config.keys) {
25-
if (breakpoint.isActive(context)) {
26-
final SlotLayoutConfig? pickedWidget = config[breakpoint];
27-
if (pickedWidget != null) {
28-
if (breakpoint.platform != null) {
29-
// Prioritize platform-specific breakpoints.
30-
return pickedWidget;
31-
} else {
32-
// Fallback to non-platform-specific.
33-
chosenWidget = pickedWidget;
34-
}
35-
} else {
36-
chosenWidget = null;
37-
}
38-
}
39-
}
40-
41-
return chosenWidget;
22+
final Breakpoint? breakpoint =
23+
Breakpoint.activeBreakpointIn(context, config.keys.toList());
24+
return breakpoint != null && config.containsKey(breakpoint)
25+
? config[breakpoint]
26+
: null;
4227
}
4328

4429
/// Maps [Breakpoint]s to [SlotLayoutConfig]s to determine what Widget to

packages/flutter_adaptive_scaffold/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_adaptive_scaffold
22
description: Widgets to easily build adaptive layouts, including navigation elements.
3-
version: 0.2.1
3+
version: 0.2.2
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_adaptive_scaffold%22
55
repository: https://github.com/flutter/packages/tree/main/packages/flutter_adaptive_scaffold
66

0 commit comments

Comments
 (0)