flutter中嵌套ListView时页面不滚动

Page doesnot scroll when nesting ListView in flutter

我有一个 ListView(2) 嵌套在另一个 ListView(1) 中。

每当我用容器替换 2 时,整个页面都可以正常滚动,但是一旦我在彼此内部启用两个列表视图,它们就会停止滚动。

这是主要的 ListView 实现

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(20.0),
      child: ListView(
        primary: true, //does not have any effect
        physics: const AlwaysScrollableScrollPhysics(),
        children: [
          Container( ...etc.

这是第一个列表视图中的第二个列表视图实现:

Expanded(
    child: ListView(
      primary: false,
      padding: const EdgeInsets.only(top: 20.0),
      children: snapshot.map((data) => _buildListItem(context, data)).toList(),
    ),
  );

Expanded 不能用于 ListView

尝试使用 SizedBox 并将高度设置为所需的数字

如果您希望 ListView(2) 占据整个屏幕(甚至更多),试试这个:

  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          Container(
            color: Colors.pink,
            width: 400,
            height: 800
          ),
          SizedBox(
            height: MediaQuery.of(context).size.height,
            child: ListView(
              children: List.generate(300, (i) => Text(i.toString()))
            ),
          )
        ]
      )
    );
  }