You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter抽屉多级菜单报错:Incorrect use of ParentDataWidget 求助

问题解决:抽屉展开二级菜单触发ParentDataWidget错误

错误原因:你在二级菜单的ListTile的leading属性中使用了Spacer组件,而Spacer必须在**Flex布局组件(如Row、Column)**内部才能正常工作,ListTile的leading插槽并非Flex容器,因此触发了Incorrect use of ParentDataWidget错误。

修复方案

将二级菜单ListTile中的Spacer替换为固定宽度的SizedBox,或者通过调整contentPadding来实现缩进效果,以下是两种可行的修复方式:

方式1:用SizedBox替代Spacer

修改二级菜单的ListTile代码:

if (showHistory) ...[
  ListTile(
    leading: const SizedBox(width: 40), // 替换Spacer为固定宽度的SizedBox
    title: const Text('Batches'),
    onTap: () {
      // Navigator.pushNamed(context, '/history');
    },
  ),
  ListTile(
    leading: const SizedBox(width: 40),
    title: const Text('Shipments'),
    onTap: () {
      // Navigator.pushNamed(context, '/history');
    },
  ),
  ListTile(
    leading: const SizedBox(width: 40),
    title: const Text('Supplier Invoices'),
    onTap: () {
      Navigator.pushNamed(context, '/supplierivcs');
    },
  ),
],

方式2:通过contentPadding实现缩进

不需要设置leading,直接调整ListTile的内边距:

if (showHistory) ...[
  ListTile(
    contentPadding: const EdgeInsets.only(left: 72), // 左侧缩进对应一级菜单图标+间距的宽度
    title: const Text('Batches'),
    onTap: () {
      // Navigator.pushNamed(context, '/history');
    },
  ),
  // 其他二级菜单ListTile同理
],

完整修复后的代码

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

class MyDrawer extends StatefulWidget {
  const MyDrawer({super.key});

  @override
  State<MyDrawer> createState() => _MyDrawerState();
}

class _MyDrawerState extends State<MyDrawer> {
  bool showHistory = false;
  bool showConfig = false;
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        children: <Widget>[
          DrawerHeader(
            child: Stack(
              children: <Widget>[
                SvgPicture.asset(
                  'assets/images/logo.svg',
                ),
              ],
            ),
          ),
          ListTile(
            leading: SvgPicture.asset(
              'assets/images/speedometer.svg',
            ),
            title: const Text('Dashboard'),
            onTap: () {
              Navigator.pushNamed(context, '/');
            },
          ),
          ListTile(
            leading: SvgPicture.asset(
              'assets/images/chemical_bottles.svg',
            ),
            title: const Text('Jobs'),
            onTap: () {
              Navigator.pushNamed(context, '/jobs');
            },
          ),
          ListTile(
            leading: SvgPicture.asset(
              'assets/images/box.svg',
            ),
            title: const Text('History'),
            onTap: () {
              setState(() {
                showHistory = !showHistory;
              });
            },
          ),
          if (showHistory) ...[
            ListTile(
              leading: const SizedBox(width: 40),
              title: const Text('Batches'),
              onTap: () {
                // Navigator.pushNamed(context, '/history');
              },
            ),
            ListTile(
              leading: const SizedBox(width: 40),
              title: const Text('Shipments'),
              onTap: () {
                // Navigator.pushNamed(context, '/history');
              },
            ),
            ListTile(
              leading: const SizedBox(width: 40),
              title: const Text('Supplier Invoices'),
              onTap: () {
                Navigator.pushNamed(context, '/supplierivcs');
              },
            ),
          ],
          ListTile(
            leading: SvgPicture.asset(
              'assets/images/cog.svg',
            ),
            title: const Text('Configurations'),
            onTap: () {
              setState(() {
                showConfig = !showConfig;
              });
            },
          ),
          if (showConfig) ...[
            ListTile(
              leading: const SizedBox(width: 40),
              title: const Text('Currency'),
              onTap: () {
                // Navigator.pushNamed(context, '/history');
              },
            )
          ]
        ],
      ),
    );
  }
}

内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 13:27:46