diff --git a/lib/ansu_ui.dart b/lib/ansu_ui.dart index 4f10078..8a8da89 100644 --- a/lib/ansu_ui.dart +++ b/lib/ansu_ui.dart @@ -28,6 +28,7 @@ export 'dialog/as_dialog_button.dart'; export 'dialog/as_show_bottom_dialog.dart'; export 'divider/as_divider.dart'; export 'drawer/as_drawer.dart'; +export 'extension/Text_extension.dart'; export 'extension/image_extension.dart'; export 'extension/list_extension.dart'; export 'extension/num_extension.dart'; diff --git a/lib/extension/Text_extension.dart b/lib/extension/Text_extension.dart new file mode 100644 index 0000000..e887c4a --- /dev/null +++ b/lib/extension/Text_extension.dart @@ -0,0 +1,9 @@ +import 'package:ansu_ui/utils/match_text.dart'; +import 'package:flutter/material.dart'; + +extension TextSpanExt on TextSpan { + TextSpan match(String match) { + return MatchText(matchText: match, text: this.text ?? '', style: this.style) + .matchSpan(); + } +} diff --git a/lib/extension/string_extension.dart b/lib/extension/string_extension.dart index d3409cc..d585d6e 100644 --- a/lib/extension/string_extension.dart +++ b/lib/extension/string_extension.dart @@ -1,4 +1,6 @@ import 'package:ansu_ui/toast/as_toast.dart'; +import 'package:ansu_ui/utils/match_text.dart'; +import 'package:flutter/material.dart'; extension PhoneExt on String { List get _parsePhone { @@ -34,4 +36,8 @@ extension PhoneExt on String { get toast { ASToast.show(this); } + + MatchText matchStr(String match, {TextStyle? style}) { + return MatchText(matchText: match, text: this); + } } diff --git a/lib/extension/widget_extension.dart b/lib/extension/widget_extension.dart index db960a0..6f9c416 100644 --- a/lib/extension/widget_extension.dart +++ b/lib/extension/widget_extension.dart @@ -1,5 +1,9 @@ +import 'package:ansu_ui/utils/match_text.dart'; +import 'package:auto_size_text_pk/auto_size_text_pk.dart'; import 'package:flutter/material.dart'; +import 'Text_extension.dart'; + extension WidgetExt on Widget { Widget paddingExt(double horizontal, [double vertical = 0]) => Padding( padding: EdgeInsets.symmetric( @@ -8,4 +12,33 @@ extension WidgetExt on Widget { ), child: this, ); + + Widget match(String match) { + var spans = (this as AutoSizeText).textSpan; + if (spans != null) { + spans = spans.match(match); + return Text.rich(spans); + } + if (this.runtimeType == Text) { + return MatchText( + matchText: match, + text: (this as Text).data ?? '', + style: (this as Text).style, + ); + } + + if (this.runtimeType == AutoSizeText) { + var spans = (this as AutoSizeText).textSpan; + if (spans != null) { + spans = spans.match(match); + return AutoSizeText.rich(spans); + } + return MatchText( + matchText: match, + text: (this as AutoSizeText).data ?? '', + style: (this as AutoSizeText).style, + ); + } + return this; + } } diff --git a/lib/utils/match_text.dart b/lib/utils/match_text.dart new file mode 100644 index 0000000..ec89697 --- /dev/null +++ b/lib/utils/match_text.dart @@ -0,0 +1,41 @@ +import 'package:ansu_ui/ansu_ui.dart'; +import 'package:flutter/material.dart'; + +class MatchText extends StatelessWidget { + final String matchText; + final String text; + final TextStyle? style; + + const MatchText({ + Key? key, + required this.matchText, + required this.text, + this.style, + }) : super(key: key); + + TextSpan matchSpan() { + return TextSpan(children: getSpans()); + } + + List getSpans() { + var textStyle = style ?? TextStyle(color: kTextColor, fontSize: 14.sp); + if (matchText.isEmpty) { + return [TextSpan(text: text, style: textStyle)]; + } + List allSpan = text + .replaceAllMapped(matchText, (Match mt) => '\r${mt[0]}\r') + .split('\r') + .map( + (e) => (matchText.matchAsPrefix(e) == null || e.length == text.length) + ? TextSpan(text: e, style: textStyle) + : TextSpan(text: e, style: textStyle.copyWith(color: Colors.red)), + ) + .toList(); + return allSpan; + } + + @override + Widget build(BuildContext context) { + return RichText(text: TextSpan(children: getSpans())); + } +} diff --git a/pubspec.lock b/pubspec.lock index fae8b8e..2d2f415 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -18,7 +18,7 @@ packages: source: hosted version: "2.6.1" auto_size_text_pk: - dependency: transitive + dependency: "direct main" description: name: auto_size_text_pk url: "https://pub.flutter-io.cn" diff --git a/pubspec.yaml b/pubspec.yaml index c6294c1..4de7d0a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,6 +17,7 @@ dependencies: image_picker: ^0.7.2 bot_toast: ^4.0.0+1 velocity_x: ^3.3.0 + auto_size_text_pk: ^3.0.0 expandable: ^5.0.1 animated_collection: git: