skip to Main Content

I have a question about flutter text overflow.

When I set maxLines: 1, overflow: TextOverflow.ellipsis, I have a issue that in case if second(or each other word except first one) word is large word, it will be replaced with 3 dot at all.

So
In case it I have text "Some laaaaaaaaaaarge text" I will have result like:

Screen Start|Some ...         | Screen end

I want to have something like:

Screen Start|Some laaaaaaaa...| Screen end

Also I maxLines: 1, softWrap: true, overflow: TextOverflow.ellipsis, but it doesn’t helps

3

Answers


  1. Try to put your Text Widget inside a Sized container.

    Like this.

    ...
    SizedBox(
        child : MyTextWidget(),
        width : myScreenHeight * 0.5,
    ),
    ...
    
    Login or Signup to reply.
  2. Text(
      "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
      overflow: TextOverflow.ellipsis,
      maxLines: 1,
    ),
    

    Output:

    enter image description here

    Login or Signup to reply.
  3. I was having the same issue and it seems like this behaviour of TextOverflow.ellipsis is by design as mentioned here: https://github.com/flutter/flutter/issues/18761#issuecomment-514413734

    I found this solution which uses a custom EllipsizedText widget and seems to work quite nicely*: https://itnext.io/writing-custom-widgets-in-flutter-part-1-ellipsizedtext-a0efdc1368a8

    Direct link to the implementation of EllipsizedText on GitHub:
    https://github.com/MatrixDev/Flutter-CustomWidgets/blob/main/lib/part1_ellipsized_text/ellipsized_text.dart

    * Apparently it can’t use a DefaultTextStyle as is. I fixed this by adding

    TextStyle _style = const TextStyle();
    
    // ignore: avoid_setters_without_getters
    set style(TextStyle style) {
      if (_style != style) _style = style;
    }
    

    to RenderEllipsizedText, and changing _layoutText() to

    double _layoutText({required int length, required double minWidth}) {
        final text = _widget.text;
        final style = _style;
        //...
    

    Now I just changed createRenderObject() to

    RenderObject createRenderObject(BuildContext context) {
        final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(context);
        TextStyle? effectiveTextStyle = style;
        if (style == null || style!.inherit) {
          effectiveTextStyle = defaultTextStyle.style.merge(style);
        }
    
        return RenderEllipsizedText()
          ..widget = this
          ..style = effectiveTextStyle!;
      }
    

    to grab the DefaultTextStyle.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search