Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #39 #62

Merged
merged 2 commits into from Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions example/lib/main.dart
Expand Up @@ -91,7 +91,7 @@ class _DemoAppState extends State<DemoApp> {
key: const Key('showMore'),
padding: const EdgeInsets.all(16),
child: ReadMoreText(
'Flutter is Google’s mobile UI open source framework to build high-quality native (super fast) interfaces for iOS and Android apps with the unified codebase.',
'Flutter is Google’s mobile UI open source framework to build high-quality native (super fast) interfaces for iOS and Android apps with the unified codebase.😊😊😊',
trimMode: _trimMode,
trimLines: _trimLines,
trimLength: _trimLength,
Expand All @@ -109,7 +109,7 @@ class _DemoAppState extends State<DemoApp> {
Padding(
padding: const EdgeInsets.all(16),
child: ReadMoreText(
'Flutter(https://flutter.dev/) has its own UI components, along with an engine to render them on both the <@123> and <@456> platforms <@999> http://google.com #read_more. Most of those UI components, right out of the box, conform to the guidelines of #Material Design.',
'Flutter(https://flutter.dev/) has its own UI components, along with an engine to render them on both the <@123> and <@456> platforms <@999> http://google.com #read_more. Most of those UI components, right out of the box, conform to the guidelines of #Material Design.😊😊😊',
trimMode: _trimMode,
trimLines: _trimLines,
trimLength: _trimLength,
Expand Down Expand Up @@ -203,7 +203,7 @@ class _DemoAppState extends State<DemoApp> {
Padding(
padding: const EdgeInsets.all(16),
child: ReadMoreText(
'The Flutter framework builds its layout via the composition of widgets, everything that you construct programmatically is a widget and these are compiled together to create the user interface. ',
'The Flutter framework builds its layout via the composition of widgets, everything that you construct programmatically is a widget and these are compiled together to create the user interface. 😊😊😊',
trimMode: _trimMode,
trimLines: _trimLines,
trimLength: _trimLength,
Expand Down Expand Up @@ -255,7 +255,7 @@ class _DemoAppState extends State<DemoApp> {
),
const TextSpan(
text:
'everything that you construct programmatically is a widget and these are compiled together to create the user interface.',
'everything that you construct programmatically is a widget and these are compiled together to create the user interface.😊😊😊',
),
],
),
Expand Down
13 changes: 10 additions & 3 deletions lib/readmore.dart
Expand Up @@ -317,12 +317,14 @@ class ReadMoreTextState extends State<ReadMoreText> {
}

final TextSpan dataTextSpan;
// Constructed by ReadMoreText.rich(...)
if (widget.richData != null) {
assert(_isTextSpan(widget.richData!));
dataTextSpan = TextSpan(
style: effectiveTextStyle,
children: [widget.richData!],
);
// Constructed by ReadMoreText(...)
} else {
dataTextSpan = _buildAnnotatedTextSpan(
data: widget.data!,
Expand Down Expand Up @@ -393,6 +395,7 @@ class ReadMoreTextState extends State<ReadMoreText> {
late final TextSpan textSpan;
switch (widget.trimMode) {
case TrimMode.Length:
// Constructed by ReadMoreText.rich(...)
if (widget.richData != null) {
final trimResult = _trimTextSpan(
textSpan: dataTextSpan,
Expand All @@ -411,8 +414,9 @@ class ReadMoreTextState extends State<ReadMoreText> {
} else {
textSpan = dataTextSpan;
}
// Constructed by ReadMoreText(...)
} else {
if (widget.trimLength < widget.data!.length) {
if (widget.trimLength < widget.data!.runes.length) {
final effectiveDataTextSpan = isCollapsed
? _trimTextSpan(
textSpan: dataTextSpan,
Expand Down Expand Up @@ -554,13 +558,16 @@ class ReadMoreTextState extends State<ReadMoreText> {
var spanEndIndex = spanStartIndex;

final text = textSpan.text;
// In the case of RichText(Constructed by ReadMoreText.rich(...)),
// "textSpan.text" is null. Therefore, in the process below, this function
// is recursively called for the contents of the children of TextSpan.
if (text != null) {
final textLen = text.length;
final textLen = text.runes.length;
spanEndIndex += textLen;

if (spanEndIndex >= endIndex) {
final nextSpan = TextSpan(
text: text.substring(0, endIndex - spanStartIndex),
text: String.fromCharCodes(text.runes, 0, endIndex - spanStartIndex),
children: null, // remove potential children
style: textSpan.style,
recognizer: textSpan.recognizer,
Expand Down