How to create a widget factory for elements side by side #833
-
I would like to use a widget factory to display elements within a div side by side. Unfortunately I don't know how to do that. For the col div class I can create a simple SizedBox with a specific width. But in order to display them side by side, I need a In the Is this currently possible @daohoangson? Html <div class="row">
<div class="col-md-4">
<p class="h2">Test</p>
</div>
<div class="col-md-8">
<p class="h2">Test 2</p>
</div>
</div> My current test widget factory mixin TestWidgetFactory on WidgetFactory {
@override
void parse(BuildMetadata meta) {
if (meta.element.localName == 'div') {
final List<String> classes = meta.element.classes.toList(growable: false);
if (classes.contains('row')) {
meta.register(
BuildOp(
onWidgets: (meta, widgets) {
/// wrap all childs with Wrap or Row widget
},
priority: 7001,
),
);
}
if (classes.contains('col')) {
meta.register(
BuildOp(
onWidgets: (meta, widgets) => listOrNull(
buildColumnPlaceholder(meta, widgets)?.wrapWith(
(_, child) => SizedBox(
width: 100, /// test value
child: child,
),
),
),
priority: 7001,
),
);
}
}
return super.parse(meta);
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
I think something like this should work https://try.fwfh.dev/?id=d7a0e963f8ba4b1b3c2ddb0fe15540b4 class _WidgetFactory extends WidgetFactory {
@override
void parse(BuildMetadata meta) {
if (meta.element.classes.contains('row')) {
meta.register(
BuildOp(
onWidgets: (meta, widgets) {
if (widgets.length < 2) {
return widgets;
}
return [Row(children: widgets.toList(growable: false))];
},
),
);
}
return super.parse(meta);
}
} |
Beta Was this translation helpful? Give feedback.
-
Thank you very much, this is working :) I don't know why I didn't think of it myself. |
Beta Was this translation helpful? Give feedback.
-
@daohoangson I have a follow-up question on the row example. I know that Bootstrap is not currently supported. We are currently trying a Widget Factory to allow Rows and Columns from Bootstrap. This works so far, except when a row has too many columns. With Bootstrap, an element then moves to the next row. Wrap does not work (the behavior is strange), so we try to build different rows. To do this, we try to split the columns into different rows in a BuildOP. In the example, the last column would have to go into the next row, because the column count is greater than 12. Unfortunately, the If we had an assignment, we could read the number of columns, and put the placeholders into multiple rows. Does anyone here possibly have an idea? Example html <div class="row">
<div class="col-6 p-2">
<div>
<p>Test</p>
</div>
</div>
<div class="col-6 p-2">
<div>
<p>Test2</p>
</div>
</div>
<div class="col-6 p-2">
<p>Test3</p>
</div>
</div> |
Beta Was this translation helpful? Give feedback.
I think something like this should work https://try.fwfh.dev/?id=d7a0e963f8ba4b1b3c2ddb0fe15540b4