-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[Web] fix Image fit when using ImgElementPlatformView #164400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ import 'platform_view.dart'; | |
/// Displays an `<img>` element with `src` set to [src]. | ||
class ImgElementPlatformView extends StatelessWidget { | ||
/// Creates a platform view backed with an `<img>` element. | ||
ImgElementPlatformView(this.src, {super.key}) { | ||
ImgElementPlatformView(this.src, {this.fit, super.key}) { | ||
if (!_registered) { | ||
_register(); | ||
} | ||
|
@@ -39,21 +39,30 @@ class ImgElementPlatformView extends StatelessWidget { | |
// without fetching it over the network again. | ||
final web.HTMLImageElement img = web.document.createElement('img') as web.HTMLImageElement; | ||
img.src = paramsMap['src']! as String; | ||
img.style.width = '100%'; | ||
img.style.height = '100%'; | ||
final String? fit = paramsMap['fit'] as String?; | ||
if (fit != null) { | ||
img.style.objectFit = fit; | ||
} | ||
return img; | ||
}); | ||
} | ||
|
||
/// The `src` URL for the `<img>` tag. | ||
final String? src; | ||
|
||
/// The `object-fit` CSS property for the `<img>` tag. | ||
final web.CSSObjectFit? fit; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain why the new type |
||
|
||
@override | ||
Widget build(BuildContext context) { | ||
if (src == null) { | ||
return const SizedBox.expand(); | ||
} | ||
return HtmlElementView( | ||
viewType: _viewType, | ||
creationParams: <String, String?>{'src': src}, | ||
creationParams: <String, String?>{'src': src, 'fit': fit?.name}, | ||
hitTestBehavior: PlatformViewHitTestBehavior.transparent, | ||
); | ||
} | ||
|
@@ -72,7 +81,7 @@ class RawWebImage extends SingleChildRenderObjectWidget { | |
this.fit, | ||
this.alignment = Alignment.center, | ||
this.matchTextDirection = false, | ||
}) : super(child: ImgElementPlatformView(image.htmlImage.src)); | ||
}) : super(child: ImgElementPlatformView(image.htmlImage.src, fit: fit?.cssObjectFit)); | ||
|
||
/// The underlying HTML element to be displayed. | ||
final WebImageInfo image; | ||
|
@@ -371,3 +380,20 @@ class RenderWebImage extends RenderShiftedBox { | |
); | ||
} | ||
} | ||
|
||
extension on BoxFit { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extensions are not something we allow in the framework, this is in the style guide here: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md#avoid-using-extension There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Especially since this can easily be replaced by a global function. :) |
||
web.CSSObjectFit get cssObjectFit { | ||
switch (this) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not enforced but the pattern matching statement is the fashion for you to consider: return switch(this) {
BoxFit.fill => web.CSSObjectFit.fill,
...
}; |
||
case BoxFit.contain: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these values all mapped to |
||
case BoxFit.cover: | ||
case BoxFit.fitWidth: | ||
case BoxFit.fitHeight: | ||
case BoxFit.scaleDown: | ||
return web.CSSObjectFit.cover; | ||
case BoxFit.fill: | ||
return web.CSSObjectFit.fill; | ||
case BoxFit.none: | ||
return web.CSSObjectFit.none; | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.