Skip to content
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

Basic flex support #1021

Merged
merged 17 commits into from
Oct 27, 2023
Merged

Basic flex support #1021

merged 17 commits into from
Oct 27, 2023

Conversation

anttileppa
Copy link
Contributor

@anttileppa anttileppa commented Sep 8, 2023

Added an initial support for very simple flex divs. This does not support all flex features, instead it allows some really basic flex features like alignments, justifications and directions.

Related to #877

@daohoangson
Copy link
Owner

Thank you for your contribution. Do you have screenshots of HTML with flex and their rendering?

@anttileppa
Copy link
Contributor Author

Sure,

Using following HTML:

<h2>Horizontal Alignments</h2>
<!-- Flex Start (default horizontal alignment) -->

<div style="display: flex; width: 100%; height: 100px; background-color: lightgray; margin-bottom: 20px; align-items: flex-start;">
  <div>align-items: flex-start</div>
</div>

<!-- Flex Center -->
<div style="display: flex; width: 100%; height: 100px; background-color: lightgray; margin-bottom: 20px; align-items: center;">
  <div>align-items: center</div>
</div>

<!-- Flex End -->
<div style="display: flex; width: 100%; height: 100px; background-color: lightgray; margin-bottom: 20px; align-items: flex-end;">
  <div>align-items: flex-end</div>
</div>

<h2>Vertical Alignments</h2>

<!-- Flex Start (default vertical alignment) -->
<div style="display: flex; width: 300px; height: 100px; background-color: lightgray; margin-bottom: 20px; justify-content: flex-start;">
  <div>justify-content: flex-start</div>
</div>

<!-- Flex Center -->
<div style="display: flex; width: 300px; height: 100px; background-color: lightgray; margin-bottom: 20px; justify-content: center;">
  <div>justify-content: center</div>
</div>

<!-- Flex End -->
<div style="display: flex; width: 300px; height: 100px; background-color: lightgray; margin-bottom: 20px; justify-content: flex-end;">
  <div>justify-content: flex-end</div>
</div>

Browser renders following output:

browser

And HtmlWidget:

flutter

Copy link
Owner

@daohoangson daohoangson left a comment

Choose a reason for hiding this comment

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

Please have a look, I have some comments. We also need some unit tests to cover the basics.

Comment on lines 684 to 693
case 'div':
final displayExpressions = meta.element.styles.where((element) => element.property == 'display');
final displayExpression = displayExpressions.isNotEmpty ? displayExpressions.first : null;
final display = displayExpression?.term ?? 'block';
if (display == 'flex') {
meta.register(_flexOp(meta));
}

meta[kCssDisplay] = kCssDisplayBlock;
break;
Copy link
Owner

Choose a reason for hiding this comment

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

Flex is supported for all kind of tags, not just DIV right? I think we should handle flex for all of them in parseStyleDisplay.

@@ -1150,14 +1158,103 @@ class WidgetFactory {
return baseUrl.resolveUri(uri).toString();
}

/// Builds custom widget for div elements with display: flex from [meta]
BuildOp _flexOp(BuildMetadata meta) {
return BuildOp(
Copy link
Owner

Choose a reason for hiding this comment

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

This op should be in its own file I think, probably packages/core/lib/src/internal/ops/flex.dart. Because of the complexity, flex implementation will grow to support all kind of use cases, the factory file is already too long.


if (value != null) {
switch (element.property) {
case 'flex-direction':
Copy link
Owner

Choose a reason for hiding this comment

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

This repo usually defines constants for inline style key & values. Like this:

const kCssBoxSizing = 'box-sizing';
const kCssBoxSizingContentBox = 'content-box';
const kCssBoxSizingBorderBox = 'border-box';

Comment on lines 1157 to 1160
onTree: (meta, tree) {
_anchorRegistry.register(id, anchor);
tree.registerAnchor(anchor);
},
Copy link
Owner

Choose a reason for hiding this comment

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

Why are these lines being removed? I think the anchor logic won't work without them?

@anttileppa
Copy link
Contributor Author

That you for the review @daohoangson, I mode the review changes and added tests for basic usage, horizontal alignments, vertical alignments and flex directions.

Let me know if there is anything else from preventing this to be merged into the project.

@daohoangson daohoangson mentioned this pull request Oct 16, 2023
Copy link
Owner

@daohoangson daohoangson left a comment

Choose a reason for hiding this comment

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

This PR also needs conflict resolve to merge into master.

@anttileppa
Copy link
Contributor Author

Conflicts have been resolved and flex has been converted to use new syntax.

@daohoangson daohoangson merged commit 2db7b6a into daohoangson:master Oct 27, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants