Skip to content

Instantly share code, notes, and snippets.

@vasyafromrussia
Last active March 14, 2020 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasyafromrussia/642aeca8a546c9bee122825bcfc2fc89 to your computer and use it in GitHub Desktop.
Save vasyafromrussia/642aeca8a546c9bee122825bcfc2fc89 to your computer and use it in GitHub Desktop.
Flutter text painter
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