22

I'm new to Flutter. How to limit text when I use TextSpan widget?

My code

Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.symmetric(vertical: 8.0), child: Row( children: <Widget>[ Expanded( flex: 2, child: Row( children: <Widget>[ Stack( children: <Widget>[ ClipRRect( borderRadius: BorderRadius.all(Radius.circular(8)), child: Image.asset( lastPlayedGame.imagePath, height: 60, width: 45, fit: BoxFit.cover, ), ), Positioned( left: 8, right: 8, top: 0, bottom: 0, child: Container( decoration: BoxDecoration( shape: BoxShape.circle, color: Colors.white, ), child: Icon( Icons.play_arrow, color: Colors.red, ), ), ), ], ), Padding( padding: const EdgeInsets.symmetric(horizontal: 12), child: RichText( text: TextSpan(children: [ TextSpan(text: lastPlayedGame.name, style: headingTwoTextStyle,), TextSpan(text: '\n'), TextSpan(text: "${lastPlayedGame.hoursPlayed} hours played", style: bodyTextStyle), ]), ), ) ], ), ), Expanded( child: GameProgressWidget(screenWidth: screenWidth, gameProgress: gameProgress), ), ], ), ); } } 

When run on my Android device, I get an error:

A RenderFlex overflowed by 15 pixels on the right.

enter image description here

How to limit text length? Maybe check if text is maximum of screen, will show Assasin's Creed... (with dots maybe?)

6 Answers 6

46

If you want to use your RichText Widget inside a Row and prevent the overflow with an ellipsis, you first have to wrap it inside a Flexible. The Flexible shows the Row that the RichText can be shrinked.

After wrapping the RichText inside a Flexible, simply add overflow: TextOverflow.ellipsis to your RichText. Here a minimal example with a RichText inside an Flexible inside a Row.

Demo

import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Container( padding: EdgeInsets.all(4.0), color: Colors.lime, width: 200.0, child: Row( children: <Widget>[ Flexible( child: RichText( overflow: TextOverflow.ellipsis, strutStyle: StrutStyle(fontSize: 12.0), text: TextSpan( style: TextStyle(color: Colors.black), text: 'A very long text :)'), ), ), Container( width: 100.0, height: 100.0, color: Colors.orangeAccent, ) ], ), )), ), ); } } 
Sign up to request clarification or add additional context in comments.

Comments

33

You can also try this if you want to customize it:

Text( _name.length > 10 ? _name.substring(0, 10)+'...' : _name, style: TextStyle( color: Colors.white, fontSize: 22, fontWeight: FontWeight.w500, ), ), 

It displays the ellipsis if the length of the text is more than 10.

1 Comment

Cool , thats what i wanted
24

There's no need of using RichText. Just add your Text a parameter of overflow: TextOverflow.ellipsis and the Wrap the Text widget with Flexible

Example:

 Row( children: <Widget>[ Icon(Icons.location_on, color: Colors.grey), Flexible( child: Text(propertyModel.propertyAddress, style: AppTextStyle.headerSmall2(context), overflow: TextOverflow.ellipsis), ) ], ), 

Comments

7

Try to set overflow property:

 overflow: TextOverflow.ellipsis 

2 Comments

Hi @Munjunath I try but still same output
This alone won't work inside a Row. @Bertho Joris you can have a look at my answer, I've added the missing information.
-3
@override Widget build(BuildContext context) { return Scaffold( body: Center( child: Container( padding: EdgeInsets.all(4.0), color: Colors.lime, height: 200, width: 200.0, child: Row( children: <Widget>[ Flexible( child: Text("Mais de 200 países criaram restrições à entrada de brasileiros por conta da nossa gestão da pandemia", overflow: TextOverflow.visible), ) ], ), ) ), ); } 

1 Comment

Please add some notes or comment about how your answer help to solve OP's problem, also as I see your solution do not cover different screen sizes...
-4

Adding more to the above-selected answer. if you want to show the dots (...) after a certain number of lines you can use the maxLines option.

Example:

.... .... Flexible( child: RichText( overflow: TextOverflow.ellipsis, maxLines: 2, // this will show dots(...) after 2 lines strutStyle: StrutStyle(fontSize: 12.0), text: TextSpan( style: TextStyle(color: Colors.black), text: 'A very long text :)' ), ), ), ..... ..... 

1 Comment

It's for line limit not for text limit

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.