-1

Flutter我想显示简单的ListView内部showGeneralDialog对话框。但我收到此错误:

RenderBox was not laid out: RenderFittedBox#d8a97 relayoutBoundary=up1
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1694 pos 12: 'hasSize'

showGeneralDialog(
    barrierLabel: "Label",
    barrierDismissible: true,
    barrierColor: Colors.black.withOpacity(0.5),
    transitionDuration: const Duration(milliseconds: 700),
    context: context,
    pageBuilder: (context, anim1, anim2) {
      return Align(
        alignment: Alignment.center,
        child: BuildPagesPopupMenu(),
      );
    },
),

class BuildPagesPopupMenu extends StatelessWidget {
  final List<MenuItems> _menuItems = [
    MenuItems(menuTitle: 'test1 ', icon: Icons.settings),
    MenuItems(menuTitle: 'test2 ', icon: Icons.list),
  ];

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: ListView.separated(
            itemBuilder: (context, index) {
              return ListTile(
                title: Text(_menuItems[index].menuTitle),
                leading: Icon(_menuItems[index].icon),
              );
            },
            separatorBuilder: (context, builder) {
              return const Divider();
            },
            itemCount: _menuItems.length,
          ),
        ),
      ],
    );
  }
}

class MenuItems {
  final String menuTitle;
  final IconData icon;

  MenuItems({@required this.menuTitle, @required this.icon});
}
4

1 回答 1

0

要解决此错误,您需要为 Expanded 小部件内的 ListView 提供大小。

我建议您将 ListView 包装在 SizedBox 中并给它一个高度。

例子:

 Expanded(
      child: SizedBox(
       height: 200.0,
       child: ListView.separated(
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(_menuItems[index].menuTitle),
            leading: Icon(_menuItems[index].icon),
          );
        },
        separatorBuilder: (context, builder) {
          return const Divider();
        },
        itemCount: _menuItems.length,
      ),
     ), 
    ),
于 2020-04-01T18:22:27.717 回答