Skip to content

Commit

Permalink
[flutter_adaptive_scaffold] Fix landscape not showing in andUp (flutt…
Browse files Browse the repository at this point in the history
…er#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
  • Loading branch information
martijn00 authored Aug 21, 2024
1 parent 1ab1a71 commit 89154b3
Show file tree
Hide file tree
Showing 6 changed files with 529 additions and 93 deletions.
4 changes: 4 additions & 0 deletions packages/flutter_adaptive_scaffold/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.2

* Fix a bug where landscape would not show body when using `andUp`.

## 0.2.1

* Add `Breakpoint.activeBreakpointOf(context)` to find the currently active breakpoint.
Expand Down
133 changes: 64 additions & 69 deletions packages/flutter_adaptive_scaffold/lib/src/breakpoints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Breakpoints {
/// case that no other breakpoint is active.
///
/// It is active from a width of -1 dp to infinity.
static const Breakpoint standard = Breakpoint(beginWidth: -1);
static const Breakpoint standard = Breakpoint.standard();

/// A window whose width is less than 600 dp and greater than 0 dp.
static const Breakpoint small = Breakpoint.small();
Expand Down Expand Up @@ -86,6 +86,30 @@ class Breakpoints {
/// A mobile window whose width is greater than 1600 dp.
static const Breakpoint extraLargeMobile =
Breakpoint.extraLarge(platform: Breakpoint.mobile);

/// A list of all the standard breakpoints.
static const List<Breakpoint> all = <Breakpoint>[
smallDesktop,
smallMobile,
small,
mediumDesktop,
mediumMobile,
medium,
mediumLargeDesktop,
mediumLargeMobile,
mediumLarge,
largeDesktop,
largeMobile,
large,
extraLargeDesktop,
extraLargeMobile,
extraLarge,
smallAndUp,
mediumAndUp,
mediumLargeAndUp,
largeAndUp,
standard,
];
}

/// A class to define the conditions that distinguish between types of
Expand Down Expand Up @@ -113,10 +137,19 @@ class Breakpoint {
this.endWidth,
this.beginHeight,
this.endHeight,
this.platform,
this.andUp = false,
this.platform,
});

/// Returns a [Breakpoint] that can be used as a fallthrough in the
/// case that no other breakpoint is active.
const Breakpoint.standard({this.platform})
: beginWidth = -1,
endWidth = null,
beginHeight = null,
endHeight = null,
andUp = true;

/// Returns a [Breakpoint] with the given constraints for a small screen.
const Breakpoint.small({this.andUp = false, this.platform})
: beginWidth = 0,
Expand Down Expand Up @@ -166,7 +199,7 @@ class Breakpoint {
TargetPlatform.iOS,
};

/// When set to true, it will include any size above the set width.
/// When set to true, it will include any size above the set width and set height.
final bool andUp;

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

final bool isHeightActive = isDesktop ||
orientation == Orientation.portrait ||
(orientation == Orientation.landscape &&
height >= lowerBoundHeight &&
height < upperBoundHeight);
(orientation == Orientation.landscape && andUp
? isWidthActive || height >= lowerBoundHeight
: height >= lowerBoundHeight && height < upperBoundHeight);

return isWidthActive && isHeightActive && isRightPlatform;
}
Expand All @@ -225,78 +258,40 @@ class Breakpoint {
static Breakpoint? maybeActiveBreakpointFromSlotLayout(BuildContext context) {
final SlotLayout? slotLayout =
context.findAncestorWidgetOfExactType<SlotLayout>();
Breakpoint? fallbackBreakpoint;

if (slotLayout != null) {
for (final MapEntry<Breakpoint, SlotLayoutConfig?> config
in slotLayout.config.entries) {
if (config.key.isActive(context)) {
if (config.key.platform != null) {
return config.key;
} else {
fallbackBreakpoint ??= config.key;
}
}
}
}
return fallbackBreakpoint;

return slotLayout != null
? activeBreakpointIn(context, slotLayout.config.keys.toList())
: null;
}

/// Returns the default [Breakpoint] based on the [BuildContext].
static Breakpoint defaultBreakpointOf(BuildContext context) {
final TargetPlatform host = Theme.of(context).platform;
final bool isDesktop = Breakpoint.desktop.contains(host);
final bool isMobile = Breakpoint.mobile.contains(host);

for (final Breakpoint breakpoint in <Breakpoint>[
Breakpoints.small,
Breakpoints.medium,
Breakpoints.mediumLarge,
Breakpoints.large,
Breakpoints.extraLarge,
]) {
if (breakpoint.isActive(context)) {
if (isDesktop) {
switch (breakpoint) {
case Breakpoints.small:
return Breakpoints.smallDesktop;
case Breakpoints.medium:
return Breakpoints.mediumDesktop;
case Breakpoints.mediumLarge:
return Breakpoints.mediumLargeDesktop;
case Breakpoints.large:
return Breakpoints.largeDesktop;
case Breakpoints.extraLarge:
return Breakpoints.extraLargeDesktop;
default:
return Breakpoints.standard;
}
} else if (isMobile) {
switch (breakpoint) {
case Breakpoints.small:
return Breakpoints.smallMobile;
case Breakpoints.medium:
return Breakpoints.mediumMobile;
case Breakpoints.mediumLarge:
return Breakpoints.mediumLargeMobile;
case Breakpoints.large:
return Breakpoints.largeMobile;
case Breakpoints.extraLarge:
return Breakpoints.extraLargeMobile;
default:
return Breakpoints.standard;
}
} else {
return breakpoint;
}
}
}
return Breakpoints.standard;
return activeBreakpointIn(context, Breakpoints.all) ?? Breakpoints.standard;
}

/// Returns the currently active [Breakpoint].
static Breakpoint activeBreakpointOf(BuildContext context) {
return maybeActiveBreakpointFromSlotLayout(context) ??
defaultBreakpointOf(context);
}

/// Returns the currently active [Breakpoint] based on the [BuildContext] and
/// a list of [Breakpoint]s.
static Breakpoint? activeBreakpointIn(
BuildContext context, List<Breakpoint> breakpoints) {
Breakpoint? currentBreakpoint;

for (final Breakpoint breakpoint in breakpoints) {
if (breakpoint.isActive(context)) {
if (breakpoint.platform != null) {
// Prioritize platform-specific breakpoints.
return breakpoint;
} else {
// Fallback to non-platform-specific.
currentBreakpoint = breakpoint;
}
}
}
return currentBreakpoint;
}
}
25 changes: 5 additions & 20 deletions packages/flutter_adaptive_scaffold/lib/src/slot_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,11 @@ class SlotLayout extends StatefulWidget {
/// be chosen from the config under the context's conditions.
static SlotLayoutConfig? pickWidget(
BuildContext context, Map<Breakpoint, SlotLayoutConfig?> config) {
SlotLayoutConfig? chosenWidget;

for (final Breakpoint breakpoint in config.keys) {
if (breakpoint.isActive(context)) {
final SlotLayoutConfig? pickedWidget = config[breakpoint];
if (pickedWidget != null) {
if (breakpoint.platform != null) {
// Prioritize platform-specific breakpoints.
return pickedWidget;
} else {
// Fallback to non-platform-specific.
chosenWidget = pickedWidget;
}
} else {
chosenWidget = null;
}
}
}

return chosenWidget;
final Breakpoint? breakpoint =
Breakpoint.activeBreakpointIn(context, config.keys.toList());
return breakpoint != null && config.containsKey(breakpoint)
? config[breakpoint]
: null;
}

/// Maps [Breakpoint]s to [SlotLayoutConfig]s to determine what Widget to
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_adaptive_scaffold/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_adaptive_scaffold
description: Widgets to easily build adaptive layouts, including navigation elements.
version: 0.2.1
version: 0.2.2
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_adaptive_scaffold%22
repository: https://github.com/flutter/packages/tree/main/packages/flutter_adaptive_scaffold

Expand Down
Loading

0 comments on commit 89154b3

Please sign in to comment.