From 1dfb5bd121efcc005f60fcbfc18f37517d2ccc2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C3=ABl=20Perret?= Date: Tue, 2 Sep 2025 19:16:40 +0200 Subject: [PATCH] feat : replace ReadMore by custom readmoretext --- .dart_tool/package_config.json | 6 -- .dart_tool/package_config_subset | 4 -- lib/ui/common/ReadMoreText.dart | 100 ++++++++++++++++++++++++++++ lib/ui/common/post_card_widget.dart | 14 ++-- pubspec.lock | 8 --- pubspec.yaml | 1 - 6 files changed, 105 insertions(+), 28 deletions(-) create mode 100644 lib/ui/common/ReadMoreText.dart diff --git a/.dart_tool/package_config.json b/.dart_tool/package_config.json index 597875c..65d9905 100644 --- a/.dart_tool/package_config.json +++ b/.dart_tool/package_config.json @@ -445,12 +445,6 @@ "packageUri": "lib/", "languageVersion": "3.0" }, - { - "name": "readmore", - "rootUri": "file:///C:/Users/Yael/AppData/Local/Pub/Cache/hosted/pub.dev/readmore-3.0.0", - "packageUri": "lib/", - "languageVersion": "3.0" - }, { "name": "recase", "rootUri": "file:///C:/Users/Yael/AppData/Local/Pub/Cache/hosted/pub.dev/recase-4.1.0", diff --git a/.dart_tool/package_config_subset b/.dart_tool/package_config_subset index 6736c23..91beb2f 100644 --- a/.dart_tool/package_config_subset +++ b/.dart_tool/package_config_subset @@ -278,10 +278,6 @@ pubspec_parse 3.0 file:///C:/Users/Yael/AppData/Local/Pub/Cache/hosted/pub.dev/pubspec_parse-1.3.0/ file:///C:/Users/Yael/AppData/Local/Pub/Cache/hosted/pub.dev/pubspec_parse-1.3.0/lib/ -readmore -3.0 -file:///C:/Users/Yael/AppData/Local/Pub/Cache/hosted/pub.dev/readmore-3.0.0/ -file:///C:/Users/Yael/AppData/Local/Pub/Cache/hosted/pub.dev/readmore-3.0.0/lib/ recase 2.12 file:///C:/Users/Yael/AppData/Local/Pub/Cache/hosted/pub.dev/recase-4.1.0/ diff --git a/lib/ui/common/ReadMoreText.dart b/lib/ui/common/ReadMoreText.dart new file mode 100644 index 0000000..8cfe19b --- /dev/null +++ b/lib/ui/common/ReadMoreText.dart @@ -0,0 +1,100 @@ +import 'package:flutter/gestures.dart'; +import 'package:flutter/material.dart'; + + +class ReadMoreText extends StatefulWidget { + const ReadMoreText( + this.text, { + Key? key, + this.trimLines = 2, + this.collapsedText = '... read more', + this.expandedText = ' read less', + this.textStyle, + }) : assert(text != null), + super(key: key); + + final String text; + final int trimLines; + final String collapsedText; + final String expandedText; + final TextStyle? textStyle; + + @override + ReadMoreTextState createState() => ReadMoreTextState(); +} + +class ReadMoreTextState extends State { + bool _readMore = true; + void _onTapLink() { + setState(() => _readMore = !_readMore); + } + + @override + Widget build(BuildContext context) { + final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(context); + final colorClickableText = Colors.blue; + final widgetColor = Colors.black; + TextSpan link = TextSpan( + text: _readMore ? widget.collapsedText : widget.expandedText, + style: TextStyle( + color: colorClickableText, + ), + recognizer: TapGestureRecognizer()..onTap = _onTapLink + ); + Widget result = LayoutBuilder( + builder: (BuildContext context, BoxConstraints constraints) { + assert(constraints.hasBoundedWidth); + final double maxWidth = constraints.maxWidth; + // Create a TextSpan with data + final text = TextSpan( + text: widget.text, + ); + // Layout and measure link + TextPainter textPainter = TextPainter( + text: link, + textDirection: TextDirection.rtl,//better to pass this from master widget if ltr and rtl both supported + maxLines: widget.trimLines, + ellipsis: '...', + ); + textPainter.layout(minWidth: constraints.minWidth, maxWidth: maxWidth); + final linkSize = textPainter.size; + // Layout and measure text + textPainter.text = text; + textPainter.layout(minWidth: constraints.minWidth, maxWidth: maxWidth); + final textSize = textPainter.size; + // Get the endIndex of data + int? endIndex; + final pos = textPainter.getPositionForOffset(Offset( + textSize.width - linkSize.width, + textSize.height, + )); + endIndex = textPainter.getOffsetBefore(pos.offset); + var textSpan; + if (textPainter.didExceedMaxLines) { + textSpan = TextSpan( + text: _readMore + ? widget.text.substring(0, endIndex) + : widget.text, + style: widget.textStyle ?? TextStyle( + color: widgetColor, + ), + children: [link], + ); + } else { + textSpan = TextSpan( + text: widget.text, + style: widget.textStyle ?? TextStyle( + color: widgetColor, + ), + ); + } + return RichText( + softWrap: true, + overflow: TextOverflow.clip, + text: textSpan, + ); + }, + ); + return result; + } +} \ No newline at end of file diff --git a/lib/ui/common/post_card_widget.dart b/lib/ui/common/post_card_widget.dart index 53090df..2d701cd 100644 --- a/lib/ui/common/post_card_widget.dart +++ b/lib/ui/common/post_card_widget.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'dart:async'; -import 'package:readmore/readmore.dart'; +import 'ReadMoreText.dart'; class PostCardWidget extends StatefulWidget { final String title; @@ -105,15 +105,11 @@ class _PostCardWidgetState extends State { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - // Contenu - ReadMoreText( - widget.content, - trimMode: TrimMode.Line, + ReadMoreText(widget.content, trimLines: 2, - colorClickableText: Theme.of(context).colorScheme.primary, - trimCollapsedText: 'Voir plus', - trimExpandedText: 'Voir moins', - moreStyle: Theme.of(context).textTheme.bodyMedium?.copyWith( + collapsedText: '... Voir plus', + expandedText: ' Voir moins', + textStyle: Theme.of(context).textTheme.bodyMedium?.copyWith( color: Colors.white, height: 1.4, ), diff --git a/pubspec.lock b/pubspec.lock index cdbef08..50b99e9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -581,14 +581,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.0" - readmore: - dependency: "direct main" - description: - name: readmore - sha256: e8fca2bd397b86342483b409e2ec26f06560a5963aceaa39b27f30722b506187 - url: "https://pub.dev" - source: hosted - version: "3.0.0" recase: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 891bd77..2b3268d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,6 @@ dependencies: google_fonts: ^6.2.1 http: ^1.2.2 intl: any - readmore: ^3.0.0 stacked: ^3.4.0 stacked_services: ^1.1.0