简体   繁体   English

在 flutter 中嵌套 ListView 时页面不滚动

[英]Page doesnot scroll when nesting ListView in flutter

I have a ListView(2) nested inside another ListView(1).我有一个嵌套在另一个 ListView(1) 中的 ListView(2)。

Whenever I replace 2 with a container, the whole page scrolls fine but as soon as I enable both list views inside each other, both stop scrolling.每当我用容器替换 2 时,整个页面都可以正常滚动,但是一旦我在彼此内部启用两个列表视图,它们都会停止滚动。

This is the main ListView implementation这是主要的 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.

And this is the second listview implementation inside the first listview:这是第一个列表视图中的第二个列表视图实现:

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

Expanded cannot be used in a ListView Expanded不能在ListView中使用

Try this using SizedBox and setting the height to a desire number尝试使用SizedBox并将高度设置为所需的数字

If you want the ListView(2) to take the entire screen (and more) try this:如果您希望 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()))
            ),
          )
        ]
      )
    );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM