Last active
March 14, 2020 12:20
-
-
Save vasyafromrussia/642aeca8a546c9bee122825bcfc2fc89 to your computer and use it in GitHub Desktop.
Flutter text painter
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
class MyTextPainter extends CustomPainter { | |
final TextSpan text; | |
final int maxLines; | |
final String ellipsis; | |
MyTextPainter({this.text, this.ellipsis, this.maxLines}) : super(); | |
@override | |
bool shouldRepaint(CustomPainter oldDelegate) => false; | |
@override | |
void paint(Canvas canvas, Size size) { | |
TextPainter painter = TextPainter( | |
text: text, | |
maxLines: maxLines, | |
textDirection: TextDirection.ltr, | |
)..ellipsis = this.ellipsis; | |
painter.layout(maxWidth: size.width); | |
painter.paint(canvas, Offset(0, 0)); | |
} | |
} | |
class HomePage extends StatelessWidget { | |
HomePage({Key key}) : super(key: key); | |
var someVeryLongText = // Some realy long text | |
@override | |
Widget build(BuildContext context) => Scaffold( | |
body: SafeArea( | |
child: Container( | |
child: CustomPaint( | |
size: Size(double.infinity, 300), | |
painter: MyTextPainter( | |
text: TextSpan(text: someVeryLongText, style: TextStyle(color: Colors.black54)), | |
ellipsis: "... more", | |
maxLines: 4, | |
), | |
), | |
), | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment