Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Wrap text on overflow, like insert ellipsis or fade

I'm trying to create a line in which center text has a maximum size, and if the text content is too large, it fits in size.

I insert the TextOverflow.ellipsis property to shorten the text and inserting the triple points ... but it is not working.

main.dart

import 'package:flutter/material.dart';  void main() { runApp(new MyApp()); }  class MyApp extends StatelessWidget {   @override   Widget build(BuildContext context) {     return new MaterialApp(       home: new HomePage(),     );   } }  class HomePage extends StatelessWidget {   @override   Widget build(BuildContext context) => new Scaffold(     appBar: new AppBar(       backgroundColor: new Color(0xFF26C6DA),     ),     body: new ListView  (       children: <Widget>[         new Card(           child: new Container(             padding: new EdgeInsets.symmetric(horizontal: 16.0, vertical: 18.0),             child: new Row(               children: <Widget>[                 new Container(                   padding: new EdgeInsets.only(right: 24.0),                   child: new CircleAvatar(                     backgroundColor: new Color(0xFFF5F5F5),                     radius: 16.0,                   )                 ),                 new Container(                   padding: new EdgeInsets.only(right: 13.0),                   child: new Text(                     'Text lar...',                     overflow: TextOverflow.ellipsis,                     style: new TextStyle(                       fontSize: 13.0,                       fontFamily: 'Roboto',                       color: new Color(0xFF212121),                       fontWeight: FontWeight.bold,                     ),                   ),                 ),                 new Container(                   child: new Column(                     crossAxisAlignment: CrossAxisAlignment.end,                     children: <Widget>[                       new Row(                         children: <Widget>[                           new Text(                             'Bill  ',                             style: new TextStyle(                               fontSize: 12.0,                               fontFamily: 'Roboto',                               color: new Color(0xFF9E9E9E)                             ),                           ),                           new Text(                             '\$ -999.999.999,95',                             style: new TextStyle(                               fontSize: 14.0,                               fontFamily: 'Roboto',                               color: new Color(0xFF212121)                             ),                           ),                         ],                       ),                       new Row(                         children: <Widget>[                           new Text(                             'Limit  ',                             style: new TextStyle(                               fontSize: 12.0,                               fontFamily: 'Roboto',                               color: new Color(0xFF9E9E9E)                             ),                           ),                           new Text(                             'R\$ 900.000.000,95',                             style: new TextStyle(                               fontSize: 14.0,                               fontFamily: 'Roboto',                               color: new Color(0xFF9E9E9E)                             ),                           ),                         ],                       ),                     ]                   )                 )               ],             ),           )         ),       ]     )   ); } 

result:

enter image description here

expected:

enter image description here

like image 581
rafaelcb21 Avatar asked Jun 16 '17 02:06

rafaelcb21


People also ask

How do you wrap text overflow in flutter?

Here's how you wrap text on overflow in Flutter:Step 1: Make sure your Text widget is inside the Row widget. Step 2: Wrap your Text widget inside the Expanded widget. Step 3: Run your app.

What is soft wrap in flutter?

softWrap. Whether the text should break at soft line breaks. If false, the glyphs in the text will be positioned as if there was unlimited horizontal space.


2 Answers

You should wrap your Container in a Flexible to let your Row know that it's ok for the Container to be narrower than its intrinsic width. Expanded will also work.

screenshot

Flexible(   child: new Container(     padding: new EdgeInsets.only(right: 13.0),     child: new Text(       'Text largeeeeeeeeeeeeeeeeeeeeeee',       overflow: TextOverflow.ellipsis,       style: new TextStyle(         fontSize: 13.0,         fontFamily: 'Roboto',         color: new Color(0xFF212121),         fontWeight: FontWeight.bold,       ),     ),   ), ), 
like image 111
Collin Jackson Avatar answered Sep 27 '22 20:09

Collin Jackson


Using Ellipsis

Text(   "This is a long text",   overflow: TextOverflow.ellipsis, ), 

enter image description here


Using Fade

Text(   "This is a long text",   overflow: TextOverflow.fade,   maxLines: 1,   softWrap: false, ), 

enter image description here


Using Clip

Text(   "This is a long text",   overflow: TextOverflow.clip,   maxLines: 1,   softWrap: false, ), 

enter image description here


Note:

If you are using Text inside a Row, you can put above Text inside Expanded like:

Expanded(   child: AboveText(), ) 
like image 30
CopsOnRoad Avatar answered Sep 27 '22 19:09

CopsOnRoad