【问题标题】:flutter text widget overflow and multiline颤动文本小部件溢出和多行
【发布时间】:2020-05-24 09:51:48
【问题描述】:

有时当我使用颤振进行编码时,当我在文本小部件中使用长文本时,有时我会收到溢出消息,有时消息可以正常工作并且在多行中,为什么会发生这种情况?请向我解释如何避免这个问题。

【问题讨论】:

    标签: flutter text overflow multiline longtext


    【解决方案1】:

    Text Widgt 中有一些关键属性:

      softWrap // if overflow then can wrap new line
      overflow // if overflow the overed text style
      maxLines // max lines
    

    如果 Text Widgt 的父容器的宽度小于或等于设备宽度,则溢出的文本将换行为多行,或者如果文本太长,则 Text 将引发溢出错误

    给父容器一个宽度

    例如:

     // overflow error
     Container(
     child: Column(
         children: <Widget>[
           Text("hellohellohellohellohellohellohellohellohe" +
              "llohellohellohellohellohellohellohellohellohe" +
             "llohellohellohellohellohellohello"),
           Text("dd")
         ],
       ),
    )
    

    给父容器固定宽度

    // overflow will change to multiple lines, notice padding and margin's effect
    
     Container(
     width: 100,
     child: Column(
         children: <Widget>[
           Text("hellohellohellohellohellohellohellohellohe" +
              "llohellohellohellohellohellohellohellohellohe" +
             "llohellohellohellohellohellohello"),
           Text("dd")
         ],
       ),
    )
    

    或者让 Text 使用 Expended 或 Flexible 填充父容器

    Expanded(
     child: Column(
         children: <Widget>[
           Text("hellohellohellohellohellohellohellohellohe" +
              "llohellohellohellohellohellohellohellohellohe" +
             "llohellohellohellohellohellohello"),
           Text("dd")
         ],
       ),
    )
    
    
    
    // or 
    
    Flexible(
     child: Column(
         children: <Widget>[
           Text("hellohellohellohellohellohellohellohellohe" +
              "llohellohellohellohellohellohellohellohellohe" +
             "llohellohellohellohellohellohello"),
           Text("dd")
         ],
       ),
    )
    

    【讨论】:

    • 嗨!感谢您的贡献。这似乎是有用的建议。你会编辑你的答案来整理缩进吗?照原样,阅读起来有点困难。
    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 2020-06-17
    • 2021-06-06
    • 2020-07-23
    • 2019-03-22
    • 2021-08-24
    • 2021-07-18
    • 2020-09-10
    相关资源
    最近更新 更多