Skip to content

Commit

Permalink
feat: add extension on build context (#1)
Browse files Browse the repository at this point in the history
* feat: add extension method
* docs: add new feature on the docs
  • Loading branch information
omariosouto committed Oct 9, 2022
1 parent 0d84dc8 commit a63b914
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -95,9 +95,9 @@ yarn add @displaykit/responsive_styles

| Feature. | Dart/Flutter | JavaScript/React |
| --- | --- | --- |
| [Responsive](./lib/responsive//responsive.md) |||
| [Responsive](./lib/responsive/responsive.md) |||
| [breakpoints](./lib/breakpoints/breakpoints.md) |||
| [GridItem](./lib/grid-item//grid_item.md) || 🚧 |
| [GridItem](./lib/grid-item/grid_item.md) || 🚧 |
| BreakpointsProvider | 🚧 | 🚧 |
| parseStylesheet | 🚧 ||
| StyleSheet | 🚧 ||
Expand Down
4 changes: 4 additions & 0 deletions lib/responsive/responsive.dart
Expand Up @@ -95,3 +95,7 @@ class Responsive {
return resolveValueForBreakpoint(value, activeBreakpoint);
}
}

extension ResponsiveStyles on BuildContext {
Responsive get responsive => Responsive(this);
}
18 changes: 18 additions & 0 deletions lib/responsive/responsive.md
Expand Up @@ -41,6 +41,24 @@ var outputValue = responsive.screenWidth.value; // 1000
var outputValue = responsive.screenWidth.percent(40); // 400
```

- You can also use responsive directlu through the Flutter build context:
```dart
class WidgetWithResponsiveOutputByBuildContext extends StatelessWidget {
const WidgetWithResponsiveOutputByBuildContext({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
var outputValue = context.responsive.value({
Breakpoints.xs: 'xs',
Breakpoints.sm: 'sm',
Breakpoints.md: 'md',
Breakpoints.lg: 'lg',
Breakpoints.xl: 'xl',
});
return Text("Screen: $outputValue");
}
}
```


### JavaScript

Expand Down
16 changes: 16 additions & 0 deletions test/responsive/responsive_test.dart
Expand Up @@ -18,6 +18,22 @@ class WidgetWithResponsiveOutput extends StatelessWidget {
}
}

// TODO: Add tests on top of this
class WidgetWithResponsiveOutputByBuildContext extends StatelessWidget {
const WidgetWithResponsiveOutputByBuildContext({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
var outputValue = context.responsive.value({
Breakpoints.xs: 'xs',
Breakpoints.sm: 'sm',
Breakpoints.md: 'md',
Breakpoints.lg: 'lg',
Breakpoints.xl: 'xl',
});
return Text("Screen: $outputValue");
}
}

class WidgetWithResponsiveScreenWidthPercentageAccess extends StatelessWidget {
final double percent;
const WidgetWithResponsiveScreenWidthPercentageAccess(
Expand Down

0 comments on commit a63b914

Please sign in to comment.