如何在Flutter中实现应用栏操作文本而不是操作图标?

8
我正在尝试在Flutter应用程序中实现appbar。我已经成功添加了一个关闭应用程序的操作图标。我想要在关闭操作图标旁边显示用户名。我尝试在appar部分的操作小部件数组中添加新的Text(),但无法对齐它。有没有直接添加操作文本的方法?

enter image description here

代码:

@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),
      ),
        ],
      ),
}

1个回答

20
将您的文本小部件包装在居中小部件中,如下所示:
new Center(
    new Text(
        userName,
        textScaleFactor: 1.5,
        style: new TextStyle(
          fontSize: 12.0,
          color: Colors.white,
        ),
   ),
)

很高兴能帮到你!如果这个答案解决了你的问题,请点赞/接受这个答案。 - Dhiraj Sharma
我和这个问题的作者有相同的问题,我尝试了上面的答案,但对我没有用,并且您在这段代码中缺少一个括号。 - AllwiN
你还需要添加水平填充,以使界面看起来好看。因为通过将其包裹在Center中,它会保持靠近屏幕的右侧。 - Darsh Shah
你还需要添加水平间距以使界面看起来好看。 因为通过将其包裹在Center中,它会保持靠近屏幕的右侧。 - undefined

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