Skip to content

Commit

Permalink
feat: constrain image height to device height if image height is larg…
Browse files Browse the repository at this point in the history
…er than device height
  • Loading branch information
hjiangsu committed Feb 21, 2025
1 parent 162290c commit e5d885d
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions lib/shared/media_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,30 @@ class _MediaViewState extends State<MediaView> with TickerProviderStateMixin {
);
}

double getMinHeight() {
if (!widget.showFullHeightImages) return ViewMode.comfortable.height;

if (widget.postViewMedia.media.first.height != null) {
if (MediaQuery.of(context).size.height < widget.postViewMedia.media.first.height!) return MediaQuery.of(context).size.height;
return widget.postViewMedia.media.first.height!;
}

return ViewMode.comfortable.height;
}

double getMaxHeight() {
if (!widget.showFullHeightImages) return ViewMode.comfortable.height;

if (widget.postViewMedia.media.first.height != null) {
if (MediaQuery.of(context).size.height < widget.postViewMedia.media.first.height!) return MediaQuery.of(context).size.height;
return widget.postViewMedia.media.first.height!;
}

if (widget.allowUnconstrainedImageHeight) return MediaQuery.of(context).size.height;

return ViewMode.comfortable.height;
}

/// Creates an image preview
Widget buildMediaImage() {
final theme = Theme.of(context);
Expand All @@ -201,13 +225,11 @@ class _MediaViewState extends State<MediaView> with TickerProviderStateMixin {
constraints: BoxConstraints(
maxHeight: switch (widget.viewMode) {
ViewMode.compact => ViewMode.compact.height,
ViewMode.comfortable => widget.showFullHeightImages
? widget.postViewMedia.media.first.height ?? (widget.allowUnconstrainedImageHeight ? double.infinity : ViewMode.comfortable.height)
: ViewMode.comfortable.height,
ViewMode.comfortable => getMaxHeight(),
},
minHeight: switch (widget.viewMode) {
ViewMode.compact => ViewMode.compact.height,
ViewMode.comfortable => widget.showFullHeightImages ? widget.postViewMedia.media.first.height ?? ViewMode.comfortable.height : ViewMode.comfortable.height,
ViewMode.comfortable => getMinHeight(),
},
maxWidth: switch (widget.viewMode) {
ViewMode.compact => ViewMode.compact.height,
Expand Down Expand Up @@ -305,13 +327,11 @@ class _MediaViewState extends State<MediaView> with TickerProviderStateMixin {
constraints: BoxConstraints(
maxHeight: switch (widget.viewMode) {
ViewMode.compact => ViewMode.compact.height,
ViewMode.comfortable => widget.showFullHeightImages
? widget.postViewMedia.media.first.height ?? (widget.allowUnconstrainedImageHeight ? double.infinity : ViewMode.comfortable.height)
: ViewMode.comfortable.height,
ViewMode.comfortable => getMaxHeight(),
},
minHeight: switch (widget.viewMode) {
ViewMode.compact => ViewMode.compact.height,
ViewMode.comfortable => widget.showFullHeightImages ? widget.postViewMedia.media.first.height ?? ViewMode.comfortable.height : ViewMode.comfortable.height,
ViewMode.comfortable => getMinHeight(),
},
maxWidth: switch (widget.viewMode) {
ViewMode.compact => ViewMode.compact.height,
Expand Down

0 comments on commit e5d885d

Please sign in to comment.