How to add `overflow` to SelectableText in Flutter?
Asked Answered
C

4

10

There is SelectableText widget that allows you to make text selectable. But it misses the overflow parameter, which is required...

Is there any workaround, which can make it work? Or a similar solution? Thanks!

Corrigible answered 20/4, 2021 at 12:53 Comment(3)
I think that all text should be shown to select text.Radley
If you use 'maxLines' parameter, you can limit vertical spread.Radley
@kuku not exactly. it will limit the number of lines, but they will become scrollable, which is terrible...Corrigible
S
15

Just experienced this. The problem is overflow hasn't been made the top level parameter in case of SelectableText as with Text in Flutter which suggests that it must be there, just not as the top level parameter which is true as it is in TextStyle.

Hence, to use overflow in SelectableText, change

SelectableText(
  text,
  overflow: TextOverflow.ellipsis
)

to

SelectableText(
  text,
  style: TextStyle(
    overflow: TextOverflow.ellipsis,
  ),
)
Shakiashaking answered 8/2, 2022 at 19:17 Comment(0)
H
1

Also, remember that when you want the space to expand, you can also use Expanded or Flexible. Hope it helps.

Row(children: [
                  Text('Código de inscrição: '),
                  Expanded(
                    child: SelectableText(club.SignUpCode),
                  )
                ]),
Hawaiian answered 4/8, 2022 at 16:35 Comment(0)
A
0

In my case above solutions didn't work. I found another solution using ListTile:

ListTile(
    title: SelectableText(
           text,
           toolbarOptions: const ToolbarOptions(
                  copy: true,
                  selectAll: true,
                  cut: true,
           ),
    ),
)

and it worked as overflow: TextOverflow.ellipsis for me

Admix answered 26/4, 2022 at 5:34 Comment(0)
M
-1

If the TextOverflow.clip effect is enough for you this did the trick for me:

SelectableText("My text"
              maxLines: 1,
              scrollPhysics: NeverScrollableScrollPhysics())
            
Miramontes answered 15/8, 2021 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.