-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
background_text_demo.dart
229 lines (215 loc) · 10.2 KB
/
background_text_demo.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import 'package:extended_text/extended_text.dart';
import 'package:ff_annotation_route_library/ff_annotation_route_library.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
@FFRoute(
name: 'fluttercandies://BackgroundTextDemo',
routeName: 'BackgroundText',
description: 'workaround for issue 24335/24337 about background')
class BackgroundTextDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('nick background for text'),
),
body: SingleChildScrollView(
child: Container(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text.rich(TextSpan(children: <TextSpan>[
TextSpan(
text: '24335',
style: const TextStyle(
color: Colors.blue,
),
recognizer: TapGestureRecognizer()
..onTap = () {
launchUrl(Uri.parse(
'https://github.com/flutter/flutter/issues/24335'));
}),
const TextSpan(text: '/'),
TextSpan(
text: '24337',
style: const TextStyle(color: Colors.blue),
recognizer: TapGestureRecognizer()
..onTap = () {
launchUrl(Uri.parse(
'https://github.com/flutter/flutter/issues/24337'));
}),
])),
const Text(
'official text background with color Alpha 255, chinese words are missing',
),
Text(
'错误演示 12345',
style: TextStyle(background: Paint()..color = Colors.orange),
),
Container(
height: 20.0,
),
const Text(
'official text background with color Alpha 102, it has a hightlight line at top',
),
Text(
'错误演示 12345',
style: TextStyle(
background: Paint()
..color = Colors.orange.withOpacity(0.4)),
),
Container(
height: 20.0,
),
const Text(
'Following demo is workaround for issue 24335/24337 about background,for more you can define your custom background too.'),
Container(
height: 20.0,
),
ExtendedText.rich(
TextSpan(children: <TextSpan>[
BackgroundTextSpan(
text: '错误演示 12345',
background: Paint()..color = Colors.blue,
),
BackgroundTextSpan(
text: '错误演示 12345',
background: Paint()..color = Colors.blue,
style: const TextStyle(color: Colors.white)),
const TextSpan(
text: ' extended text with nice background '),
BackgroundTextSpan(
text:
'错误演示 12345 错误演示 12345 错误演示 12345 错误演示 12345 错误演示 12345 错误演示 12345',
background: Paint()..color = Colors.orange,
),
const TextSpan(
text:
' extended text with nice background,only problem is that we can get offset of ellipsis,so can\'t paint background at end of ellipsis. please let me know if any way to get offset of ellipsis.'),
BackgroundTextSpan(
text: 'paint background end of line 错误演示12345',
style: const TextStyle(color: Colors.red),
background: Paint()..color = Colors.red.withOpacity(0.1),
),
]),
maxLines: 8,
overflow: TextOverflow.ellipsis,
),
Container(
height: 20.0,
),
ExtendedText.rich(TextSpan(children: <TextSpan>[
BackgroundTextSpan(
text:
'This text has nice background with borderradius,no mattter how many line,it likes nice',
background: Paint()..color = Colors.indigo,
clipBorderRadius:
const BorderRadius.all(Radius.circular(3.0))),
])),
Container(
height: 20.0,
),
ExtendedText.rich(TextSpan(children: <TextSpan>[
BackgroundTextSpan(
text:
'if you don\'t like default background, you can use paintBackground call back to draw your background',
background: Paint()..color = Colors.teal,
clipBorderRadius:
const BorderRadius.all(Radius.circular(3.0)),
paintBackground: (BackgroundTextSpan backgroundTextSpan,
Canvas canvas,
Offset offset,
TextPainter? painter,
Rect rect,
{Offset? endOffset,
TextPainter? wholeTextPainter}) {
final Rect textRect = offset & painter!.size;
///top-right
if (endOffset != null) {
final Rect firstLineRect = offset &
Size(rect.right - offset.dx, painter.height);
if (backgroundTextSpan.clipBorderRadius != null) {
canvas.save();
canvas.clipPath(Path()
..addRRect(backgroundTextSpan.clipBorderRadius!
.resolve(painter.textDirection)
.toRRect(firstLineRect)));
}
///start
canvas.drawRect(
firstLineRect, backgroundTextSpan.background);
if (backgroundTextSpan.clipBorderRadius != null) {
canvas.restore();
}
///endOffset.y has deviation,so we calculate with text height
final int fullLinesAndLastLine =
((endOffset.dy - offset.dy) / painter.height)
.round();
double y = offset.dy;
for (int i = 0; i < fullLinesAndLastLine; i++) {
y += painter.height;
//last line
if (i == fullLinesAndLastLine - 1) {
final Rect lastLineRect = Offset(0.0, y) &
Size(endOffset.dx, painter.height);
if (backgroundTextSpan.clipBorderRadius != null) {
canvas.save();
canvas.clipPath(Path()
..addRRect(backgroundTextSpan
.clipBorderRadius!
.resolve(painter.textDirection)
.toRRect(lastLineRect)));
}
canvas.drawRect(
lastLineRect, backgroundTextSpan.background);
if (backgroundTextSpan.clipBorderRadius != null) {
canvas.restore();
}
}
///draw full line
else {
final Rect fullLineRect = Offset(0.0, y) &
Size(rect.width, painter.height);
if (backgroundTextSpan.clipBorderRadius != null) {
canvas.save();
canvas.clipPath(Path()
..addRRect(backgroundTextSpan
.clipBorderRadius!
.resolve(painter.textDirection)
.toRRect(fullLineRect)));
}
///draw full line
canvas.drawRect(
fullLineRect, backgroundTextSpan.background);
if (backgroundTextSpan.clipBorderRadius != null) {
canvas.restore();
}
}
}
} else {
if (backgroundTextSpan.clipBorderRadius != null) {
canvas.save();
canvas.clipPath(Path()
..addRRect(backgroundTextSpan.clipBorderRadius!
.resolve(painter.textDirection)
.toRRect(textRect)));
}
canvas.drawRect(
textRect, backgroundTextSpan.background);
if (backgroundTextSpan.clipBorderRadius != null) {
canvas.restore();
}
}
///remember return true to igore default background
return true;
}),
])),
],
)),
),
);
}
}