在抽屉头部移除额外的空格

huangapple go评论72阅读模式
英文:

Remove extra space in drawer header

问题

我想要实现这个抽屉标题

但是我的抽屉有很多空白。如何去除抽屉标题中的额外空间?

代码

class AppDrawer extends HookWidget {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          _createHeader(),
          _createDrawerItem(
            icon: Icons.contacts,
            text: 'Contacts',
            onTap: () {},
          ),
          _createDrawerItem(
            icon: Icons.event,
            text: 'Events',
            onTap: () {},
          ),
        ],
      ),
    );
  }

  Widget _createDrawerItem(
      {required IconData icon,
      required String text,
      required GestureTapCallback onTap}) {
    return ListTile(
      title: Row(
        children: <Widget>[
          Icon(icon),
          Padding(
            padding: const EdgeInsets.only(left: 8.0),
            child: Text(text),
          )
        ],
      ),
      onTap: onTap,
    );
  }

  Widget _createHeader() {
    return const DrawerHeader(
        decoration: BoxDecoration(color: Colors.green),
        child: ListTile(
          title: Text("Name"),
          subtitle: Text("Email"),
          dense: true,
          leading: CircleAvatar(child: Icon(Icons.people)),
        ));
  }
}
英文:

I want achieve this drawer header
在抽屉头部移除额外的空格

But mine have lot of space.How to remove the extra space in drawer header?

在抽屉头部移除额外的空格

Code

class AppDrawer extends HookWidget {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: &lt;Widget&gt;[
          _createHeader(),
          _createDrawerItem(
            icon: Icons.contacts,
            text: &#39;Contacts&#39;,
            onTap: () {},
          ),
          _createDrawerItem(
            icon: Icons.event,
            text: &#39;Events&#39;,
            onTap: () {},
          ),
        ],
      ),
    );
  }

  Widget _createDrawerItem(
      {required IconData icon,
      required String text,
      required GestureTapCallback onTap}) {
    return ListTile(
      title: Row(
        children: &lt;Widget&gt;[
          Icon(icon),
          Padding(
            padding: const EdgeInsets.only(left: 8.0),
            child: Text(text),
          )
        ],
      ),
      onTap: onTap,
    );
  }

  Widget _createHeader() {
    return const DrawerHeader(
        decoration: BoxDecoration(color: Colors.green),
        child: ListTile(
          title: Text(&quot;Name&quot;),
          subtitle: Text(&quot;Email&quot;),
          dense: true,
          leading: CircleAvatar(child: Icon(Icons.people)),
        ));
  }
}

Ignore this line epfokeofkpekf efewfefeefefefefefefewf fewfef fefef
fefjoej jifjeojfo oijoewfjoej oijfoijfoiejf rvw

答案1

得分: 2

你可以使用 Container 或 SizedBox 代替 DrawerHeader,以根据需求更改其高度。

英文:

Instead of DrawerHeader you can use Container or SizedBox to change its height according to the requirement.

答案2

得分: 2

不使用 DrawerHeader(),可以使用 Container()

示例:

class AppDrawer extends HookWidget {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          _createHeader(),
          _createDrawerItem(
            icon: Icons.contacts,
            text: '联系人',
            onTap: () {},
          ),
          _createDrawerItem(
            icon: Icons.event,
            text: '事件',
            onTap: () {},
          ),
        ],
      ),
    );
  }

  Widget _createDrawerItem(
      {required IconData icon,
      required String text,
      required GestureTapCallback onTap}) {
    return ListTile(
      title: Row(
        children: <Widget>[
          Icon(icon),
          Padding(
            padding: const EdgeInsets.only(left: 8.0),
            child: Text(text),
          )
        ],
      ),
      onTap: onTap,
    );
  }

  Widget _createHeader() {
    return Container(
        height: 100,
        decoration: BoxDecoration(color: Colors.green),
        child: ListTile(
          title: Text("姓名"),
          subtitle: Text("邮箱"),
          dense: true,
          leading: CircleAvatar(child: Icon(Icons.people)),
        ));
  }
}
英文:

Instead of using DrawerHeader() you can use Container().

>Example:

class AppDrawer extends HookWidget {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: &lt;Widget&gt;[
          _createHeader(),
          _createDrawerItem(
            icon: Icons.contacts,
            text: &#39;Contacts&#39;,
            onTap: () {},
          ),
          _createDrawerItem(
            icon: Icons.event,
            text: &#39;Events&#39;,
            onTap: () {},
          ),
        ],
      ),
    );
  }

  Widget _createDrawerItem(
      {required IconData icon,
      required String text,
      required GestureTapCallback onTap}) {
    return ListTile(
      title: Row(
        children: &lt;Widget&gt;[
          Icon(icon),
          Padding(
            padding: const EdgeInsets.only(left: 8.0),
            child: Text(text),
          )
        ],
      ),
      onTap: onTap,
    );
  }

  Widget _createHeader() {
    return Container(
        height: 100,
        decoration: BoxDecoration(color: Colors.green),
        child: ListTile(
          title: Text(&quot;Name&quot;),
          subtitle: Text(&quot;Email&quot;),
          dense: true,
          leading: CircleAvatar(child: Icon(Icons.people)),
        ));
  }
}

huangapple
  • 本文由 发表于 2023年4月11日 14:10:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定