首页 > 解决方案 > 如何在 Flutter 中实现应用栏操作文本而不是操作图标?

问题描述

我正在尝试在颤振应用程序中实现 appbar。我可以添加一个关闭应用程序的操作图标。我愿意在关闭操作图标旁边显示用户名。我尝试在 appar 部分的操作小部件数组中添加 new Text() 。但无法对齐。有没有办法直接添加动作文本?

在此处输入图像描述

代码:

@override
  Widget build(BuildContext context) {
    //build a form widget using the form key we created above
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(StringRef.appName),
        actions: <Widget>[

          new Container(),

        new Text(
        userName,
        textScaleFactor: 1.5,
        style: new TextStyle(
          fontSize: 12.0,
          color: Colors.white,
        ),
      ),

      new IconButton(
        icon: new Icon(Icons.close),
        tooltip: 'Closes application',
        onPressed: () => exit(0),
      ),
        ],
      ),
}

标签: flutterdartflutter-layoutappbar

解决方案


Wrap your Text widget inside Center widget as

new Center(
    new Text(
        userName,
        textScaleFactor: 1.5,
        style: new TextStyle(
          fontSize: 12.0,
          color: Colors.white,
        ),
   ),
)

推荐阅读