forked from vandadnp/flutter-tips-and-tricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-aspect-ratio-in-flutter.dart
47 lines (42 loc) · 1.58 KB
/
image-aspect-ratio-in-flutter.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 🐦 Twitter https://twitter.com/vandadnp
// 🔵 LinkedIn https://linkedin.com/in/vandadnp
// 🎥 YouTube https://youtube.com/c/vandadnp
// 💙 Free Flutter Course https://linktr.ee/vandadnp
// 📦 11+ Hours Bloc Course https://youtu.be/Mn254cnduOY
// 🔶 7+ Hours MobX Course https://youtu.be/7Od55PBxYkI
// 🦄 8+ Hours RxSwift Coursde https://youtu.be/xBFWMYmm9ro
// 🤝 Want to support my work? https://buymeacoffee.com/vandad
import 'dart:io' show File;
import 'dart:typed_data' show Uint8List;
import 'dart:async' show Completer;
import 'package:flutter/material.dart' as material
show Image, ImageConfiguration, ImageStreamListener;
extension GetImageAspectRatio on material.Image {
Future<double> getAspectRatio() {
final completer = Completer<double>();
image.resolve(const material.ImageConfiguration()).addListener(
material.ImageStreamListener(
(imageInfo, synchronousCall) {
final aspectRatio = imageInfo.image.width / imageInfo.image.height;
imageInfo.image.dispose();
completer.complete(aspectRatio);
},
),
);
return completer.future;
}
}
extension GetImageDataAspectRatio on Uint8List {
Future<double> getAspectRatio() {
final image = material.Image.memory(this);
return image.getAspectRatio();
}
}
typedef FilePath = String;
extension GetImageFileAspectRatio on FilePath {
Future<double> getAspectRatio() {
final file = File(this);
final image = material.Image.file(file);
return image.getAspectRatio();
}
}