抽屉的 ListView 没有显示任何内容。

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

Drawer's ListView doesn't show anything

问题

I'm trying to make a drawer for my app, but it doesn't show anything I want it to show for some reason. The console doesn't throw any errors, so I have no idea what happened. Can anyone tell me why?

class _MainDrawerState extends State<MainDrawer> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: [
            const DrawerHeader(
              decoration: BoxDecoration(
                color: Colors.cyan,
              ),
              child: Text(
                'DBug',
                style: TextStyle(
                  fontSize: 25,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            ListTile(
              title: const Text('item 1'),
              onTap: () {

                Navigator.pop(context);
              },
            ),
            ListTile(
              title: const Text('item 2'),
              onTap: () {

                Navigator.pop(context);
              },
            ),
          ],
        ),
      ),
    );
  }
}

I tried making it both a stateless and a stateful widget, didn't help in either.

英文:

i'm trying to make a drawer for my app, but it doesn't show anything i want it to show for some reason. The console doesn't throw any errors, so i have no idea what happened. Can anyone tell me why?

class _MainDrawerState extends State&lt;MainDrawer&gt; {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: [
            const DrawerHeader(
              decoration: BoxDecoration(
                color: Colors.cyan,
              ),
              child: Text(
                &#39;DBug&#39;,
                style: TextStyle(
                  fontSize: 25,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            ListTile(
              title: const Text(&#39;item 1&#39;),
              onTap: () {

                Navigator.pop(context);
              },
            ),
            ListTile(
              title: const Text(&#39;item 2&#39;),
              onTap: () {

                Navigator.pop(context);
              },
            ),
          ],
        ),
      ),
    );
  }
}

i tried making it both a stateless and a stateful widget, didn't help in either.

答案1

得分: 0

Drawer将显示在父上下文的Scaffold中,您将调用MainDrawer的地方。从_MainDrawerState中移除Scaffold

英文:

Drawer will be showed on parent context scaffold, the place you will call MainDrawer. remove Scaffold from _MainDrawerState .

class _MainDrawerState extends State&lt;MainDrawer&gt; {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: [
          const DrawerHeader(
            decoration: BoxDecoration(
              color: Colors.cyan,
            ),
            child: Text(
              &#39;DBug&#39;,
              style: TextStyle(
                fontSize: 25,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          ListTile(
            title: const Text(&#39;item 1&#39;),
            onTap: () {
              Navigator.pop(context);
            },
          ),
          ListTile(
            title: const Text(&#39;item 2&#39;),
            onTap: () {
              Navigator.pop(context);
            },
          ),
        ],
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年5月7日 16:10:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192824.html
匿名

发表评论

匿名网友

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

确定