Skip to content

Commit bc13e9e

Browse files
author
Jens Becker
committed
feat(context_extensions): add byFormFactor<T>
1 parent b50992f commit bc13e9e

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ All features with links to their page in the documentation:
9999
Returns the correct [FormFactor](https://pub.dev/documentation/fleasy/latest/fleasy/FormFactor-class.html) based on the [FormFactorBreakpoints](https://pub.dev/documentation/fleasy/latest/fleasy/FormFactorBreakpoints-class.html).
100100
- #### [screenSize](https://pub.dev/documentation/fleasy/latest/fleasy/AdpativeContextExtensions/screenSize.html)
101101
Returns the correct [ScreenSize](https://pub.dev/documentation/fleasy/latest/fleasy/ScreenSize-class.html) based on the [FormFactorBreakpoints](https://pub.dev/documentation/fleasy/latest/fleasy/FormFactorBreakpoints-class.html).
102-
102+
- #### [byFormFactor<T>](https://pub.dev/documentation/fleasy/latest/fleasy/AdpativeContextExtensions/byFormFactor.html)
103+
Returns onWatch, onHandset, onTablet or onDesktop depending on the current [formFactor](https://pub.dev/documentation/fleasy/latest/fleasy/AdpativeContextExtensions/formFactor.html).
104+
Example:
105+
```dart
106+
double width = context.byFormFactor<double>(onHandset: 10, onTablet: 25, onDesktop: 50);
107+
```
103108
104109
- ### Extensions on `BuildContext` (Navigation helpers):
105110
- #### [pushPage(...)](https://pub.dev/documentation/fleasy/latest/fleasy/NavigationContextExtensions/pushPage.html)

lib/src/extensions/context_extensions.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,27 @@ extension AdpativeContextExtensions on BuildContext {
9393
? ScreenSize.normal
9494
: ScreenSize.small;
9595
}
96+
97+
/// Returns onWatch, onHandset, onTablet or onDesktop depending on the current [formFactor].
98+
///
99+
/// Example:
100+
/// ```dart
101+
/// double width = context.byFormFactor<double>(onHandset: 10, onTablet: 25, onDesktop: 50);
102+
/// ```
103+
T byFormFactor<T>({
104+
T? onWatch,
105+
required T onHandset,
106+
required T onTablet,
107+
required T onDesktop,
108+
}) {
109+
return formFactor.isWatch
110+
? onWatch ?? onHandset
111+
: formFactor.isHandset
112+
? onHandset
113+
: formFactor.isTablet
114+
? onTablet
115+
: onDesktop;
116+
}
96117
}
97118

98119
extension NavigationContextExtensions on BuildContext {

0 commit comments

Comments
 (0)