如何删除 Flutter 中 AppBar 前导图标周围的额外填充

How to remove extra padding around AppBar leading icon in Flutter

在我的 Flutter 应用中,我有这个 AppBar

Widget setAppBar(){
  return new AppBar(
    title: addAppBarTextWidget('Explore'),
    elevation: 0.0,
    leading: addLeadingIcon(),
    actions: <Widget>[
      addAppBarActionWidget(Constant.iconNotification, 22.0, 16.0, 8.0),
      addAppBarActionWidget(Constant.iconProfile, 30.0, 30.0, 15.0)
    ],
  );
}

Widget addLeadingIcon(){
  return new Container(
    height: 25.0,
    width: 25.0,
    padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
    margin: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
    child: new Stack(
      alignment: AlignmentDirectional.center,
      children: <Widget>[
        new Image.asset(
          Constant.iconNotification,
          width: 25.0,
          height: 25.0,
        ),
        new FlatButton(
            onPressed: (){
              onLeadingShowCategoryClick();
            }
        )
      ],
    ),
  );
}

看起来像:

As you can see on the AppBar, there is some extra padding around the leading icon. How can I remove this extra padding.

您不能这样做,因为它是一个预定义的小部件。 但是,您可以这样做:

appBar: AppBar(
  automaticallyImplyLeading: false, // Don't show the leading button
  title: Row(
    mainAxisAlignment: MainAxisAlignment.start,
    crossAxisAlignment: CrossAxisAlignment.center,
    children: <Widget>[
      IconButton(
        onPressed: () => Navigator.pop(context),
        icon: Icon(Icons.arrow_back, color: Colors.white),
      ),
      // Your widgets here
    ],
  ),
),

其中 automaticallyImplyLeading: true 隐藏了前导 IconButton,因此您可以添加自己的小部件。

只需添加一个名为 titleSpacing 的 属性,

样本

appBar: AppBar(
        leading: Icon(Icons.android),
        titleSpacing: 0,
        title: Text(widget.title),
      ),

如果您使用 Material 包中的小部件,它们的定义符合 Material 设计规范 document。因此,如果您的修改违反了此规范,您必须构建自己的小部件,而不是使用 Material 小部件。

根据 Adrian 的一些输入完成工作解决方法。

return Scaffold(
  key: _scaffoldKey,
  appBar: AppBar(
    titleSpacing: 0.0,
    title: Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        IconButton(
          icon: Icon(Icons.menu),
          onPressed: () => _scaffoldKey.currentState.openDrawer(),
        ),
        Stack(
          alignment: Alignment.center,
          children: <Widget>[
            IconButton(
              icon: Icon(Icons.mail_outline),
              onPressed: _onClickNotification,
            ),
            Positioned(
              top: 12.0,
              right: 10.0,
              width: 10.0,
              height: 10.0,
              child: Container(
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: AppColors.notification,
                ),
              ),
            )
          ],
        ),
        Expanded(
          child: Center(child: Text('test')),
        )
      ],
    ),
    automaticallyImplyLeading: false,
    centerTitle: true,
    actions: <Widget>[
      Row(
        children: <Widget>[
          Text('Online'),
          Switch(
            value: true,
            onChanged: (bool value) {},
          )
        ],
      )
    ],
  ),
  drawer: Drawer(
    child: _buildDrawer(),
  ),
  body: Container(
    color: Colors.orange,
  ),
);

如果您只需要像我一样将图标向左移动一点,您仍然可以使用前导 属性 并在 IconButton 上设置对齐方式:

    leading: Builder(
      builder: (BuildContext context) {
        return IconButton(
          onPressed: () => Navigator.pop(context),
          icon: MyIcon(),
          alignment: Alignment(-0.5, 0.0), // move icon a bit to the left
        );
      },
    ),

简答:

AppBar(
  leadingWidth: 8, // <-- Use this
  centerTitle: false, // <-- and this
  leading: Icon(Icons.android),
  title: Text('Title'),
)

更多定制:

AppBar(
  leading: Transform.translate(
    offset: Offset(-15, 0),
    child: Icon(Icons.android),
  ),
  titleSpacing: -30,
  centerTitle: false,
  title: Text("Title"),
)

如果您不想使用任何领先的小部件:

AppBar(
  title: Text('Title'),
  centerTitle: false,
  titleSpacing: 0,
)

只需设置 titleSpacingautomaticallyImplyLeading 即可移除 space

喜欢

AppBar(
        titleSpacing: 0,
        automaticallyImplyLeading: false,
)

你可以试试这个,效果很好。当您设置 leading = null 时,领先小部件的额外 space 将被删除。

appBar: new AppBar(
        leading: null,
        titleSpacing: 0.0,
        title: new Text("title"),
      ),

添加标题间距将解决此问题。

 AppBar(
   centerTitle: false,
   automaticallyImplyLeading: false,
   titleSpacing: 0
 )

对于 Sliver Appbar Padding

  sliver: SliverAppBar(
            pinned: true,
            expandedHeight: 400.0,
            flexibleSpace: FlexibleSpaceBar(
              centerTitle: false,
              titlePadding: EdgeInsets.zero,
              title: Text('Appbar'),
            ),
          ),