Skip to content

Commit 91a7e70

Browse files
committed
Re-add support for list-style-image and do some final cleanup
1 parent e3d9831 commit 91a7e70

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

lib/custom_render.dart

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'package:flutter/material.dart';
88
import 'package:flutter_html/flutter_html.dart';
99
import 'package:flutter_html/src/html_elements.dart';
1010
import 'package:flutter_html/src/utils.dart';
11-
import 'package:list_counter/list_counter.dart';
1211

1312
typedef CustomRenderMatcher = bool Function(RenderContext context);
1413

@@ -158,41 +157,12 @@ CustomRender listElementRender(
158157
{Style? style, Widget? child, List<InlineSpan>? children}) {
159158
return CustomRender.inlineSpan(
160159
inlineSpan: (context, buildChildren) {
161-
final usedStyle = style ?? context.style;
162-
final listStyleType = usedStyle.listStyleType ?? ListStyleType.decimal;
163-
final counterStyle =
164-
CounterStyleRegistry.lookup(listStyleType.counterStyle);
165-
String counterContent;
166-
if (usedStyle.marker?.content.isNormal ?? true) {
167-
counterContent = counterStyle.generateMarkerContent(
168-
context.tree.counters.lastOrNull?.value ?? 0,
169-
);
170-
} else if (!(usedStyle.marker?.content.display ?? true)) {
171-
counterContent = '';
172-
} else {
173-
counterContent = usedStyle.marker?.content.replacementContent ??
174-
counterStyle.generateMarkerContent(
175-
context.tree.counters.lastOrNull?.value ?? 0,
176-
);
177-
}
178-
final listChildren = buildChildren()
179-
..insertAll(
180-
0,
181-
[
182-
if (usedStyle.listStylePosition == ListStylePosition.inside)
183-
TextSpan(
184-
text: counterContent,
185-
style: usedStyle.marker?.style?.generateTextStyle(),
186-
),
187-
],
188-
);
189-
190160
return WidgetSpan(
191161
child: CssBoxWidget.withInlineSpanChildren(
192162
key: context.key,
193-
style: usedStyle,
163+
style: style ?? context.style,
194164
shrinkWrap: context.parser.shrinkWrap,
195-
children: listChildren,
165+
children: buildChildren(),
196166
),
197167
);
198168
},

lib/src/css_box_widget.dart

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ class CssBoxWidget extends StatelessWidget {
5757

5858
@override
5959
Widget build(BuildContext context) {
60-
final markerBox = _generateMarkerBox(style);
60+
final markerBox = style.listStylePosition == ListStylePosition.outside
61+
? _generateMarkerBoxSpan(style)
62+
: null;
6163

6264
return _CSSBoxRenderer(
6365
width: style.width ?? Width.auto(),
@@ -80,7 +82,7 @@ class CssBoxWidget extends StatelessWidget {
8082
padding: style.padding ?? EdgeInsets.zero,
8183
child: child,
8284
),
83-
if (markerBox != null) markerBox,
85+
if (markerBox != null) Text.rich(markerBox),
8486
],
8587
);
8688
}
@@ -92,6 +94,15 @@ class CssBoxWidget extends StatelessWidget {
9294
return Container();
9395
}
9496

97+
// Generate an inline marker box if the list-style-position is set to
98+
// inside. Otherwise the marker box will be added elsewhere.
99+
if (style.listStylePosition == ListStylePosition.inside) {
100+
final inlineMarkerBox = _generateMarkerBoxSpan(style);
101+
if (inlineMarkerBox != null) {
102+
children.insert(0, inlineMarkerBox);
103+
}
104+
}
105+
95106
return Text.rich(
96107
TextSpan(
97108
style: style.generateTextStyle(),
@@ -129,17 +140,38 @@ class CssBoxWidget extends StatelessWidget {
129140
);
130141
}
131142

132-
static Widget? _generateMarkerBox(Style style) {
133-
if (style.display == Display.listItem &&
134-
style.listStylePosition == ListStylePosition.outside) {
135-
if (style.marker?.content.replacementContent?.isNotEmpty ?? false) {
136-
return Text.rich(
137-
TextSpan(
138-
text: style.marker!.content.replacementContent!,
139-
style: style.marker!.style?.generateTextStyle(),
143+
static InlineSpan? _generateMarkerBoxSpan(Style style) {
144+
if (style.display == Display.listItem) {
145+
// First handle listStyleImage
146+
if (style.listStyleImage != null) {
147+
return WidgetSpan(
148+
alignment: PlaceholderAlignment.middle,
149+
child: Image.network(
150+
style.listStyleImage!.uriText,
151+
errorBuilder: (_, __, ___) {
152+
if (style.marker?.content.replacementContent?.isNotEmpty ??
153+
false) {
154+
return Text.rich(
155+
TextSpan(
156+
text: style.marker!.content.replacementContent!,
157+
style: style.marker!.style?.generateTextStyle(),
158+
),
159+
);
160+
}
161+
162+
return Container();
163+
},
140164
),
141165
);
142166
}
167+
168+
// Display list marker with given style
169+
if (style.marker?.content.replacementContent?.isNotEmpty ?? false) {
170+
return TextSpan(
171+
text: style.marker!.content.replacementContent!,
172+
style: style.marker!.style?.generateTextStyle(),
173+
);
174+
}
143175
}
144176

145177
return null;

0 commit comments

Comments
 (0)