Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flutter_adaptive_scaffold] Fix landscape not showing in andUp #7425

Merged
merged 8 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})
martijn00 marked this conversation as resolved.
Show resolved Hide resolved
: 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 any height above the set height.
martijn00 marked this conversation as resolved.
Show resolved Hide resolved
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
Renzo-Olivares marked this conversation as resolved.
Show resolved Hide resolved
martijn00 marked this conversation as resolved.
Show resolved Hide resolved
? 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