정의된 규칙을 사용하여 텍스트의 특정 부분에 스타일 지정 및 상호작용을 가능하게 하는 플러터 패키지입니다. 단어, 구절 또는 패턴을 매칭하고, 사용자 정의 스타일이나 onTap 이벤트와 같은 액션을 적용할 수 있습니다. 키워드 강조, 링크 삽입, 특정 텍스트 세그먼트에 애니메이션 추가와 같은 동적 포맷팅을 갖춘 풍부한 텍스트 표시를 만들기에 적합합니다. 모바일, 웹, 데스크톱을 포함한 모든 주요 플랫폼에서 작동합니다.
사용 사례
텍스트 내 키워드 강조
탭 동작을 사용한 상호작용 텍스트 만들기
문단 내에 스타일링된 링크 삽입
특정 텍스트 세그먼트에 애니메이션 추가
동적 규칙을 사용하여 형식화된 콘텐츠 표시
채팅 또는 기사 앱에서의 리치 텍스트 렌더링
주요 기능
패턴 기반 텍스트 매칭
일치 항목별 맞춤 스타일 적용
인터랙티브 콜백 (예: onTap)
동시에 여러 규칙을 지원
크로스 플랫폼 호환성
기존 Text 위젯과의 간편한 통합
적합한 대상
리치 텍스트 인터페이스를 구축하는 개발자
키워드 강조가 필요한 앱
채팅 또는 메시징 애플리케이션
콘텐츠가 풍부한 교육 또는 뉴스 앱
동적 텍스트 포맷팅이 필요한 UI
인터랙티브 텍스트 요소가 필요한 프로젝트
고려 사항
성능이 큰 텍스트 블록에 영향을 줄 수 있습니다
복잡한 정규 표현식 패턴은 신중한 테스트가 필요합니다
기본 스타일을 초과하는 내장 이모지 지원은 제한적
텍스트 패턴 매칭에 대한 이해가 필요합니다
중첩되거나 겹치는 매칭을 위해 추가 로직이 필요할 수 있습니다
기능이 풍부한 리치 텍스트 에디터용이 아님
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Flutter DecoratedText
The DecoratedText widget allows you to style and interact with different parts of a text string based on predefined rules. It's versatile, easy-to-use, and can handle different scenarios such as matching specific words, phrases, or patterns, allowing you to style the matched text differently.
Features
Selectable Text: Enable the selectable property (selectable: true) to allow text selection in the DecoratedText widget.
Predefined Rules: These are used to match different elements within the text. These rules include:
Matching words.
Matching text starting with a particular prefix.
Matching text between specific tags.
Matching links (URLs).
Interactive: Each rule can include an onTap callback, which returns the matched text, allowing you to implement interactive behavior such as opening a link when a URL is tapped.
Performance Optimized: RegExp patterns are cached internally, decoration results are memoized in text controllers, and tag deduplication uses an efficient O(n log n) algorithm.
Usage Examples
Match Text Between Tags
DecoratedText(
text: "Like between brackets {this is an example} or html tags <p>this is a paragraph</p>",
rules: [
DecoratorRule.between(
start: "{",
end: "}",
style: TextStyle(
color: Colors.blue,
),
),
DecoratorRule.between(
start: "<p>",
end: "</p>",
style: TextStyle(
color: Colors.green,
),
),
],
);
DecoratedText(
text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
rules: [
DecoratorRule.words(
words: ["lorem ipsum", "industry", "book", "make"],
onTap: (match) {
print(match);
},
style: TextStyle(
background: Paint()..color = Colors.yellow,
),
),
],
);
Match URLs
DecoratedText(
text: "You can match links with https https://pub.dev/ and links without it like google.com",
rules: [
DecoratorRule.url(
onTap: (url) {
print(url);
},
style: TextStyle(
color: Colors.blue,
decoration: TextDecoration.underline,
),
),
],
);
DecoratedText(
text: "I love Flutter! 😍",
rules: [
DecoratorRule(
regExp: RegExp(r'(\p{Emoji_Presentation})', unicode: true),
style: constTextStyle(
fontSize: 30.0,
),
onTap: (emoji) {
debugPrint('You tapped on the emoji: $emoji');
},
),
],
);
Performance Tips
For best performance when using custom RegExp patterns, cache them outside the build() method:
// Cache at the class or file level — avoid creating inside build()final _myRegExp = RegExp(r'my-pattern');
@overrideWidgetbuild(BuildContext context) {
returnDecoratedText(
text: "...",
rules: [
DecoratorRule(regExp: _myRegExp, style: TextStyle(...)),
],
);
}
Built-in rules like DecoratorRule.words(), DecoratorRule.url(), etc. handle caching automatically.
Remember, with the DecoratedText widget, you can style your text based on predefined rules, add interactivity, and handle complex matching scenarios with ease. Happy coding!