Skip to content

Commit

Permalink
[adaptive_style] add DeviceSize and mostSimilarTo
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Iapicca committed Apr 20, 2024
1 parent 8b4dd23 commit a5a48dc
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/adaptive_style/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
build/
3 changes: 3 additions & 0 deletions packages/adaptive_style/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* TODO: Describe initial release.
1 change: 1 addition & 0 deletions packages/adaptive_style/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
39 changes: 39 additions & 0 deletions packages/adaptive_style/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.

## Usage

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.

```dart
const like = 'sample';
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
4 changes: 4 additions & 0 deletions packages/adaptive_style/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
1 change: 1 addition & 0 deletions packages/adaptive_style/lib/adaptive_style.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
library adaptive_style;
27 changes: 27 additions & 0 deletions packages/adaptive_style/lib/src/device_size.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'dart:ui';

/// a collection of well known screen dimesions
/// as offered by [chrome device toolbar]
enum DeviceSize {
iphoneSE(Size(375, 667)),
iphoneXR(Size(414, 896)),
iphone12PRO(Size(390, 844)),
iphone14PRO(Size(430, 932)),
pixel7(Size(412, 915)),
galaxyS8Plus(Size(360, 740)),
galaxyS20Ultra(Size(412, 915)),
iPadMini(Size(768, 1024)),
iPadAir(Size(820, 1180)),
iPadPro(Size(1024, 1366)),
surfacePro7(Size(912, 1368)),
surfaceDuo(Size(540, 720)),
galaxyFold(Size(280, 653)),
galaxyA51(Size(412, 914)),
galaxyA71(Size(412, 914)),
nestHub(Size(1024, 600)),
nestHubMax(Size(1280, 800));

const DeviceSize(this.size);

final Size size;
}
42 changes: 42 additions & 0 deletions packages/adaptive_style/lib/src/extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'dart:math' as math show min, sqrt;
import 'dart:ui';

import 'device_size.dart';

extension DeviceSizeSizesX on List<DeviceSize> {
List<Size> get sizes => [for (final value in this) value.size];
}

extension FixedRatioSizeScaleX<T extends Size> on T {
double fixedRatioScale(T size) => math.min(
width / size.width,
height / size.height,
);
}

extension ReverseScaleX<T extends double> on T {
double get reversedRatio => 1 / this;
}

extension SizeAreaX<T extends Size> on T {
double get area => width * height;
}

extension MostSimilarSizeX<T extends Size> on Iterable<T> {
T mostSimilarTo(T size) {
final sizes = [
for (final element in this)
(
element,

/// this is arbitrary but seems to work
(size.aspectRatio - element.aspectRatio).abs() *
math.sqrt((size.area - element.area).abs())
)
]..sort(
(a, b) => a.$2.compareTo(b.$2),
);

return sizes.first.$1;
}
}
15 changes: 15 additions & 0 deletions packages/adaptive_style/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: adaptive_style
description: Make you Flutter UI both responsive and adaptive
version: 0.0.1
homepage: https://github.com/yakforward-ou/yak_packages

environment:
sdk: '>=3.3.3 <4.0.0'

dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^3.0.2

0 comments on commit a5a48dc

Please sign in to comment.