如何使appbar的标题居中

Chr*_*ian 54 flutter

我试图将标题文本集中在一个既有前导又有尾随动作的应用栏中.

@override
Widget build(BuildContext context) {
  final menuButton = new PopupMenuButton<int>(
    onSelected: (int i) {},
    itemBuilder: (BuildContext ctx) {},
    child: new Icon(
      Icons.dashboard,
    ),
  );

  return new Scaffold(
    appBar: new AppBar(
      // Here we take the value from the MyHomePage object that
      // was created by the App.build method, and use it to set
      // our appbar title.
      title: new Text(widget.title, textAlign: TextAlign.center),
      leading: new IconButton(
          icon: new Icon(Icons.accessibility),
          onPressed: () {},
      ),
      actions: [
        menuButton,
      ],
    ),
    body: new Center(
      child: new Text(
        'Button tapped $_counter time${ _counter == 1 ? '' : 's' }.',
      ),
    ),
    floatingActionButton: new FloatingActionButton(
      onPressed: _incrementCounter,
      tooltip: 'Increment',
      child: new Icon(Icons.add),
    ), // This trailing comma makes auto-formatting nicer for build methods.
  );
}
Run Code Online (Sandbox Code Playgroud)

这很有效,除了标题在左边对齐,如下图所示:

标题左下角

当我尝试将标题包含在中心时,它似乎在左边太多了:

@override
Widget build(BuildContext context) {
  final menuButton = new PopupMenuButton<int>(
    onSelected: (int i) {},
    itemBuilder: (BuildContext ctx) {},
    child: new Icon(
      Icons.dashboard,
    ),
  );

  return new Scaffold(
    appBar: new AppBar(
      // Here we take the value from the MyHomePage object that
      // was created by the App.build method, and use it to set
      // our appbar title.
      title: new Center(child: new Text(widget.title, textAlign: TextAlign.center)),
      leading: new IconButton(
          icon: new Icon(Icons.accessibility),
          onPressed: () {},
      ),
      actions: [
        menuButton,
      ],
    ),
    body: new Center(
      child: new Text(
        'Button tapped $_counter time${ _counter == 1 ? '' : 's' }.',
      ),
    ),
    floatingActionButton: new FloatingActionButton(
      onPressed: _incrementCounter,
      tooltip: 'Increment',
      child: new Icon(Icons.add),
    ), // This trailing comma makes auto-formatting nicer for build methods.
  );
}
Run Code Online (Sandbox Code Playgroud)

没有很好的中心

我想要一个解决方案,让标题文本在2个图标之间完美居中.非常感谢,

Col*_*son 130

将标题居中是iOS上的默认设置.在Android上,AppBar标题默认为左对齐,但您可以通过centerTitle: true作为参数传递给AppBar构造函数来覆盖它.

  • 这甚至不影响我的标题对齐。 (3认同)
  • 在这种情况下,标题似乎不完全位于中心:/ (2认同)

ann*_*fey 26

我遇到了同样的问题,当我将
mainAxisSize: MainAxisSize.min添加到我的 Row 小部件时,它终于起作用了。我希望这有帮助!

 return new Scaffold(
      appBar: new AppBar(
        // Here we take the value from the MyHomePage object that
        // was created by the App.build method, and use it to set
        // our appbar title.
        title: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text(
              widget.title,
            ),
          ],
        ),

        leading: new IconButton(
          icon: new Icon(Icons.accessibility),
          onPressed: () {},
        ),
        actions: [
          menuButton,
        ],
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)


Sim*_*ier 14

在我的情况下,我想要一个标志/图像居中而不是文本。在这种情况下,centerTitle还不够(不知道为什么,我有一个 svg 文件,也许这就是原因......),例如,这个:

appBar: AppBar(centerTitle: true, title: AppImages.logoSvg)
Run Code Online (Sandbox Code Playgroud)

不会真正使图像居中(加上图像可能太大等)。对我有用的是这样的代码:

appBar: AppBar(centerTitle: true,
    title: ConstrainedBox(
        constraints: BoxConstraints(maxHeight: 35, maxWidth: 200),
        child: AppImages.logoSvg)),
Run Code Online (Sandbox Code Playgroud)


小智 14

您可以使用centerTitleappBar部分中的属性来将标题居中:

centerTitle: true
Run Code Online (Sandbox Code Playgroud)


小智 7

appbar:AppBar(
centerTitle: true,
title:Text("HELLO")
)
Run Code Online (Sandbox Code Playgroud)


Ann*_*ngh 6

如果您想创建自定义应用程序栏标题,可以使用不同的方法。例如,您想要在应用程序栏的中心放置图像和文本,然后添加

appBar: AppBar(
          title: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(
                Icons.your_app_icon,
                color: Colors.green[500],
              ),
              Container(
                  padding: const EdgeInsets.all(8.0), child: Text('YourAppTitle'))
            ],

          ),
  )
Run Code Online (Sandbox Code Playgroud)

在这里,我们创建了一个以孩子为中心的Rowwith 。MainAxisAlignment.center然后我们添加了两个子项 - 一个图标和一个Container带有文本的子项。我将Text小部件包裹在 中Container以添加必要的填充。


小智 6

这是我在应用栏上设置centerTitle的方法:

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: new AppBar(
    centerTitle: true ,
    title: new Text("Login"),
  ),
  body: new Container(
    padding: EdgeInsets.all(18.0),
      key: formkey,
        child: ListView(
        children: buildInputs() + buildSubmitButton(),
      ),
   ) 
);
}
Run Code Online (Sandbox Code Playgroud)


My *_*Car 5

尝试以下代码:

\n
AppBar(\n  centerTitle: true,\n  \xe2\x80\xa6\n),\n
Run Code Online (Sandbox Code Playgroud)\n