Skip to content

Commit

Permalink
Add breakpoint extension to get the current active breakpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
martijn00 committed Aug 13, 2024
1 parent 069ce4d commit dad42f0
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions packages/flutter_adaptive_scaffold/lib/src/breakpoints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,49 @@ class Breakpoint {
return isWidthActive && isHeightActive && isRightPlatform;
}
}

/// An extension on [BuildContext] that returns the active [Breakpoint] based on
/// an available [SlotLayout] ancestor or the default breakpoints.
extension BreakpointExtension on BuildContext {
/// Returns the currently active [Breakpoint].
Breakpoint get activeBreakpoint {
final SlotLayout? slotLayout = findAncestorWidgetOfExactType<SlotLayout>();
if (slotLayout != null) {
for (final MapEntry<Breakpoint, SlotLayoutConfig?> config
in slotLayout.config.entries) {
if (config.key.isActive(this)) {
return config.key;
}
}
}

final TargetPlatform platform = Theme.of(this).platform;

if (Breakpoint.desktop.contains(platform)) {
if (Breakpoints.extraLarge.isActive(this)) {
return Breakpoints.extraLargeDesktop;
} else if (Breakpoints.large.isActive(this)) {
return Breakpoints.largeDesktop;
} else if (Breakpoints.mediumLarge.isActive(this)) {
return Breakpoints.mediumLargeDesktop;
} else if (Breakpoints.medium.isActive(this)) {
return Breakpoints.mediumDesktop;
} else if (Breakpoints.small.isActive(this)) {
return Breakpoints.smallDesktop;
}
} else if (Breakpoint.mobile.contains(platform)) {
if (Breakpoints.extraLarge.isActive(this)) {
return Breakpoints.extraLargeMobile;
} else if (Breakpoints.large.isActive(this)) {
return Breakpoints.largeMobile;
} else if (Breakpoints.mediumLarge.isActive(this)) {
return Breakpoints.mediumLargeMobile;
} else if (Breakpoints.medium.isActive(this)) {
return Breakpoints.mediumMobile;
} else if (Breakpoints.small.isActive(this)) {
return Breakpoints.smallMobile;
}
}
return Breakpoints.standard;
}
}

0 comments on commit dad42f0

Please sign in to comment.