简体   繁体   English

如何在 flutter 中使用列中的文本时删除文本溢出

[英]How to remove text overflow while using text in column in flutter

I am using a row, inside it, each text is a column and I want that whenever my text overflows, it should automatically go to next line, I tried everything, but it won't work.我正在使用一行,在其中,每个文本都是一列,我希望每当我的文本溢出时,它应该自动 go 到下一行,我尝试了一切,但它不起作用。 When I tried to use expanded or flexible, it gave error.当我尝试使用扩展或灵活时,它给出了错误。 I tried to make maxlines 2 and then use expanded/flexible, I also gave it a container as a parent widget and made it's height big enough, still error.我尝试制作 maxlines 2,然后使用扩展/灵活,我还给它一个容器作为父小部件,并使其高度足够大,仍然错误。

Here is the code snippet -这是代码片段 -

class RouteItems extends StatelessWidget {
  final int index;
  final NodeElement data;

  RouteItems({required this.index, required this.data});

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Column(
              children: [
                SvgPicture.asset(
                  'assets/icons/road-straight-line-top-view.svg',
                  height: 15,
                  width: 80,
                ),
                SvgPicture.asset(
                  filterRoutesIcon("${data.node?.type}"),
                  height: 30,
                  width: 30,
                ),
                SvgPicture.asset(
                  'assets/icons/road-straight-line-top-view.svg',
                  height: 15,
                  width: 80,
                ),
              ],
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20.0),
              child: Container(
                //height: 60,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Text(
                          '${data.node?.name}',
                          style: TextStyle(fontSize: 12.0),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ],
                    ),
                    Text(
                      data.eta!.getFormattedDate('hh:mm a, d MMM'),
                      style: TextStyle(fontSize: 10.0, color: colorDarkGrey),
                    )
                  ],
                ),
              ),
            ),

          ],
        ),
        Align(
          alignment: Alignment.centerRight,
          child: Icon(
            Icons.timer,
          ),
        ),
      ],
    );
  }
}

在此处输入图像描述

Try wrapping the text you want to not overflow in a Flexible() widget.尝试将您不想溢出的文本包装在 Flexible() 小部件中。 That will make sure it doesn't take up more space in the row than is available.这将确保它不会在行中占用比可用空间更多的空间。

This Row can be removed, what is the purpose of it?这个Row可以去掉,它的目的是什么?

Row(
  children: [
    Text(
      '${data.node?.name}',
      style: TextStyle(fontSize: 12.0),
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
    ),
  ],
)

You can try removing the maxLines: 1 , and adding width constraints to your Text eg wrapping it with a SizedBox , this way the text will wrap to the next line:您可以尝试删除maxLines: 1 ,并为您的Text添加宽度约束,例如用SizedBox包装它,这样文本将换行到下一行:

SizedBox(
  width: 200,
  child: Text(
    '${data.node?.name}',
    style: TextStyle(fontSize: 12.0),
  ),
)

You can also add or replace width paramater of SizedBox widget with您还可以添加或替换 SizedBox 小部件的宽度参数

width: MediaQuery.of(context).size.width,

to constraint the Text child to the device screen width.将 Text 子项限制为设备屏幕宽度。

You can try this aproach你可以试试这个方法

Expanded(
Row(
children: [
Card(
child: Text(" My awseome no overflow text"),),
..Other Widgets
]
),
);

Try below code hope its helpful to you.试试下面的代码希望它对你有帮助。 Just wrap your Text widget inside Expanded of Flexible只需将您的 Text 小部件包装在 Expanded 的 Flexible

Refer Expanded here参考这里Expanded

Refer Flexible here 在此处参考Flexible

Or Try to add your Inside Row widgets wrap it with Expanded or Flexible refer my answer here or here or here hope its helpful to you或者尝试添加您的 Inside Row 小部件,用ExpandedFlexible将其包裹起来,请参阅我的答案hereherehere希望它对您有所帮助

       Row(
            children: [
              Expanded(
                child: Text(
                  '{data.node?.name}{data.node?.name}{data.node?.name}{data.node?.name}'
                  '{data.node?.name}{data.node?.name}{data.node?.name}{data.node?.name}'
                  '{data.node?.name}{data.node?.name}{data.node?.name}{data.node?.name}'
                  '{data.node?.name}{data.node?.name}',
                  style: TextStyle(fontSize: 12.0),
                   
                ),
              ),
            ],
          ),

Your result Screen->您的结果屏幕-> 输出图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 拨动开关文字溢出 - Flutter Toggle Switch Text Overflow ListView Flutter中的文本溢出问题 - Text overflow issue in ListView Flutter 如何使用API​​ 8上的AppCompat删除文本和徽标或在Android ActionBar上添加溢出? - How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? 使用 textswitcher 切换文本时删除边距 - Remove margin while switching text using textswitcher 在 Tap 上显示 Flutter 文本选择工具栏溢出 - Flutter text selection toolbar overflow showed on Tap 如何在使用“CarouselSlider”时根据移动设备大小调整图像大小并在该图像上放置按钮文本颤动? - How to adjust images size with respect to mobile size while using "CarouselSlider" and put button text on that image in flutter? 如何使用按钮单击删除 UnderlineSpan() 文本 - How to remove UnderlineSpan() text using button click 如何在颤振中刷新文本 - How to refresh text in flutter Flutter 键盘一直隐藏文本字段并显示溢出像素 - Flutter keyboard keeps hiding the text field and showing overflow pixels Flutter Android 模拟器中的文本溢出,在 IOS 模拟器中工作正常 - Flutter text overflow in Android emulator, works fine in IOS emulator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM