getListBlock method

  1. @override
Future<Widget> getListBlock(
  1. String line,
  2. bool isCheckList, [
  3. TextStyle? style
])
override

Implementation

@override
Future<pw.Widget> getListBlock(String line, bool isCheckList, [pw.TextStyle? style]) async {
  final List<pw.WidgetSpan> widgets = <pw.WidgetSpan>[];
  final RegExpMatch? checkMatch = Constant.LIST_CHECK_MD_PATTERN.firstMatch(line);
  final RegExpMatch? listMatch = Constant.LIST_PATTERN.firstMatch(line);
  final List<pw.InlineSpan> styledWidgets = <pw.InlineSpan>[];
  if (!isCheckList && listMatch != null) {
    final String typeList = listMatch.group(1)!;
    final String content = listMatch.group(2)!;
    styledWidgets.addAll(await _getStylesSpans(content, style));
    if (typeList.startsWith('*')) {
      //replace with bullet widget by error with fonts callback
      widgets.add(
        pw.WidgetSpan(
          child: pw.Container(
            padding: const pw.EdgeInsets.only(left: 15, bottom: 1.5),
            child: pw.RichText(
              softWrap: true,
              overflow: pw.TextOverflow.span,
              text: pw.TextSpan(
                text: '• ',
                children: <pw.InlineSpan>[
                  pw.TextSpan(children: styledWidgets),
                ],
              ),
            ),
          ),
        ),
      );
      return pw.RichText(text: pw.TextSpan(children: widgets));
    }
    widgets.add(
      pw.WidgetSpan(
        child: pw.Container(
          padding: const pw.EdgeInsets.only(left: 15, bottom: 1.5),
          child: pw.RichText(
            softWrap: true,
            overflow: pw.TextOverflow.span,
            text: pw.TextSpan(
              text: '$typeList ',
              children: <pw.InlineSpan>[
                pw.TextSpan(children: styledWidgets),
              ],
            ),
          ),
        ),
      ),
    );
    return pw.RichText(
      softWrap: true,
      overflow: pw.TextOverflow.span,
      text: pw.TextSpan(children: widgets),
    );
  }
  if (checkMatch != null) {
    final bool checked = checkMatch.group(2) != ' ';
    final String? align = checkMatch.group(4);
    final String content = checkMatch.group(5)!;
    styledWidgets.addAll(await _getStylesSpans(content, style));
    widgets.add(pw.WidgetSpan(
      child: pw.Container(
        padding: const pw.EdgeInsets.only(left: 15, bottom: 1.5),
        child: pw.Row(
          children: <pw.Widget>[
            pw.Checkbox(
              activeColor: PdfColors.blue400,
              name: 'check ${Random.secure().nextInt(9999999) + 50}',
              value: checked,
            ),
            pw.Expanded(
              child: pw.RichText(
                textAlign: align?.resolvePdfTextAlign,
                softWrap: true,
                overflow: pw.TextOverflow.span,
                text: pw.TextSpan(
                  children: <pw.InlineSpan>[
                    pw.TextSpan(children: <pw.InlineSpan>[...styledWidgets])
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    ));
  }
  return pw.RichText(
    softWrap: true,
    overflow: pw.TextOverflow.span,
    text: pw.TextSpan(children: widgets),
  );
}