Created
March 14, 2020 12:07
-
-
Save vasyafromrussia/208a6a919f149709572ab85824dc64dc to your computer and use it in GitHub Desktop.
Flutter expandable text step 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension _TextMeasurer on RichText { | |
List<TextBox> measure(BuildContext context, Constraints constraints) { | |
final renderObject = createRenderObject(context)..layout(constraints); | |
return renderObject.getBoxesForSelection( | |
TextSelection( | |
baseOffset: 0, | |
extentOffset: text.toPlainText().length, | |
), | |
); | |
} | |
} | |
class _ExpandableTextState extends State<ExpandableText> { | |
// ... | |
@override | |
Widget build(BuildContext context) => LayoutBuilder( | |
builder: (context, constraints) { | |
final maxLines = widget.maxLines; | |
final richText = Text.rich(widget.textSpan).build(context) as RichText; | |
final boxes = richText.measure(context, constraints); | |
if (boxes.length <= maxLines || _isExpanded) { | |
return RichText(text: widget.textSpan); | |
} else { | |
// TODO: deal with ellipsized text | |
} | |
}, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment