build method

  1. @override
Widget build(
  1. BuildContext context,
  2. String text,
  3. GptMarkdownConfig config
)
override

Implementation

@override
Widget build(
  BuildContext context,
  String text,
  final GptMarkdownConfig config,
) {
  final List<Map<int, String>> value = text
      .split('\n')
      .map<Map<int, String>>(
        (e) => e
            .split('|')
            .where((element) => element.isNotEmpty)
            .toList()
            .asMap(),
      )
      .toList();
  bool heading = RegExp(
    r"^\|.*?\|\n\|-[-\\ |]*?-\|$",
    multiLine: true,
  ).hasMatch(text.trim());
  int maxCol = 0;
  for (final each in value) {
    if (maxCol < each.keys.length) {
      maxCol = each.keys.length;
    }
  }
  if (maxCol == 0) {
    return Text("", style: config.style);
  }
  final controller = ScrollController();
  return Scrollbar(
    controller: controller,
    child: SingleChildScrollView(
      controller: controller,
      scrollDirection: Axis.horizontal,
      child: Table(
        textDirection: config.textDirection,
        defaultColumnWidth: CustomTableColumnWidth(),
        defaultVerticalAlignment: TableCellVerticalAlignment.middle,
        border: TableBorder.all(
          width: 1,
          color: Theme.of(context).colorScheme.onSurface,
        ),
        children: value
            .asMap()
            .entries
            .map<TableRow>(
              (entry) => TableRow(
                decoration: (heading)
                    ? BoxDecoration(
                        color: (entry.key == 0)
                            ? Theme.of(context)
                                .colorScheme
                                .surfaceContainerHighest
                            : null,
                      )
                    : null,
                children: List.generate(
                  maxCol,
                  (index) {
                    var e = entry.value;
                    String data = e[index] ?? "";
                    if (RegExp(r"^--+$").hasMatch(data.trim()) ||
                        data.trim().isEmpty) {
                      return const SizedBox();
                    }

                    return Center(
                      child: Padding(
                        padding: const EdgeInsets.symmetric(
                            horizontal: 8, vertical: 4),
                        child: MdWidget(
                          (e[index] ?? "").trim(),
                          config: config,
                        ),
                      ),
                    );
                  },
                ),
              ),
            )
            .toList(),
      ),
    ),
  );
}