Visitar URL original
Allow custom bullet points through a widget builder (#167) · stuartmorgan-g/packages@0191c32 · GitHub
Skip to content

Commit 0191c32

Browse files
authored
Allow custom bullet points through a widget builder (flutter#167)
1 parent 2247f1d commit 0191c32

4 files changed

Lines changed: 96 additions & 111 deletions

File tree

‎packages/flutter_markdown/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.6.1
2+
3+
* Added builder option bulletBuilder
4+
15
## 0.6.0
26

37
* Null safety release

‎packages/flutter_markdown/lib/src/builder.dart‎

Lines changed: 34 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,7 @@ import '_functions_io.dart' if (dart.library.html) '_functions_web.dart';
1010
import 'style_sheet.dart';
1111
import 'widget.dart';
1212

13-
const List<String> _kBlockTags = const <String>[
14-
'p',
15-
'h1',
16-
'h2',
17-
'h3',
18-
'h4',
19-
'h5',
20-
'h6',
21-
'li',
22-
'blockquote',
23-
'pre',
24-
'ol',
25-
'ul',
26-
'hr',
27-
'table',
28-
'thead',
29-
'tbody',
30-
'tr'
31-
];
13+
const List<String> _kBlockTags = const <String>['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'blockquote', 'pre', 'ol', 'ul', 'hr', 'table', 'thead', 'tbody', 'tr'];
3214

3315
const List<String> _kListTags = const <String>['ul', 'ol'];
3416

@@ -99,6 +81,7 @@ class MarkdownBuilder implements md.NodeVisitor {
9981
required this.imageDirectory,
10082
required this.imageBuilder,
10183
required this.checkboxBuilder,
84+
required this.bulletBuilder,
10285
required this.builders,
10386
required this.listItemCrossAxisAlignment,
10487
this.fitContent = false,
@@ -125,6 +108,9 @@ class MarkdownBuilder implements md.NodeVisitor {
125108
/// Call when build a checkbox widget.
126109
final MarkdownCheckboxBuilder? checkboxBuilder;
127110

111+
/// Called when building a custom bullet.
112+
final MarkdownBulletBuilder? bulletBuilder;
113+
128114
/// Call when build a custom widget.
129115
final Map<String, MarkdownElementBuilder> builders;
130116

@@ -188,16 +174,14 @@ class MarkdownBuilder implements md.NodeVisitor {
188174
_addAnonymousBlockIfNeeded();
189175
if (_isListTag(tag)) {
190176
_listIndents.add(tag);
191-
if (element.attributes["start"] != null)
192-
start = int.parse(element.attributes["start"]!) - 1;
177+
if (element.attributes["start"] != null) start = int.parse(element.attributes["start"]!) - 1;
193178
} else if (tag == 'blockquote') {
194179
_isInBlockquote = true;
195180
} else if (tag == 'table') {
196181
_tables.add(_TableElement());
197182
} else if (tag == 'tr') {
198183
final length = _tables.single.rows.length;
199-
BoxDecoration? decoration =
200-
styleSheet.tableCellsDecoration as BoxDecoration?;
184+
BoxDecoration? decoration = styleSheet.tableCellsDecoration as BoxDecoration?;
201185
if (length == 0 || length % 2 == 1) decoration = null;
202186
_tables.single.rows.add(TableRow(
203187
decoration: decoration,
@@ -227,9 +211,7 @@ class MarkdownBuilder implements md.NodeVisitor {
227211
// The Markdown parser passes empty table data tags for blank
228212
// table cells. Insert a text node with an empty string in this
229213
// case for the table cell to get properly created.
230-
if (element.tag == 'td' &&
231-
element.children != null &&
232-
element.children!.isEmpty) {
214+
if (element.tag == 'td' && element.children != null && element.children!.isEmpty) {
233215
element.children!.add(md.Text(''));
234216
}
235217

@@ -244,13 +226,7 @@ class MarkdownBuilder implements md.NodeVisitor {
244226
}
245227

246228
String? extractTextFromElement(element) {
247-
return element is md.Element && (element.children?.isNotEmpty ?? false)
248-
? element.children!
249-
.map((e) => e is md.Text ? e.text : extractTextFromElement(e))
250-
.join("")
251-
: ((element.attributes?.isNotEmpty ?? false)
252-
? element.attributes["alt"]
253-
: "");
229+
return element is md.Element && (element.children?.isNotEmpty ?? false) ? element.children!.map((e) => e is md.Text ? e.text : extractTextFromElement(e)).join("") : ((element.attributes?.isNotEmpty ?? false) ? element.attributes["alt"] : "");
254230
}
255231

256232
@override
@@ -286,8 +262,7 @@ class MarkdownBuilder implements md.NodeVisitor {
286262

287263
Widget? child;
288264
if (_blocks.isNotEmpty && builders.containsKey(_blocks.last.tag)) {
289-
child = builders[_blocks.last.tag!]!
290-
.visitText(text, styleSheet.styles[_blocks.last.tag!]);
265+
child = builders[_blocks.last.tag!]!.visitText(text, styleSheet.styles[_blocks.last.tag!]);
291266
} else if (_blocks.last.tag == 'pre') {
292267
child = Scrollbar(
293268
child: SingleChildScrollView(
@@ -299,9 +274,7 @@ class MarkdownBuilder implements md.NodeVisitor {
299274
} else {
300275
child = _buildRichText(
301276
TextSpan(
302-
style: _isInBlockquote
303-
? styleSheet.blockquote!.merge(_inlines.last.style)
304-
: _inlines.last.style,
277+
style: _isInBlockquote ? styleSheet.blockquote!.merge(_inlines.last.style) : _inlines.last.style,
305278
text: _isInBlockquote ? text.text : trimText(text.text),
306279
recognizer: _linkHandlers.isNotEmpty ? _linkHandlers.last : null,
307280
),
@@ -325,9 +298,7 @@ class MarkdownBuilder implements md.NodeVisitor {
325298

326299
if (current.children.isNotEmpty) {
327300
child = Column(
328-
crossAxisAlignment: fitContent
329-
? CrossAxisAlignment.start
330-
: CrossAxisAlignment.stretch,
301+
crossAxisAlignment: fitContent ? CrossAxisAlignment.start : CrossAxisAlignment.stretch,
331302
children: current.children,
332303
);
333304
} else {
@@ -351,19 +322,11 @@ class MarkdownBuilder implements md.NodeVisitor {
351322
bullet = _buildBullet(_listIndents.last);
352323
}
353324
child = Row(
354-
textBaseline: listItemCrossAxisAlignment ==
355-
MarkdownListItemCrossAxisAlignment.start
356-
? null
357-
: TextBaseline.alphabetic,
358-
crossAxisAlignment: listItemCrossAxisAlignment ==
359-
MarkdownListItemCrossAxisAlignment.start
360-
? CrossAxisAlignment.start
361-
: CrossAxisAlignment.baseline,
325+
textBaseline: listItemCrossAxisAlignment == MarkdownListItemCrossAxisAlignment.start ? null : TextBaseline.alphabetic,
326+
crossAxisAlignment: listItemCrossAxisAlignment == MarkdownListItemCrossAxisAlignment.start ? CrossAxisAlignment.start : CrossAxisAlignment.baseline,
362327
children: <Widget>[
363328
SizedBox(
364-
width: styleSheet.listIndent! +
365-
styleSheet.listBulletPadding!.left +
366-
styleSheet.listBulletPadding!.right,
329+
width: styleSheet.listIndent! + styleSheet.listBulletPadding!.left + styleSheet.listBulletPadding!.right,
367330
child: bullet,
368331
),
369332
Expanded(child: child)
@@ -401,8 +364,7 @@ class MarkdownBuilder implements md.NodeVisitor {
401364
final _InlineElement parent = _inlines.last;
402365

403366
if (builders.containsKey(tag)) {
404-
final Widget? child =
405-
builders[tag]!.visitElementAfter(element, styleSheet.styles[tag]);
367+
final Widget? child = builders[tag]!.visitElementAfter(element, styleSheet.styles[tag]);
406368
if (child != null) current.children[0] = child;
407369
} else if (tag == 'img') {
408370
// create an image widget for this image
@@ -474,8 +436,7 @@ class MarkdownBuilder implements md.NodeVisitor {
474436
}
475437

476438
if (_linkHandlers.isNotEmpty) {
477-
TapGestureRecognizer recognizer =
478-
_linkHandlers.last as TapGestureRecognizer;
439+
TapGestureRecognizer recognizer = _linkHandlers.last as TapGestureRecognizer;
479440
return GestureDetector(child: child, onTap: recognizer.onTap);
480441
} else {
481442
return child;
@@ -497,7 +458,17 @@ class MarkdownBuilder implements md.NodeVisitor {
497458
}
498459

499460
Widget _buildBullet(String listTag) {
500-
if (listTag == 'ul') {
461+
final int index = _blocks.last.nextListIndex;
462+
final bool isUnordered = listTag == 'ul';
463+
464+
if (bulletBuilder != null) {
465+
return Padding(
466+
padding: styleSheet.listBulletPadding!,
467+
child: bulletBuilder!(index, isUnordered ? BulletStyle.unorderedList : BulletStyle.orderedList),
468+
);
469+
}
470+
471+
if (isUnordered) {
501472
return Padding(
502473
padding: styleSheet.listBulletPadding!,
503474
child: Text(
@@ -508,7 +479,6 @@ class MarkdownBuilder implements md.NodeVisitor {
508479
);
509480
}
510481

511-
final int index = _blocks.last.nextListIndex;
512482
return Padding(
513483
padding: styleSheet.listBulletPadding!,
514484
child: Text(
@@ -583,28 +553,20 @@ class MarkdownBuilder implements md.NodeVisitor {
583553
) {
584554
List<Widget> mergedTexts = <Widget>[];
585555
for (Widget child in children) {
586-
if (mergedTexts.isNotEmpty &&
587-
mergedTexts.last is RichText &&
588-
child is RichText) {
556+
if (mergedTexts.isNotEmpty && mergedTexts.last is RichText && child is RichText) {
589557
RichText previous = mergedTexts.removeLast() as RichText;
590558
TextSpan previousTextSpan = previous.text as TextSpan;
591-
List<TextSpan> children = previousTextSpan.children != null
592-
? List.from(previousTextSpan.children!)
593-
: [previousTextSpan];
559+
List<TextSpan> children = previousTextSpan.children != null ? List.from(previousTextSpan.children!) : [previousTextSpan];
594560
children.add(child.text as TextSpan);
595561
TextSpan? mergedSpan = _mergeSimilarTextSpans(children);
596562
mergedTexts.add(_buildRichText(
597563
mergedSpan,
598564
textAlign: textAlign,
599565
));
600-
} else if (mergedTexts.isNotEmpty &&
601-
mergedTexts.last is SelectableText &&
602-
child is SelectableText) {
566+
} else if (mergedTexts.isNotEmpty && mergedTexts.last is SelectableText && child is SelectableText) {
603567
SelectableText previous = mergedTexts.removeLast() as SelectableText;
604568
TextSpan previousTextSpan = previous.textSpan!;
605-
List<TextSpan> children = previousTextSpan.children != null
606-
? List.from(previousTextSpan.children!)
607-
: [previousTextSpan];
569+
List<TextSpan> children = previousTextSpan.children != null ? List.from(previousTextSpan.children!) : [previousTextSpan];
608570
if (child.textSpan != null) {
609571
children.add(child.textSpan!);
610572
}
@@ -667,10 +629,7 @@ class MarkdownBuilder implements md.NodeVisitor {
667629

668630
for (int index = 1; index < textSpans.length; index++) {
669631
TextSpan? nextChild = textSpans[index];
670-
if (nextChild is TextSpan &&
671-
nextChild.recognizer == mergedSpans.last.recognizer &&
672-
nextChild.semanticsLabel == mergedSpans.last.semanticsLabel &&
673-
nextChild.style == mergedSpans.last.style) {
632+
if (nextChild is TextSpan && nextChild.recognizer == mergedSpans.last.recognizer && nextChild.semanticsLabel == mergedSpans.last.semanticsLabel && nextChild.style == mergedSpans.last.style) {
674633
TextSpan previous = mergedSpans.removeLast();
675634
mergedSpans.add(TextSpan(
676635
text: previous.toPlainText() + nextChild.toPlainText(),
@@ -685,9 +644,7 @@ class MarkdownBuilder implements md.NodeVisitor {
685644

686645
// When the mergered spans compress into a single TextSpan return just that
687646
// TextSpan, otherwise bundle the set of TextSpans under a single parent.
688-
return mergedSpans.length == 1
689-
? mergedSpans.first
690-
: TextSpan(children: mergedSpans);
647+
return mergedSpans.length == 1 ? mergedSpans.first : TextSpan(children: mergedSpans);
691648
}
692649

693650
Widget _buildRichText(TextSpan? text, {TextAlign? textAlign}) {

0 commit comments

Comments
 (0)