Flutter - 如何更改 AppBar 高度并对齐标题垂直中心?

Fet*_*mos 5 dart flutter flutter-layout

我需要更改我的 flutter 应用程序中的应用栏高度。我使用这段代码:

 Widget build(BuildContext context) {
 return Scaffold(
    appBar: PreferredSize(
      preferredSize: Size.fromHeight(100.0),
      child: AppBar(
        automaticallyImplyLeading: false, 
        flexibleSpace: Container(),
        centerTitle: true,
        title: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Icon(Icons.search),
            Icon(Icons.home),
            PopupMenuButton<String>(
              icon: Icon(Icons.menu),
                itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
                  PopupMenuItem<String>(
                    value: "1",
                    child: Text('Hello'),
                  ),
                  PopupMenuItem<String>(
                    value: "2",
                    child: Text('World'),
                  ),
                ]
            )
          ],
        ),
      ),
    ),
    body: Container());
}
Run Code Online (Sandbox Code Playgroud)

这是我的结果:

在此输入图像描述

高度已经改变,但我需要将内容垂直居中对齐。我尝试这个选项,但它不起作用:

        automaticallyImplyLeading: false, 
        flexibleSpace: Container(),
        centerTitle: true,
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

小智 16

设置包裹 AppBar 的 PreferredSize。然后将AppBar的toolBarHeight字段匹配设置为preferredSize。

appBar: PreferredSize(preferredSize: const 
        Size.fromHeight(120.0),
        child: AppBar(
          toolbarHeight: 120.0,
          automaticallyImplyLeading: false,
          flexibleSpace: Container(),
          centerTitle: true,
          backgroundColor: Colors.transparent,
          elevation: 0,
          shape: const Border(
            bottom: BorderSide(width: 1, color: Colors.black12),
          ),
)
Run Code Online (Sandbox Code Playgroud)


小智 5

尝试这个

Widget build(BuildContext context) {
return Scaffold(
    appBar: PreferredSize(
      preferredSize: Size.fromHeight(100.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          AppBar(
            elevation: 0.0,
            automaticallyImplyLeading: false,
            flexibleSpace: Container(),
            centerTitle: true,
            title: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Icon(Icons.search),
                Icon(Icons.home),
                PopupMenuButton<String>(
                    icon: Icon(Icons.menu),
                    itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
                      PopupMenuItem<String>(
                        value: "1",
                        child: Text('Hello'),
                      ),
                      PopupMenuItem<String>(
                        value: "2",
                        child: Text('World'),
                      ),
                    ]
                )
              ],
            ),
          ),
        ],
      ),
    ),
    body: Container());
 }
Run Code Online (Sandbox Code Playgroud)


ped*_*ech 3

https://dartpad.dartlang.org/115b02f36456fe9579cb8daf092bd906

class MyAppBar extends StatelessWidget with PreferredSizeWidget {
  final double appBarHeight = 120.0;
  @override
  get preferredSize => Size.fromHeight(appBarHeight);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          AppBar(
            automaticallyImplyLeading: false,
            elevation: 0,
            title: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Icon(Icons.search),
                Icon(Icons.home),
                PopupMenuButton<String>(
                  icon: Icon(Icons.menu),
                  itemBuilder: (BuildContext context) =>
                      <PopupMenuEntry<String>>[
                    PopupMenuItem<String>(
                      value: "1",
                      child: Text('Hello'),
                    ),
                    PopupMenuItem<String>(
                      value: "2",
                      child: Text('World'),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
      decoration: BoxDecoration(
        boxShadow: <BoxShadow>[
          BoxShadow(
              color: Colors.black54,
              blurRadius: 15.0,
              offset: Offset(0.0, 0.75))
        ],
        color:
            ThemeData.dark().appBarTheme.color ?? ThemeData.dark().primaryColor,
      ),
    );
  }
}

Run Code Online (Sandbox Code Playgroud)

在此输入图像描述