【问题标题】:Flutter nested ListViewFlutter 嵌套 ListView
【发布时间】:2021-05-19 05:35:34
【问题描述】:

我有 2 个ListViews,第一个是页面顶部,显示故事,所以它是 horizontal,第二个是显示帖子,所以它是 vertical。

我想要的是,当我 scroll 我的帖子时,我希望故事立即消失,它们被卡在页面顶部。

解说视频this

@override
        Widget build(context) {
          return Scaffold(
          
            body: SafeArea(
              child: Column(
                children: [
                
                Container(
                    padding: EdgeInsets.only(left: 15),
                    height: 90,
                    width: double.infinity,
                    child: StreamBuilder(
                      stream: masterListStart().asStream(),
                      builder: (context, snapshot) {
                    
                          return (ConnectionState.done == snapshot.connectionState)
                              //First Listview
                              ? ListView.builder(
                                  scrollDirection: Axis.horizontal,
                                  shrinkWrap: true,
                                  itemCount: storysList.length,
                                  itemBuilder: (context, index) {
                                    //
                                    StoryItems data = storysList[index];
                                    //
                                    return StoryItems(
                                      data: data,
                                    );
                                  },
                                )
                              : CircularProgressIndicator();
                        }
                      },
                    ),
                  );

                  //Second ListView
                  Expanded(
                    child:  RefreshIndicator(
                      child: ListView(
                        children: posts,
                      ),
                      key: refreshkey,
                      onRefresh: () => refreshlist(),
                    );
                  ),
                ],
              ),
            ),
          );
        }

【问题讨论】:

    标签: flutter listview nested-lists


    【解决方案1】:

    您可以将您的故事列表视图放在 SliverAppBar 中,当您向上滚动时它会隐藏您的故事列表。这是链接https://flutter.dev/docs/cookbook/lists/floating-app-bar

    【讨论】:

      【解决方案2】:

      您可以像这样使用 CustomScrollView。

           CustomScrollView(
            slivers : [
            SliverToBoxAdapter(child : Container(height : listHeight, 
            child ListView(),),),
            SliverList(delegate: SliverChildBuilderDelegate((context,index){
                              return Your Item;
                            },
                            childCount: data.length
                        ),]
      

      【讨论】:

        【解决方案3】:

        您可以在Column 之上添加SingleChildScrollView 以加载整个属性。

        然后你可以在 秒 ListView 用这个 code 停止scroll:

        ListView(
            physics: NeverScrollableScrollPhysics(),
            shrinkWrap: true, // Edit new line
        
            .
            .
                ),
        

        在这种情况下,您将在 SingleChildScrollView 之后有一个 vertical scroll,并且页面是完全动画的。

        【讨论】:

        • 您好,感谢您的回答,我这样做了,但我得到了这个错误 RenderBox was not layouted: RenderRepaintBoundary#5dc0c relayoutBoundary=up2 NEEDS-PAINT 'package:flutter/src/rendering/box.dart' : 断言失败: line 1785 pos 12: 'hasSize' 我也尝试给它一个静态高度来解决这个问题,但错误仍然出现任何想法?
        【解决方案4】:

        使用 singlchildscrollview 作为父小部件而不是列参见示例

        body: SingleChildScrollView(
            child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Text(
                'Headline',
                style: TextStyle(fontSize: 18),
              ),
              SizedBox(
                height: 200.0,
                child: ListView.builder(
                  physics: ClampingScrollPhysics(),
                  shrinkWrap: true,
                  scrollDirection: Axis.horizontal,
                  itemCount: 15,
                  itemBuilder: (BuildContext context, int index) => Card(
                        child: Center(child: Text('Dummy Card Text')),
                      ),
                ),
              ),
              Text(
                'Demo Headline 2',
                style: TextStyle(fontSize: 18),
              ),
              Card(
                child: ListTile(title: Text('Motivation $int'), subtitle: Text('this is a description of the motivation')),
              ),
              Card(
                child: ListTile(title: Text('Motivation $int'), subtitle: Text('this is a description of the motivation')),
              ),
              Card(
                child: ListTile(title: Text('Motivation $int'), subtitle: Text('this is a description of the motivation')),
              ),
              Card(
                child: ListTile(title: Text('Motivation $int'), subtitle: Text('this is a description of the motivation')),
              ),
              Card(
                child: ListTile(title: Text('Motivation $int'), subtitle: Text('this is a description of the motivation')),
              ),
            ],
          ),
        )`
        

        【讨论】:

        • 感谢您的回答,当我这样做时出现以下错误:RenderBox 未布置:RenderRepaintBoundary#30415 relayoutBoundary=up1 NEEDS-PAINT 'package:flutter/src/rendering/box.dart' :断言失败:第 1785 行 pos 12:'hasSize' 相关的导致错误的小部件是 Scaffold-[LabeledGlobalKey#c7cd1] 任何想法?
        • 它的高度问题通过媒体查询计算高度为两个列表视图设置静态高度谢谢
        猜你喜欢
        • 1970-01-01
        • 2019-01-17
        • 2023-04-08
        • 1970-01-01
        • 2017-12-29
        • 1970-01-01
        • 2020-02-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多