简体   繁体   English

Flutter - 如何在一行中显示文本和图标?

[英]Flutter - How to show text and icon in one line in row?

I am using row widget with three child widgets: Text Icon Text我正在使用带有三个子小部件的行小部件:文本图标文本

I want all of them to appear in single line same level horizontally and drop to new line if text increases.我希望它们全部水平显示在同一水平的单行中,如果文本增加则下降到新行。

I am using below code for Row widget but last Text widget is not aligned correctly我正在为 Row 小部件使用以下代码,但最后一个 Text 小部件未正确对齐在此处输入图像描述

The text dropping should start below the " Tap " and " on the right hand " is not aligned文本放置应从“ Tap ”下方开始,并且“ on the right hand ”未对齐

Row(
  mainAxisAlignment: MainAxisAlignment.start,
  mainAxisSize: MainAxisSize.min,
  children: <Widget>[
    Text(
      'Tap ',
      style: TextStyle(
        fontSize: 17,
      ),
    ),
    Icon(Icons.add),
    Expanded(
      child: Text(
        'on the right hand corner to start a new chat.',
        style: TextStyle(
          fontSize: 17,
        ),
      ),
    )
  ],
)

Use Text.rich with WidgetSpan to put icon inside text (inline)使用Text.richWidgetSpan将图标放在文本中(内联)

Text.rich(
  TextSpan(
    style: TextStyle(
      fontSize: 17,
    ),
    children: [
      TextSpan(
        text: 'Tap',
      ),
      WidgetSpan(
        child: Icon(Icons.add),
      ),
      TextSpan(
        text: 'on the right hand corner to start a new chat.',
      )
    ],
  ),
)

Flutter has WidgetSpan() to add a widget inside the RichText(). Flutter 有 WidgetSpan() 来在 RichText() 中添加一个小部件。

RichText(
  text: TextSpan(
    children: [
      TextSpan(
        text: "Click ",
      ),
      WidgetSpan(
        child: Icon(Icons.add, size: 14),
      ),
      TextSpan(
        text: " to add",
      ),
    ],
  ),
)

Above code will produce:上面的代码将产生:

Click > to add点击 > 添加

You can treat the child of WidgetSpan like the usual widget.您可以像对待普通小部件一样对待 WidgetSpan 的子项。

You need put sinlein line crossAxisAlignment: CrossAxisAlignment.start inside Row你需要把 sinlein 线crossAxisAlignment: CrossAxisAlignment.start放在 Row 里面

Row (
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
                  Icon(Icons.add, size: 14),
                  Expanded(
                      child: Text("your multiline text "),
                      ),
                    ),
                ],
    
)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM