From 7fa0a9fa32fef6363c322ec7562bf2b3752190c0 Mon Sep 17 00:00:00 2001 From: sherlock Date: Mon, 3 Jul 2023 13:46:13 +0700 Subject: [PATCH] improve/add pill builder for customization --- lib/flutter_html.dart | 3 +++ lib/text_parser.dart | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/flutter_html.dart b/lib/flutter_html.dart index a613d66485..fef0a670eb 100644 --- a/lib/flutter_html.dart +++ b/lib/flutter_html.dart @@ -32,6 +32,7 @@ class Html extends StatelessWidget { this.getCodeLanguage, this.setCodeLanguage, this.inlineSpanEnd, + this.pillBuilder, }) : super(key: key); final String data; @@ -57,6 +58,7 @@ class Html extends StatelessWidget { /// Setting and getting code langauge cache final SetCodeLanguage? setCodeLanguage; final GetCodeLanguage? getCodeLanguage; + final PillBuilder? pillBuilder; final InlineSpan? inlineSpanEnd; @@ -89,6 +91,7 @@ class Html extends StatelessWidget { setCodeLanguage: setCodeLanguage, getCodeLanguage: getCodeLanguage, inlineSpanEnd: inlineSpanEnd, + pillBuilder: pillBuilder, ), ), ); diff --git a/lib/text_parser.dart b/lib/text_parser.dart index a0d2f3914f..58fb9db88d 100644 --- a/lib/text_parser.dart +++ b/lib/text_parser.dart @@ -24,6 +24,7 @@ typedef OnPillTap = void Function(String identifier); typedef GetMxcUrl = String Function(String mxc, double? width, double? height, {bool? animated}); typedef GetPillInfo = Future> Function(String identifier); +typedef PillBuilder = Widget? Function(String identifier, String url, OnPillTap? onTap, GetMxcUrl? getMxcUrl); const OFFSET_TAGS_FONT_SIZE_FACTOR = 0.7; //The ratio of the parent font for each of the offset tags: sup or sub @@ -105,6 +106,7 @@ class TextParser extends StatelessWidget { this.setCodeLanguage, this.getCodeLanguage, this.inlineSpanEnd, + this.pillBuilder, }); final double indentSize = 10.0; @@ -127,6 +129,7 @@ class TextParser extends StatelessWidget { final SetCodeLanguage? setCodeLanguage; final GetCodeLanguage? getCodeLanguage; final InlineSpan? inlineSpanEnd; + final PillBuilder? pillBuilder; TextSpan _parseTextNode( BuildContext context, ParseContext parseContext, dom.Text node) { @@ -461,7 +464,7 @@ class TextParser extends StatelessWidget { if (isPill) { return WidgetSpan( alignment: PlaceholderAlignment.middle, - child: Pill( + child: pillBuilder?.call(identifier, url, onPillTap, getMxcUrl) ?? Pill( identifier: identifier, url: url, future: getPillInfo?.call(url),