简体   繁体   中英

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在此处输入图像描述

The text dropping should start below the " Tap " and " on the right hand " is not aligned

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.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().

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.

You need put sinlein line crossAxisAlignment: CrossAxisAlignment.start inside Row

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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