6

我正在尝试在颤振应用程序中实现 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),
      ),
        ],
      ),
}

4

2 回答 2

14

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,
        ),
   ),
)
于 2018-07-05T08:19:57.230 回答
1

您还可以将您的小部件包装在Text小部件内Align并给出alignment: Alignment.center如下所示

Align(
  alignment: Alignment.center,
  child: Text(
    "Save",
    textScaleFactor: 1.5,
    style: TextStyle(
      fontSize: 12.0,
      color: Colors.white,
    ),
  ),
)

你也可以通过使用TextButton

TextButton(
  onPressed: () {},
  child: Text('Save', style: TextStyle(color: Colors.white),),
)

在此处输入图像描述

于 2021-05-08T03:03:31.353 回答