Flutter中的Text换行问题

1.问题说明

flutter的Text默认不支持自动换行,如果文字过长,会导致文字溢出到屏幕之外。

例如如下情况,Text换行就会失效:

Row->Column->Text

2.如何实现换行

找到Text被包裹的所有的Row(必须是Row),在Row内部,使用Expanded包住里面的元素。

嵌套关系如下:

Row->Expanded->Clumn->Text

Dart 复制代码
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: Column(
            children: [Text("我是一段很长的文字我是一段很长的文字我是一段很长的文字我是一段很长的文字")],
          ),
        ),
      ],
    );
  }

3.嵌套的太深

Dart 复制代码
Widget build(BuildContext context) {
  return Row(
    children: [
      Expanded(
        child: Column(
          children: [
            Row(
              children: [
                Expanded(
                  child: Column(
                    children: [
                      Row(
                        children: [
                          Expanded(child: Text("我是一段很长的文字我是一段很长的文字我是一段很长的文字"))
                        ],
                      )
                    ],
                  ),
                ),
              ],
            )
          ],
        ),
      )
    ],
  );
}

Tips:Column里放Expaned会报错,因为没有限制Column高度,内部Expanded展开会让高度变得无穷大,因此会报错。需要指定Column高度,例如套一层SizedBox(height:200)。

相关推荐
Nerd(BUG版)16 小时前
Flutter学习day5--事件手势与命中测试
学习·flutter
传奇开心果20 小时前
【Flutter踩坑排雷】第5课:扩展方法冲突坑|同名扩展引发编译歧义,故障规避手段
android·flutter
雪铃儿2 天前
Flutter 双端热更新新方案:基于 Dart 官方动态模块的 flutter_patcher 1.0.0
flutter·hotfix
好的佩奇2 天前
Dart 之方法、操作符与可调用对象
android·flutter·dart
恋猫de小郭2 天前
Flutter Impeller Vulkan 内存持续上涨,问题没想到是文字阴影
android·前端·flutter
传奇开心果3 天前
【Flutter踩坑排雷】第3课:const 误用坑|const 错误使用引发 UI 异常与性能损耗
android·flutter
行者-全栈开发3 天前
Flutter 插件 device_calendar_plus 开源鸿蒙适配实战:从权限弹窗失灵到循环日程跑通
flutter·跨平台·harmonyos·arkts·日历·适配·开源鸿蒙
恋猫de小郭3 天前
Flutter 开始支持 Jetpack Compose Platform View
android·前端·flutter
SoaringHeart3 天前
Flutter 查缺补漏:@JsonKey 属性全解,让模型层不再被后端字段牵着走
flutter