Conversation

TheCarpetMerchant

This PR is not ready to be merged. I made it as best as I could while trying to learn the inner workings of this beautiful package :)

Hopefully, it's not too shabby. The use of shrinkWrap should probably be removed in favor of understanding what's going wrong in the _calculateUsedMargins, but I haven't even tried given shrinkWrap fixes it (by bypassing the issue), at least AFAIK.

@Rockets1080

image

  1. It is necessary to determine whether the current style width is greater than the screen width;
  2. Keep the aspect ratio of the picture, otherwise the picture will be stretched and distorted
    image
    Where used set like this.

@eEQK

This PR is not ready to be merged. I made it as best as I could while trying to learn the inner workings of this beautiful package :)

@TheCarpetMerchant Do you intend to finish it or would you prefer someone to take over?

@TheCarpetMerchant

@eEQK Would prefer someone at least look it over. I didn't even run tests because my local setup kept erroring out ; it just works for my use case as is. If what I did is enough then great, but it would still be better someone take it over, especially to separate the 2 commits which are really two different things.

Comment on lines 62 to 76
Width? maxWidthCalculated;
if (style.maxWidth != null && style.width != null) {
if (style.maxWidth!.unit == Unit.percent &&
style.width!.unit == Unit.px) {
// If our max is a percentage, we want to look at the size available and not be bigger than that.
try {
double width =
MediaQuery.of(context).size.width * (style.maxWidth!.value / 100);
maxWidthCalculated = Width(width, style.width!.unit);
} catch (_) {}
} else if (style.width!.unit == style.maxWidth!.unit &&
style.width!.value > style.maxWidth!.value) {
maxWidthCalculated = Width(style.maxWidth!.value, style.maxWidth!.unit);
}
}
Copy link

@eEQK eEQK Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be extracted to a method called calculateMaxWidth and then you'd have final maxWidthCalculated = _calculateMaxWidth(style);

this way you don't have to use a var (finals should be used whenever possible)

double width =
MediaQuery.of(context).size.width * (style.maxWidth!.value / 100);
maxWidthCalculated = Width(width, style.width!.unit);
} catch (_) {}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

silent errors are generally not a good idea - you should print the exception with a stacktrace at the very least (if this code happens to fail, you won't know unless you debug carefully the whole flow)

@@ -59,8 +59,24 @@ class CssBoxWidget extends StatelessWidget {
final direction = _checkTextDirection(context, textDirection);
final padding = style.padding?.resolve(direction);

Width? maxWidthCalculated;
if (style.maxWidth != null && style.width != null) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (style.maxWidth != null && style.width != null) {
final maxWidth = style.maxWidth;
final width = style.width;
if (maxWidth != null && width != null) {

this way you don't have to use the bang operator below (style.width!), just use width

Copy link

@eEQK eEQK Sep 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, if it's not entirely clear what an if checks for, I really like to assign names to stuff I put inside if, e.g. instead of

    if (maxWidth != null && width != null) {

I would do

    final hasWidthConstraint = maxWidth != null && width != null;
    if(hasWidthConstraint)

but I'd say it's fine to leave the one below as it is since it's pretty self-explanatory what you're trying to do there

if (maxWidth.unit == Unit.percent && width.unit == Unit.px)

maxWidthCalculated = Width(width, style.width!.unit);
} catch (_) {}
} else if (style.width!.unit == style.maxWidth!.unit &&
style.width!.value > style.maxWidth!.value) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here would also be a good idea to name what the condition checks for

@TheCarpetMerchant

@eEQK Finally back to it. What's missing for now it the ability to compare 2 Widths that aren't in the same unit, if you know how to do that on a sufficient "best effort" basis at least that'd be great.
Since there's now a bunch of commits and this is 2 separate things in one PR (maxWidth and the BoxFit.contain change), I can close this PR and open 2 others if you think it's better for the commit history.

@utkarshmarwaha

Adding
fit: BoxFit.contain,
to lib/src/builtins/image_builtin.dart

and using the width property in HTML() as follows:
'img': Style( width: Width( MediaQuery.of(context).size.width, Unit.auto, ), )

This give image width a good size but shows empty white space above and below the image.
Something that can be recommended?

@pranavo72bex

'img': Style( width: Width( MediaQuery.of(context).size.width, Unit.auto, ), )

Any update on this issue?

 Html(
                  shrinkWrap: true,
                  data: widget.content,
                  onLinkTap: (url, _, __) {
                    handleLinkTap(url, _, __, _);
                  },
                  style: {
                    "body": Style(
                      margin: Margins.zero,
                      padding: HtmlPaddings.symmetric(horizontal: 0),
                    ),
                    'img': Style(
                      width: Width(100, Unit.px),
                    )
                  }),

unable to render image

@codecovCodecov

Codecov Report

Attention: coverage is 26.31579% with 14 lines in your changes missing coverage. Please review.

Project coverage is 63.83%. Comparing base (2814407) to head (db25c35).

Files with missing lines%Lines
lib/src/css_box_widget.dart25.00%9 Missing⚠️
lib/src/css_parser.dart0.00%5 Missing⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1359      +/-   ##
==========================================
- Coverage   64.08%   63.83%   -0.25%     
==========================================
  Files          39       39              
  Lines        3032     3050      +18     
==========================================
+ Hits         1943     1947       +4     
- Misses       1089     1103      +14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on . Already have an account? Sign in to comment
None yet
Status: Todo

Successfully merging this pull request may close these issues.