Skip to content

The image aspect ratio is distorted when using the webHtmlElementStrategy property. #163288

Closed

Description

@mycalls

Steps to reproduce

(Flutter 3.29) The aspect ratio is distorted when using the prefer value for the webHtmlElementStrategy property in Stack > Positioned.fill > Image.network.

  1. Open the DartPad page.
  2. Select the beta channel.
  3. Run the code sample.

You can check the distorted images with incorrect aspect ratios.

Expected results

Images with correct aspect ratio.

Actual results

Images with distorted aspect ratio.

Code sample

Code sample
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(title: Text('Broken Images')),
        body: Center(child: BrokenImages()),
      ),
    );
  }
}

class BrokenImages extends StatelessWidget {
  const BrokenImages({super.key});

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        ...locations
            .map((item) => ImageItem(imageUrl: item.imageUrl, name: item.name))
            .expand((item) => [item, SizedBox(height: 12)]),
      ],
    );
  }
}

class ImageItem extends StatelessWidget {
  const ImageItem({super.key, required this.imageUrl, required this.name});

  final String imageUrl;
  final String name;

  @override
  Widget build(BuildContext context) {
    return AspectRatio(
      aspectRatio: 9 / 16,
      child: Stack(
        children: [
          Positioned.fill(
            child: Image.network(
              imageUrl,
              fit: BoxFit.cover,
              webHtmlElementStrategy: WebHtmlElementStrategy.prefer,
            ),
          ),
          Positioned(
            left: 16,
            top: 24,
            child: Text(
              style: Theme.of(
                context,
              ).textTheme.headlineMedium?.copyWith(color: Colors.white),
              name,
            ),
          ),
        ],
      ),
    );
  }
}

class Location {
  const Location({
    required this.name,
    required this.place,
    required this.imageUrl,
  });

  final String name;
  final String place;
  final String imageUrl;
}

const urlPrefix =
    'https://docs.flutter.dev/cookbook/img-files/effects/parallax';
const locations = [
  Location(
    name: 'Mount Rushmore',
    place: 'U.S.A',
    imageUrl: '$urlPrefix/01-mount-rushmore.jpg',
  ),
  Location(
    name: 'Gardens By The Bay',
    place: 'Singapore',
    imageUrl: '$urlPrefix/02-singapore.jpg',
  ),
  Location(
    name: 'Machu Picchu',
    place: 'Peru',
    imageUrl: '$urlPrefix/03-machu-picchu.jpg',
  ),
  Location(
    name: 'Vitznau',
    place: 'Switzerland',
    imageUrl: '$urlPrefix/04-vitznau.jpg',
  ),
  Location(
    name: 'Bali',
    place: 'Indonesia',
    imageUrl: '$urlPrefix/05-bali.jpg',
  ),
  Location(
    name: 'Mexico City',
    place: 'Mexico',
    imageUrl: '$urlPrefix/06-mexico-city.jpg',
  ),
  Location(name: 'Cairo', place: 'Egypt', imageUrl: '$urlPrefix/07-cairo.jpg'),
];

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
Set `style.height` to any appropriate value to stop this message.
Width of Platform View type: [Flutter__ImgElementImage__] may not be set. Defaulting to `width: 100%`.

Set `style.width` to any appropriate value to stop this message.
Height of Platform View type: [Flutter__ImgElementImage__] may not be set. Defaulting to `height: 100%`.

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.29.0, on macOS 15.2 24C101 darwin-arm64, locale ko-KR)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.2)
[✓] VS Code (version 1.97.1)
[✓] Connected device (3 available)
[✓] Network resources

Activity

  1. felipendc commented on Feb 14, 2025

    @felipendc

    I'm also facing this same problem when I close a windows which has a text filed, when the window (screen) is closed the image gets all distorted on Flutter 3.29.0 on Windows 11 x64

  2. darshankawar commented on Feb 17, 2025

    @darshankawar
    Member

    @mycalls Thanks for the report. What's the expected result upon running the code ? Did it start to occur after upgrading to 3.29 ? Can you provide us correct results before it started to show distorted ones ?

  3. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 17, 2025
  4. mycalls commented on Feb 17, 2025

    @mycalls
    Author

    @darshankawar
    Thank you for your response. I apologize for the lack of information. So I have edited Steps to reproduce.
    This issue occurs in version 3.29. If you run the code sample in DartPad's beta channel, you can see the distorted images with incorrect aspect ratios.

  5. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 17, 2025
  6. darshankawar commented on Feb 18, 2025

    @darshankawar
    Member

    Thanks for the update. I do see the same distorted behavior. Since webHtmlElementStrategy: WebHtmlElementStrategy.prefer, is used whose description reads whether to display images in HTML elements. Could it be related to the html renderer being deprecated ?

    stable : 3.29.0
    master : 3.30.0-1.0.pre.191
    
  7. added
    a: imagesLoading, displaying, rendering images
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    frameworkflutter/packages/flutter repository. See also f: labels.
    and removed
    in triagePresently being triaged by the triage team
    on Feb 18, 2025
  8. mycalls commented on Feb 18, 2025

    @mycalls
    Author

    @darshankawar
    Thanks for your check. I don't believe it's related to the HTML renderer being deprecated. Based on #149843, #157755, #159917 , I suspect it's related to the CORS configuration for images with CanvasKit and Skwasm.

  9. 8 remaining items

  10. added
    P1High-priority issues at the top of the work list
    and removed
    P2Important issues not at the top of the work list
    on Jul 2, 2025
  11. harryterkelsen commented on Jul 9, 2025

    @harryterkelsen
    Contributor

    I have a fix which seems to work. We were improperly sizing the <img> element before. Now I just need to add tests and I'll upload the PR.

    Image
  12. added
    r: fixedIssue is closed as already fixed in a newer version
    on Jul 17, 2025
  13. github-actions commented on Jul 31, 2025

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  14. locked as resolved and limited conversation to collaborators on Jul 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P1High-priority issues at the top of the work lista: imagesLoading, displaying, rendering imagescustomer: azulefound in release: 3.29Found to occur in 3.29found in release: 3.30Found to occur in 3.30frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-webWeb applications specificallyr: fixedIssue is closed as already fixed in a newer versionteam-webOwned by Web platform teamtriaged-webTriaged by Web platform team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions