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

Raf*_*san 9 dart flutter flutter-layout

在我的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();
            }
        )
      ],
    ),
  );
}
Run Code Online (Sandbox Code Playgroud)

看起来像:

在此输入图像描述

正如你在AppBar上看到的那样,领先图标周围有一些额外的填充.如何删除这个额外的填充.

Vin*_*mar 21

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

样品

appBar: new AppBar(
        leading: new Icon(Icons.android),
        titleSpacing: 0.0,
        title: new Text(widget.title),
      ),
Run Code Online (Sandbox Code Playgroud)

  • 它删除标题前的填充/边距,而不删除前导图标周围的多余填充。 (4认同)
  • 这应该是公认的答案。此解决方案保留应用栏的默认行为。默认情况下,应用栏显示菜单按钮或后退按钮,具体取决于导航器。请参阅此处https://api.flutter.dev/flutter/material/AppBar/leading.html (2认同)

Cop*_*oad 21

在此处输入图片说明

简短的回答:

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

更多定制:

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

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

在此处输入图片说明

AppBar(
  title: Text('Title'),
  centerTitle: false,
  titleSpacing: 0,
)
Run Code Online (Sandbox Code Playgroud)


Bha*_*pal 16

添加标题间距可以解决这个问题。

 AppBar(
   centerTitle: false,
   automaticallyImplyLeading: false,
   titleSpacing: 0
 )
Run Code Online (Sandbox Code Playgroud)


小智 12

您无法执行此操作,因为它是预定义的小部件。但是,您可以这样做:

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
    ],
  ),
),
Run Code Online (Sandbox Code Playgroud)

哪里automaticallyImplyLeading: true隐藏了领导,IconButton以便您可以添加自己的小部件。


Ter*_*ius 9

只需设置titleSpacingautomaticallyImplyLeading删除空间

喜欢

AppBar(
        titleSpacing: 0,
        automaticallyImplyLeading: false,
)
Run Code Online (Sandbox Code Playgroud)

  • [this](/sf/answers/4055964601/)帖子中提到了这两个属性,请不要添加相同的答案。 (2认同)

小智 8

使用 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,
  ),
);
Run Code Online (Sandbox Code Playgroud)


小智 7

你可以试试这个,这很好用。当您设置前导 = null 时,前导小部件的额外空间将被删除。

appBar: new AppBar(
        leading: null,
        titleSpacing: 0.0,
        title: new Text("title"),
      ),
Run Code Online (Sandbox Code Playgroud)