添加 匹配文字标红组件及扩展

master
张萌 3 years ago
parent b5eb30faeb
commit 04b44e65f0

@ -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';

@ -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();
}
}

@ -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);
}
}

@ -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;
}
}

@ -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<TextSpan> getSpans() {
var textStyle = style ?? TextStyle(color: kTextColor, fontSize: 14.sp);
if (matchText.isEmpty) {
return [TextSpan(text: text, style: textStyle)];
}
List<TextSpan> allSpan = text
.replaceAllMapped(matchText, (Match mt) => '\r${mt[0]}\r')
.split('\r')
.map<TextSpan>(
(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()));
}
}

@ -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"

@ -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:

Loading…
Cancel
Save