forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpandable_text.dart
More file actions
96 lines (88 loc) · 3.18 KB
/
Copy pathexpandable_text.dart
File metadata and controls
96 lines (88 loc) · 3.18 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
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:omi/utils/l10n_extensions.dart';
class ExpandableTextWidget extends StatefulWidget {
final String text;
final bool isExpanded;
final Function toggleExpand;
final TextStyle style;
final int maxLines;
final String? expandText;
final String? collapseText;
final Color linkColor;
const ExpandableTextWidget({
super.key,
required this.text,
required this.style,
this.maxLines = 3,
this.expandText,
this.collapseText,
this.linkColor = Colors.deepPurple,
required this.isExpanded,
required this.toggleExpand,
});
@override
_ExpandableTextWidgetState createState() => _ExpandableTextWidgetState();
}
class _ExpandableTextWidgetState extends State<ExpandableTextWidget> {
@override
Widget build(BuildContext context) {
final expandLabel = widget.expandText ?? context.l10n.showMore;
final collapseLabel = widget.collapseText ?? context.l10n.showLess;
final span = TextSpan(text: widget.text, style: widget.style);
final tp = TextPainter(text: span, maxLines: widget.maxLines, textDirection: TextDirection.ltr);
var width = MediaQuery.of(context).size.width;
tp.layout(maxWidth: width);
final isOverflowing = tp.didExceedMaxLines;
final maxChars = tp.getPositionForOffset(Offset(width, 0)).offset * widget.maxLines;
return SelectionArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MarkdownBody(
shrinkWrap: true,
styleSheet: MarkdownStyleSheet.fromTheme(Theme.of(context)).copyWith(
a: widget.style,
p: widget.style,
blockquote: widget.style.copyWith(backgroundColor: Colors.transparent, color: Colors.black),
blockquoteDecoration:
BoxDecoration(color: const Color(0xFF35343B), borderRadius: BorderRadius.circular(4)),
code: widget.style.copyWith(
backgroundColor: Colors.transparent,
decoration: TextDecoration.none,
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
data: widget.isExpanded
? widget.text
: widget.text.length > maxChars
? widget.text.substring(0, maxChars)
: widget.text,
),
// Text(
// widget.text,
// style: widget.style,
// maxLines: widget.isExpanded ? 10000 : widget.maxLines,
// overflow: TextOverflow.ellipsis,
// ),
if (isOverflowing)
InkWell(
onTap: () => widget.toggleExpand(),
child: Padding(
padding: const EdgeInsets.only(top: 4.0),
child: Text(
widget.isExpanded ? collapseLabel : expandLabel,
style: TextStyle(
color: Colors.deepPurple,
fontWeight: FontWeight.w500,
fontSize: widget.style.fontSize,
),
),
),
),
],
),
);
}
}