【问题标题】:Flutter- wrapping text [duplicate]Flutter-环绕文本[重复]
【发布时间】:2019-01-26 14:00:53
【问题描述】:

我想随着文本的增长而换行。我搜索并尝试使用几乎所有内容进行换行,但文本仍然停留在一行并从屏幕溢出。 有谁知道如何实现这一目标? 非常感谢任何帮助!

Positioned(
    left: position.dx,
    top: position.dy,
    child: new Draggable(
      data: widget.index,
      onDragStarted: widget.setDragging,
      onDraggableCanceled: (velocity, offset) {
        setState(() {
          position = offset;
          widget.secondCallback(offset, widget.index);
          widget.endDragging();
        });
      },
      child: new GestureDetector(
        onTap: () {
          widget.callback(widget.caption, widget.index);
        },
        child: new Text(
            widget.caption.caption,
            style: new TextStyle(
              color: widget.caption.color,
              fontSize: widget.caption.fontSize,
            ),
          ),
      ),
      feedback: new Material(
        type: MaterialType.transparency,
        child: new Text(
          widget.caption.caption,
          style: new TextStyle(
              color: widget.caption.color,
              fontSize: widget.caption.fontSize),
          softWrap: true,
        ),
      ),
    ));

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    Flexible 成功了

    new Container(
           child: Row(
             children: <Widget>[
                Flexible(
                   child: new Text("A looooooooooooooooooong text"))
                    ],
            ));
    

    这是关于如何排列小部件的官方文档https://flutter.dev/docs/development/ui/layout#lay-out-multiple-widgets-vertically-and-horizontally

    请记住,FlexibleExpanded 只能在 ColumnRowFlex 中使用,因为 Incorrect use of ParentDataWidget

    解决方案不仅仅是Flexible

    【讨论】:

    • 对单个子小部件使用行和列不是一个好主意
    • 这也可以用Expanded() 代替,但是Expanded() 让孩子填满剩余的可用空间。可以在 Flutter 的 Inspector 工具中观察,更好地理解。
    • 我使用了Flexible() 小部件,它被用词了。您应该编辑您的答案,这将是公认的解决方案。
    • Flexible 可以解决问题,但 Container 允许您添加填充。这是关于如何排列小部件的官方文档flutter.dev/docs/development/ui/…,正如@KonstantinKozirev 所说,您也可以使用 Expanded。
    • 当 Text 嵌套在容器中时,这会引发不正确的 ParentDataWidget 错误,因此它的 Container -> Flexible -> Text as Flexible 只能在行、列和 Flex 中使用?
    【解决方案2】:
    Container(
      color: Color.fromRGBO(224, 251, 253, 1.0),
      child: ListTile(
        dense: true,
        title: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            RichText(
              textAlign: TextAlign.left,
              softWrap: true,
              text: TextSpan(children: <TextSpan>
              [
                TextSpan(text: "hello: ",
                    style: TextStyle(
                        color: Colors.black, fontWeight: FontWeight.bold)),
                TextSpan(text: "I hope this helps",
                    style: TextStyle(color: Colors.black)),
              ]
              ),
            ),
          ],
        ),
      ),
    ),
    

    【讨论】:

      【解决方案3】:

      尝试Wrap 小部件在文本增长时换行:

      示例:

      Wrap(
        direction: Axis.vertical, //Vertical || Horizontal
        children: <Widget>[
          Text(
            'Your Text',
            style: TextStyle(fontSize: 30),
          ),
          Text(
            'Your Text',
            style: TextStyle(fontSize: 30),
          ),
        ],
      ),
      

      【讨论】:

      • 如果 Text 小部件是 Column 的子小部件,则此解决方案不起作用。还有什么建议吗?谢谢
      【解决方案4】:

      使用省略号

      Text(
        "This is a long text",
        overflow: TextOverflow.ellipsis,
      ),
      


      使用淡入淡出

      Text(
        "This is a long text",
        overflow: TextOverflow.fade,
        maxLines: 1,
        softWrap: false,
      ),
      


      使用剪辑

      Text(
        "This is a long text",
        overflow: TextOverflow.clip,
        maxLines: 1,
        softWrap: false,
      ),
      


      注意:

      如果您在Row 中使用Text,您可以将Text 上方放在Expanded 中,例如:

      Expanded(
        child: AboveText(),
      )
      

      【讨论】:

        【解决方案5】:

        您可以使用灵活的小部件包裹小部件,而不是使用文本小部件的溢出属性设置文本的属性。 你必须设置 TextOverflow。剪辑 例如:-

        Flexible
        (child: new Text("This is Dummy Long Text",
         style: TextStyle(
         fontFamily: "Roboto",
         color: Colors.black,
         fontSize: 10.0,
         fontWeight: FontWeight.bold),
         overflow: TextOverflow.clip,),)
        

        希望这对某人有所帮助:)

        【讨论】:

        • 这应该是正确的答案。更少的代码和重点(小部件的实际用途)
        • 灵活只是关闭我所有的文字,不显示任何内容
        【解决方案6】:

        如果您要包装单个文本小部件,您可以使用 灵活扩展 小部件。

        Expanded(
          child: Text('Some lengthy text for testing'),
        )
        

        Flexible(
          child: Text('Some lengthy text for testing'),
        )
        

        对于多个小部件,您可以选择Wrap小部件。更多详情请查看this

        【讨论】:

        • 如果 Text 小部件是 Column 的子级,则两种解决方案都不起作用。还有什么建议吗?谢谢。
        • 在我看来,推荐 FlexibleExpanded 而不提 Column / Row 是有害的。
        【解决方案7】:

        您可以使用灵活,在这种情况下 person.name 可以是一个长名称(Labels 和 BlankSpace 是返回小部件的自定义类):

        new Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
           new Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                new Flexible(
                    child: Labels.getTitle_2(person.name,
                    color: StyleColors.COLOR_BLACK)),
                BlankSpace.column(3),
                Labels.getTitle_1(person.likes())
              ]),
            BlankSpace.row(3),
            Labels.getTitle_2(person.shortDescription),
          ],
        )
        

        【讨论】:

          【解决方案8】:

          使用扩展

           Expanded(
                      child: new Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          new Text(_name, style: Theme.of(context).textTheme.subhead),
                          new Container(
                            margin: const EdgeInsets.only(top: 5.0),
                            child: new Text(text),
                          ),
                        ],
                      ),
          

          【讨论】:

          • 这个。您也可以使用不带 flex 的灵活(即 flex 设置为 1)以填充 100% 的可用空间。
          • 很棒的东西。好,易于。感谢您的帮助!
          【解决方案9】:

          在我的一个项目中,我将Text 实例包裹在Containers 周围。此特定代码示例具有两个堆叠的 Text 对象。

          这是一个代码示例。

              //80% of screen width
              double c_width = MediaQuery.of(context).size.width*0.8;
          
              return new Container (
                padding: const EdgeInsets.all(16.0),
                width: c_width,
                child: new Column (
                  children: <Widget>[
                    new Text ("Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 Long text 1 ", textAlign: TextAlign.left),
                    new Text ("Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2, Long Text 2", textAlign: TextAlign.left),
                  ],
                ),
              );
          

          [编辑] 为容器添加了宽度约束

          【讨论】:

          • 感谢您的帮助,但仍然无法正常工作。我认为这与堆栈或定位或可拖动有关。因为层次结构是堆栈-> 定位-> 可拖动的。我说的对吗?
          • 非常感谢,它运行良好。你让我很开心!
          • 尝试使用灵活小部件。不需要硬编码容器宽度。
          • 它没有用我尝试了很多次。你有什么想法吗?
          • Perl 分支会说:“有不止一种方法可以做到这一点”。当时它对我很有帮助,当时 Flutter 还很新。我很高兴现在有一条官方路径。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多