feat : replace ReadMore by custom readmoretext
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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/
|
||||
|
||||
100
lib/ui/common/ReadMoreText.dart
Normal file
100
lib/ui/common/ReadMoreText.dart
Normal file
@@ -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<ReadMoreText> {
|
||||
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: <TextSpan>[link],
|
||||
);
|
||||
} else {
|
||||
textSpan = TextSpan(
|
||||
text: widget.text,
|
||||
style: widget.textStyle ?? TextStyle(
|
||||
color: widgetColor,
|
||||
),
|
||||
);
|
||||
}
|
||||
return RichText(
|
||||
softWrap: true,
|
||||
overflow: TextOverflow.clip,
|
||||
text: textSpan,
|
||||
);
|
||||
},
|
||||
);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -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<PostCardWidget> {
|
||||
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,
|
||||
),
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user