如何在使用flutter的richText时垂直对齐图标?

6

我正在尝试创建一个简单的手势检测器,其中包含一些文本和一个图标在开始位置,但是当我尝试将图标居中时似乎遇到了难题(是的,我已经尝试过一行,但我需要宽度不超过内容范围的解决方案),以下是我目前所拥有的:

Container(
            color: Colors.white,
            child: GestureDetector(
                onTap: () => setState(() {
                      playerNames.remove(name);
                    }),
                child: RichText(
                  text: TextSpan(children: [
                    WidgetSpan(
                      child: Icon(
                        Icons.clear,
                        size: MediaQuery.of(context).size.height * 0.02,
                      ),
                    ),
                    WidgetSpan(
                      child: AutoSizeText(
                        name,
                        maxLines: 1,
                        style: new TextStyle(
                            fontSize:
                                MediaQuery.of(context).size.height * 0.021,
                            color: Color.fromRGBO(0, 0, 0, 0.71)),
                      ),
                    )
                  ]),
                )),
          )
1个回答

19

尝试在中使用

       TextSpan(
            style: TextStyle(fontSize: 24, color: Colors.black), 
            children: <InlineSpan>[ 
              TextSpan(text: 'hello'), 
              WidgetSpan( 
                alignment: ui.PlaceholderAlignment.middle, 
                child: Text( 'Jack', 
                    style: TextStyle(fontSize: 40, color: Colors.white), 
                ), 
              ), 
              TextSpan(text: ' doctor'), 
            ] 
        ),

这个“ui”是谁?它在对齐方面有问题。 - Thiago Loureiro
1
有一个名为dart:ui的库,我将其导入为ui。 简而言之,导入语句为"import "dart:ui" as ui"。 - Islomkhuja Akhrarov
5
像大多数人一样,只需删除“ui.”并写入PlaceholderAlignment.middle即可。 - Ma1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接