-
Notifications
You must be signed in to change notification settings - Fork 734
Expand file tree
/
Copy pathbbcode_widget.dart
More file actions
211 lines (203 loc) · 7.42 KB
/
bbcode_widget.dart
File metadata and controls
211 lines (203 loc) · 7.42 KB
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
import 'dart:ui' as ui;
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:antlr4/antlr4.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:url_launcher/url_launcher.dart';
import 'bbcode_base_listener.dart';
import 'bbcode_elements.dart';
import 'generated/BBCodeParser.dart';
import 'generated/BBCodeLexer.dart';
class BBCodeWidget extends StatefulWidget {
const BBCodeWidget({
super.key, required this.bbcode,
this.imageRadius = const BorderRadius.all(Radius.circular(8))
});
final String bbcode;
final BorderRadiusGeometry imageRadius;
@override
State<StatefulWidget> createState() => _BBCodeWidgetState();
}
class _BBCodeWidgetState extends State<BBCodeWidget> {
bool _isVisible = false;
/// color 可以为三种表现形式
///
/// `ARGB: #FFFFFFFF`
///
/// `RGB: #FFFFFF`
///
/// `NAME: red`
///
/// 若全部解析失败则返回 null 使用默认颜色
Color? _parseColor(String hex) {
if (hex.startsWith('#')) {
hex = hex.replaceFirst('#', '');
if (hex.length == 6) {
hex = "FF$hex";
}
if (hex.length == 8) {
return Color(int.parse(hex, radix: 16));
}
}
switch (hex) {
case 'red':
return Colors.red;
case 'blue':
return Colors.blue;
case 'orange':
return Colors.orange;
case 'green':
return Colors.green;
case 'grey':
return Colors.grey;
default:
return null;
}
}
@override
Widget build(BuildContext context) {
BBCodeParser.checkVersion();
BBCodeParser.checkVersion();
final input = InputStream.fromString(widget.bbcode);
final lexer = BBCodeLexer(input);
final tokens = CommonTokenStream(lexer);
final parser = BBCodeParser(tokens);
final tree = parser.document();
final bbcodeBaseListener = BBCodeBaseListener();
ParseTreeWalker.DEFAULT.walk(bbcodeBaseListener, tree);
bbCodeTag.clear();
return Wrap(
children: [
SelectableText.rich(
TextSpan(
children: bbcodeBaseListener.bbcode.map((e) {
if (e is BBCodeText) {
Color? textColor = (!_isVisible && e.masked)
? Colors.transparent
: (e.link != null)
? Colors.blue
: (e.quoted)
? Theme.of(context).colorScheme.outline
: (e.color != null)
? _parseColor(e.color!)
: null;
return TextSpan(
text: e.text,
mouseCursor: (e.link != null || e.masked)
? SystemMouseCursors.click
: SystemMouseCursors.text,
recognizer: TapGestureRecognizer()
..onTap = (e.link != null || e.masked)
? () {
if ((!e.masked || _isVisible) && e.link != null) {
launchUrl(Uri.parse(e.link!));
} else if (e.masked) {
setState(() {
_isVisible = !_isVisible;
});
}
}
: null,
style: TextStyle(
fontWeight: (e.bold) ? FontWeight.bold : null,
fontStyle: (e.italic) ? FontStyle.italic : null,
decoration: TextDecoration.combine([
if (e.underline || e.link != null)
TextDecoration.underline,
if (e.strikeThrough) TextDecoration.lineThrough,
]),
decorationColor: textColor,
fontSize: e.size.toDouble(),
color: textColor,
backgroundColor:
(!_isVisible && e.masked) ? Color(0xFF555555) : null,
fontFeatures: [FontFeature.tabularFigures()],
),
);
} else if (e is BBCodeImg) {
return WidgetSpan(
child:ClipRRect(
borderRadius: widget.imageRadius,
child: CachedNetworkImage(
imageUrl: e.imageUrl,
placeholder: (context, url) =>
const SizedBox(width: 1, height: 1),
errorWidget: (context, error, stackTrace) {
return const Text('.');
},
),
),
);
} else if (e is BBCodeBgm) {
String url;
if (e.id == 11 || e.id == 23) {
url = 'https://bangumi.tv/img/smiles/bgm/${e.id}.gif';
}
if (e.id < 24) {
url = 'https://bangumi.tv/img/smiles/bgm/${e.id}.png';
}
if (e.id < 33) {
url = 'https://bangumi.tv/img/smiles/tv/0${e.id - 23}.gif';
}
url = 'https://bangumi.tv/img/smiles/tv/${e.id - 23}.gif';
return WidgetSpan(
child: ClipRRect(
borderRadius: widget.imageRadius,
child: CachedNetworkImage(
imageUrl: url,
placeholder: (context, url) =>
const SizedBox(width: 1, height: 1),
errorWidget: (context, error, stackTrace) {
return const Text('.');
},
),
),
);
} else if (e is BBCodeMusume) {
return WidgetSpan(
child: ClipRRect(
borderRadius: widget.imageRadius,
child: CachedNetworkImage(
imageUrl: 'https://lain.bgm.tv/img/smiles/musume/musume_${e.id}.gif',
placeholder: (context, url) =>
const SizedBox(width: 1, height: 1),
errorWidget: (context, error, stackTrace) {
return const Text('.');
},
width: 50,
height: 50,
),
) ,
);
} else if (e is BBCodeSticker) {
return WidgetSpan(
child: ClipRRect(
borderRadius: widget.imageRadius,
child: CachedNetworkImage(
imageUrl: 'https://bangumi.tv/img/smiles/${e.id}.gif',
placeholder: (context, url) =>
const SizedBox(width: 1, height: 1),
errorWidget: (context, error, stackTrace) {
return const Text('.');
},
),
) ,
);
} else {
// e is Icon
return WidgetSpan(
child: Icon(
(e as Icon).icon,
color: Theme.of(context).colorScheme.outline,
),
alignment: PlaceholderAlignment.top,
);
}
}).toList(),
),
selectionHeightStyle: ui.BoxHeightStyle.max,
),
],
);
}
}