feat : replace ReadMore by custom readmoretext
This commit is contained in:
@@ -445,12 +445,6 @@
|
|||||||
"packageUri": "lib/",
|
"packageUri": "lib/",
|
||||||
"languageVersion": "3.0"
|
"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",
|
"name": "recase",
|
||||||
"rootUri": "file:///C:/Users/Yael/AppData/Local/Pub/Cache/hosted/pub.dev/recase-4.1.0",
|
"rootUri": "file:///C:/Users/Yael/AppData/Local/Pub/Cache/hosted/pub.dev/recase-4.1.0",
|
||||||
|
|||||||
@@ -278,10 +278,6 @@ pubspec_parse
|
|||||||
3.0
|
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/
|
||||||
file:///C:/Users/Yael/AppData/Local/Pub/Cache/hosted/pub.dev/pubspec_parse-1.3.0/lib/
|
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
|
recase
|
||||||
2.12
|
2.12
|
||||||
file:///C:/Users/Yael/AppData/Local/Pub/Cache/hosted/pub.dev/recase-4.1.0/
|
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 'package:flutter/material.dart';
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:readmore/readmore.dart';
|
import 'ReadMoreText.dart';
|
||||||
|
|
||||||
class PostCardWidget extends StatefulWidget {
|
class PostCardWidget extends StatefulWidget {
|
||||||
final String title;
|
final String title;
|
||||||
@@ -105,15 +105,11 @@ class _PostCardWidgetState extends State<PostCardWidget> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
// Contenu
|
ReadMoreText(widget.content,
|
||||||
ReadMoreText(
|
|
||||||
widget.content,
|
|
||||||
trimMode: TrimMode.Line,
|
|
||||||
trimLines: 2,
|
trimLines: 2,
|
||||||
colorClickableText: Theme.of(context).colorScheme.primary,
|
collapsedText: '... Voir plus',
|
||||||
trimCollapsedText: 'Voir plus',
|
expandedText: ' Voir moins',
|
||||||
trimExpandedText: 'Voir moins',
|
textStyle: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||||
moreStyle: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
|
||||||
color: Colors.white,
|
color: Colors.white,
|
||||||
height: 1.4,
|
height: 1.4,
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -581,14 +581,6 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.3.0"
|
version: "1.3.0"
|
||||||
readmore:
|
|
||||||
dependency: "direct main"
|
|
||||||
description:
|
|
||||||
name: readmore
|
|
||||||
sha256: e8fca2bd397b86342483b409e2ec26f06560a5963aceaa39b27f30722b506187
|
|
||||||
url: "https://pub.dev"
|
|
||||||
source: hosted
|
|
||||||
version: "3.0.0"
|
|
||||||
recase:
|
recase:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ dependencies:
|
|||||||
google_fonts: ^6.2.1
|
google_fonts: ^6.2.1
|
||||||
http: ^1.2.2
|
http: ^1.2.2
|
||||||
intl: any
|
intl: any
|
||||||
readmore: ^3.0.0
|
|
||||||
stacked: ^3.4.0
|
stacked: ^3.4.0
|
||||||
stacked_services: ^1.1.0
|
stacked_services: ^1.1.0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user