Skip to content

Commit 3b92f42

Browse files
committed
Support gradient text
1 parent b068c7d commit 3b92f42

File tree

16 files changed

+936
-36
lines changed

16 files changed

+936
-36
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 14.1.0
2+
3+
* Support gradient text
4+
15
## 14.0.1
26

37
* Implement SelectionArea triple click gestures ([#144563](https://github.com/flutter/flutter/pull/144563))

README-ZH.md

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717

1818
ExtendedText 是 Flutter 官方 Text 的三方扩展库,主要扩展功能如下:
19-
| 功能 | ExtendedText | Flutter 官方 Text |
20-
|--------------------------------------|-----------------------------------------------------------|-----------------------------------------------------|
21-
| 支持自定义文本溢出效果 | 支持,可以自定义溢出的 Widget,并控制溢出位置(前、中、后) | 不支 持 ([26748](https://github.com/flutter/flutter/issues/26748),[45336](https://github.com/flutter/flutter/issues/45336)) |
22-
| 支持复制特殊文本的真实值 | 支持,可以复制出文本的真实值,而不仅是 WidgetSpan 的占位值 | 只能复制出 WidgetSpan 的占位值 (\uFFFC) |
23-
| 根据文本格式快速构建富文本 | 支持,可以根据文本格式快速构建富文本 | 不支持 |
19+
| 功能 | ExtendedText | Flutter 官方 Text |
20+
| -------------------------- | ----------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
21+
| 支持自定义文本溢出效果 | 支持,可以自定义溢出的 Widget,并控制溢出位置(前、中、后) | 不支 持 ([26748](https://github.com/flutter/flutter/issues/26748),[45336](https://github.com/flutter/flutter/issues/45336)) |
22+
| 支持复制特殊文本的真实值 | 支持,可以复制出文本的真实值,而不仅是 WidgetSpan 的占位值 | 只能复制出 WidgetSpan 的占位值 (\uFFFC) |
23+
| 根据文本格式快速构建富文本 | 支持,可以根据文本格式快速构建富文本 | 不支持 |
2424

2525
> 已支持 `HarmonyOS`. 请使用最新的带有 `ohos` 标志的版本. 你可以在 `Versions` 签查找.
2626
@@ -499,12 +499,12 @@ Text背景相关的issue[24335](https://github.com/flutter/flutter/issues/24335)
499499

500500
文本溢出相关issue [26748](https://github.com/flutter/flutter/issues/26748)
501501

502-
| parameter | description | default |
503-
| ----------- | ------------------------------------------------------------ | ------------------- |
504-
| child | The widget of TextOverflow. | @required |
505-
| maxHeight | Widget的最大高度,默认为 TextPaint计算出来的行高 preferredLineHeight. | preferredLineHeight |
506-
| align | left,靠近最后裁剪文本;right,靠近文本的右下角 | right |
507-
| position | 溢出文本出现的地方. | TextOverflowPosition.end |
502+
| parameter | description | default |
503+
| --------- | --------------------------------------------------------------------- | ------------------------ |
504+
| child | The widget of TextOverflow. | @required |
505+
| maxHeight | Widget的最大高度,默认为 TextPaint计算出来的行高 preferredLineHeight. | preferredLineHeight |
506+
| align | left,靠近最后裁剪文本;right,靠近文本的右下角 | right |
507+
| position | 溢出文本出现的地方. | TextOverflowPosition.end |
508508

509509
```dart
510510
ExtendedText(
@@ -600,6 +600,49 @@ class MyTextSelectionControls extends TextSelectionControls {
600600
601601
```
602602

603+
## Gradient
604+
605+
### GradientConfig
606+
607+
608+
用于配置文本渐变的设置。
609+
610+
* [gradient] 是要应用于文本的渐变效果。
611+
* [ignoreWidgetSpan] 决定是否将 WidgetSpan 元素包含在渐变应用中。默认情况下,WidgetSpan 被忽略。
612+
* [renderMode] 指定渐变应用于文本的方式。默认值为 [GradientRenderMode.fullText],即将渐变应用于整个文本。
613+
* [ignoreRegex] 是一个正则表达式,用于排除文本中的某些部分,使其不受渐变效果影响。例如,可以用来排除特定字符或词语(如表情符号或特殊符号)以免它们受到渐变的影响。
614+
* [beforeDrawGradient] 在渐变被绘制到文本之前调用的回调函数。
615+
616+
* [blendMode] 应用于渐变的混合模式。
617+
默认值: [BlendMode.srcIn](即渐变将应用于文本上)。
618+
推荐使用 [BlendMode.srcIn][BlendMode.srcATop]
619+
620+
``` dart
621+
GradientConfig _config = GradientConfig(
622+
gradient: const LinearGradient(
623+
colors: <Color>[Colors.blue, Colors.red],
624+
),
625+
ignoreRegex: GradientConfig.ignoreEmojiRegex,
626+
ignoreWidgetSpan: true,
627+
renderMode: GradientRenderMode.fullText,
628+
);
629+
```
630+
631+
### IgnoreGradientSpan
632+
633+
`InlineSpan` 将始终忽略渐变效果。
634+
635+
``` dart
636+
class IgnoreGradientTextSpan extends TextSpan with IgnoreGradientSpan {
637+
IgnoreGradientTextSpan({String? text, List<InlineSpan>? children})
638+
: super(
639+
text: text,
640+
children: children,
641+
);
642+
}
643+
```
644+
645+
603646

604647
## ☕️Buy me a coffee
605648

README.md

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Extended official text to build special text like inline image or @somebody quic
1010

1111
ExtendedText is a third-party extension library for Flutter's official Text component. The main extended features are as follows:
1212

13-
| Feature | ExtendedText | Text |
14-
|------------------------------------------|-------------------------------------------------------------|-------------------------------------------------------|
15-
| Customized text overflow effects | Supported, allows customizing the overflow widget and controlling overflow positions (before, middle, after) | Not supported ([26748](https://github.com/flutter/flutter/issues/26748),[45336](https://github.com/flutter/flutter/issues/45336)) |
16-
| Copying the actual value of special text | Supported, enables copying the actual value of the text, not just the placeholder value of WidgetSpan | Can only copy the placeholder value of WidgetSpan (\uFFFC) |
17-
| Quick construction of rich text based on text format | Supported, enables quick construction of rich text based on text format | Not supported |
13+
| Feature | ExtendedText | Text |
14+
| ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------- |
15+
| Customized text overflow effects | Supported, allows customizing the overflow widget and controlling overflow positions (before, middle, after) | Not supported ([26748](https://github.com/flutter/flutter/issues/26748),[45336](https://github.com/flutter/flutter/issues/45336)) |
16+
| Copying the actual value of special text | Supported, enables copying the actual value of the text, not just the placeholder value of WidgetSpan | Can only copy the placeholder value of WidgetSpan (\uFFFC) |
17+
| Quick construction of rich text based on text format | Supported, enables quick construction of rich text based on text format | Not supported |
1818

1919
> `HarmonyOS` is supported. Please use the latest version which contains `ohos` tag. You can check it in `Versions` tab.
2020
@@ -40,6 +40,9 @@ dependencies:
4040
- [Custom Background](#custom-background)
4141
- [Custom Overflow](#custom-overflow)
4242
- [Join Zero-Width Space](#join-zero-width-space)
43+
- [Gradient](#gradient)
44+
- [GradientConfig](#gradientconfig)
45+
- [IgnoreGradientSpan](#ignoregradientspan)
4346
4447
## Speical Text
4548
@@ -481,12 +484,12 @@ refer to issues [24335](https://github.com/flutter/flutter/issues/24335)/[24337]
481484

482485
refer to issue [26748](https://github.com/flutter/flutter/issues/26748)
483486

484-
| parameter | description | default |
485-
| ----------- | ------------------------------------------------------------ | ------------------- |
486-
| child | The widget of TextOverflow. | @required |
487-
| maxHeight | The maxHeight of [TextOverflowWidget], default is preferredLineHeight. | preferredLineHeight |
488-
| align | The Align of [TextOverflowWidget], left/right. | right |
489-
| position | The position which TextOverflowWidget should be shown. | TextOverflowPosition.end |
487+
| parameter | description | default |
488+
| --------- | ---------------------------------------------------------------------- | ------------------------ |
489+
| child | The widget of TextOverflow. | @required |
490+
| maxHeight | The maxHeight of [TextOverflowWidget], default is preferredLineHeight. | preferredLineHeight |
491+
| align | The Align of [TextOverflowWidget], left/right. | right |
492+
| position | The position which TextOverflowWidget should be shown. | TextOverflowPosition.end |
490493

491494
```dart
492495
ExtendedText(
@@ -584,3 +587,52 @@ class MyTextSelectionControls extends TextSelectionControls {
584587
585588
```
586589

590+
## Gradient
591+
592+
### GradientConfig
593+
594+
595+
Configuration for applying gradients to text.
596+
597+
* [gradient] is the gradient that will be applied to the text.
598+
599+
* [ignoreWidgetSpan] determines whether `WidgetSpan` elements should be
600+
included in the gradient application. By default, widget spans are ignored.
601+
602+
* [renderMode] specifies how the gradient should be applied to the text. The default
603+
is [GradientRenderMode.fullText], meaning the gradient will apply to the entire text.
604+
605+
* [ignoreRegex] is a regular expression used to exclude certain parts of the text
606+
from the gradient effect. For example, it can be used to exclude specific characters
607+
or words (like emojis or special symbols) from the gradient application.
608+
609+
* [beforeDrawGradient] A callback function that is called before the gradient is drawn on the text.
610+
611+
* [blendMode] The blend mode to be used when applying the gradient.
612+
default: [BlendMode.srcIn] (i.e., the gradient will be applied to the text).
613+
It's better to use [BlendMode.srcIn] or [BlendMode.srcATop].
614+
615+
``` dart
616+
GradientConfig _config = GradientConfig(
617+
gradient: const LinearGradient(
618+
colors: <Color>[Colors.blue, Colors.red],
619+
),
620+
ignoreRegex: GradientConfig.ignoreEmojiRegex,
621+
ignoreWidgetSpan: true,
622+
renderMode: GradientRenderMode.fullText,
623+
);
624+
```
625+
626+
### IgnoreGradientSpan
627+
628+
The `InlineSpan` will always ignore the gradient.
629+
630+
``` dart
631+
class IgnoreGradientTextSpan extends TextSpan with IgnoreGradientSpan {
632+
IgnoreGradientTextSpan({String? text, List<InlineSpan>? children})
633+
: super(
634+
text: text,
635+
children: children,
636+
);
637+
}
638+
```

example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ build/
6969
!**/ios/**/default.pbxuser
7070
!**/ios/**/default.perspectivev3
7171
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
72+
.flutter-plugins-dependencies

example/lib/example_route.dart

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/example_routes.dart

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)