Skip to content

Commit 7fc13e6

Browse files
committed
Allow centering images with auto-margins
1 parent b15a6bf commit 7fc13e6

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

lib/html_parser.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ class HtmlParser extends StatelessWidget {
456456
return WidgetSpan(
457457
alignment: tree.alignment,
458458
baseline: TextBaseline.alphabetic,
459-
child: tree.toWidget(context)!,
459+
child: tree.toWidget(newContext)!,
460460
);
461461
}
462462
} else if (tree is InteractableElement) {
@@ -848,7 +848,12 @@ class HtmlParser extends StatelessWidget {
848848
if (tree.children.isEmpty) {
849849
// Handle case (4) from above.
850850
if ((tree.style.height ?? 0) == 0) {
851-
tree.style.margin = Margins.all(0);
851+
tree.style.margin = tree.style.margin?.copyWith(
852+
left: Margin(value: 0, type: tree.style.margin!.left?.type ?? MarginType.VALUE),
853+
right: Margin(value: 0, type: tree.style.margin!.right?.type ?? MarginType.VALUE),
854+
top: Margin(value: 0, type: tree.style.margin!.top?.type ?? MarginType.VALUE),
855+
bottom: Margin(value: 0, type: tree.style.margin!.bottom?.type ?? MarginType.VALUE),
856+
) ?? Margins.all(0);
852857
}
853858
return tree;
854859
}
@@ -1058,9 +1063,9 @@ class ContainerSpan extends StatelessWidget {
10581063
),
10591064
);
10601065
if(style.margin?.isAutoHorizontal ?? false) {
1061-
return Row(
1062-
mainAxisAlignment: style.margin?.alignment ?? MainAxisAlignment.start,
1063-
children: [Flexible(child: container)],
1066+
return Align(
1067+
alignment: style.margin!.alignment,
1068+
child: container
10641069
);
10651070
}
10661071
return container;

lib/image_render.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ ImageRender networkImageRender({
152152
image.image.resolve(ImageConfiguration()).removeListener(listener);
153153
}
154154
if (snapshot.hasData) {
155-
return Container(
155+
var imageWidget = Container(
156156
constraints: BoxConstraints(
157157
maxWidth: width ?? _width(attributes) ?? snapshot.data!.width,
158158
maxHeight:
@@ -176,6 +176,13 @@ ImageRender networkImageRender({
176176
),
177177
),
178178
);
179+
if (context.style.margin?.isAutoHorizontal ?? false) {
180+
return Align(
181+
alignment: context.style.margin!.alignment,
182+
child: imageWidget,
183+
);
184+
}
185+
return imageWidget;
179186
} else if (snapshot.hasError) {
180187
return altWidget?.call(_alt(attributes)) ??
181188
Text(_alt(attributes) ?? "",

lib/style.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,13 @@ class Margins {
486486

487487
bool get isAutoHorizontal => (left?.isAuto ?? false) || (right?.isAuto ?? false);
488488

489-
MainAxisAlignment get alignment {
489+
AlignmentGeometry get alignment {
490490
if((left?.isAuto ?? false) && (right?.isAuto ?? false)) {
491-
return MainAxisAlignment.center;
491+
return Alignment.center;
492492
} else if(left?.isAuto ?? false) {
493-
return MainAxisAlignment.end;
493+
return Alignment.topRight;
494494
}
495-
return MainAxisAlignment.start;
495+
return Alignment.topLeft;
496496
}
497497

498498
/// Analogous to [EdgeInsets.all]

0 commit comments

Comments
 (0)