简体   繁体   中英

Flutter nested ListView

I have 2 ListViews the first one is one top of the page and shows stories so it's horizontal and the second one is Showing Posts so it's vertical .

What I want is that when I scroll my posts I want the storys to disappear right now they are stuck on top of the page.

Explanation Video 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(),
                    );
                  ),
                ],
              ),
            ),
          );
        }

you can put your story listview in SliverAppBar, it will hide your story list when you scroll up. here's the link https://flutter.dev/docs/cookbook/lists/floating-app-bar

You can use CustomScrollView for it like this.

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

You can add on top of Column SingleChildScrollView to load the entire attribute.

Then you can stop the scroll in the second ListView with this code :

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

    .
    .
        ),

In this case, you will have one vertical scroll that follows SingleChildScrollView and the page is completely animated.

Use singlchildscrollview as a parent widget instead of a column see the example

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')),
      ),
    ],
  ),
)`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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