Skip to content

Commit

Permalink
Update Breakpoints Comparator API
Browse files Browse the repository at this point in the history
- Update API to match Conditionals API.
  • Loading branch information
rayliverified committed Apr 17, 2023
1 parent 03da742 commit 7f8b3a9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Use the labels you defined for layouts and values.

```dart
// Example: if the screen is bigger than the Mobile breakpoint, build full width AppBar icons and labels.
if (ResponsiveBreakpoints.of(context).isLargerThan(MOBILE))
if (ResponsiveBreakpoints.of(context).largerThan(MOBILE))
FullWidthAppBarItems()
// Booleans
Expand All @@ -70,8 +70,9 @@ ResponsiveBreakpoints.of(context).isPhone;
// Conditionals
ResponsiveBreakpoints.of(context).equals(DESKTOP)
ResponsiveBreakpoints.of(context).isLargerThan(MOBILE)
ResponsiveBreakpoints.of(context).isSmallerThan(TABLET)
ResponsiveBreakpoints.of(context).largerThan(MOBILE)
ResponsiveBreakpoints.of(context).smallerThan(TABLET)
...
```

### Customization
Expand All @@ -91,7 +92,7 @@ breakpoints: [

Then, in our code, set the value based on the breakpoint condition.

> expand: ResponsiveBreakpoints.of(context).isLargerThan('EXPAND_SIDE_PANEL')
> expand: ResponsiveBreakpoints.of(context).largerThan('EXPAND_SIDE_PANEL')
### Responsive Framework Widgets
The ResponsiveFramework includes a few custom widgets that to supplement Flutter's responsive capabilities. They are showcased in the demo projects.
Expand Down
38 changes: 34 additions & 4 deletions lib/responsive_breakpoints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,22 +332,52 @@ class ResponsiveBreakpointsData {
String toString() =>
'ResponsiveWrapperData(breakpoint: $breakpoint, breakpoints: ${breakpoints.asMap()}, isMobile: $isMobile, isPhone: $isPhone, isTablet: $isTablet, isDesktop: $isDesktop)';

bool equals(String? name) => breakpoint.name == name;
/// Returns if the active breakpoint is [name].
bool equals(String name) => breakpoint.name == name;

/// Is the width larger than or equal to [name]?
/// Is the [screenWidth] larger than [name]?
/// Defaults to false if the [name] cannot be found.
bool isLargerThan(String? name) =>
bool largerThan(String name) =>
screenWidth >
(breakpoints.firstWhereOrNull((element) => element.name == name)?.end ??
double.infinity);

/// Is the [screenWidth] larger than or equal to [name]?
/// Defaults to false if the [name] cannot be found.
bool largerOrEqualTo(String name) =>
screenWidth >=
(breakpoints.firstWhereOrNull((element) => element.name == name)?.start ??
double.infinity);

/// Is the [screenWidth] smaller than the [name]?
/// Defaults to false if the [name] cannot be found.
bool isSmallerThan(String? name) =>
bool smallerThan(String name) =>
screenWidth <
(breakpoints.firstWhereOrNull((element) => element.name == name)?.start ??
0);

/// Is the [screenWidth] smaller than or equal to the [name]?
/// Defaults to false if the [name] cannot be found.
bool smallerOrEqualTo(String name) =>
screenWidth <=
(breakpoints.firstWhereOrNull((element) => element.name == name)?.end ??
0);

/// Is the [screenWidth] smaller than or equal to the [name]?
/// Defaults to false if the [name] cannot be found.
bool between(String name, String name1) {
return (screenWidth >=
(breakpoints
.firstWhereOrNull((element) => element.name == name)
?.start ??
0) &&
screenWidth <=
(breakpoints
.firstWhereOrNull((element) => element.name == name1)
?.end ??
0));
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
Expand Down
4 changes: 2 additions & 2 deletions lib/responsive_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class ResponsiveValue<T> {

if (condition.condition == Conditional.SMALLER_THAN) {
if (condition.name != null) {
if (responsiveWrapperData.isSmallerThan(condition.name!)) {
if (responsiveWrapperData.smallerThan(condition.name!)) {
return condition;
}
}
Expand All @@ -116,7 +116,7 @@ class ResponsiveValue<T> {

if (condition.condition == Conditional.LARGER_THAN) {
if (condition.name != null) {
if (responsiveWrapperData.isLargerThan(condition.name!)) {
if (responsiveWrapperData.largerThan(condition.name!)) {
return condition;
}
}
Expand Down

0 comments on commit 7f8b3a9

Please sign in to comment.