How to implement Nested ListView in Flutter?

后端 未结 6 1913
我寻月下人不归
我寻月下人不归 2020-12-08 05:00

What is the preferred way to achieve a nested ListView, or in other words, ListView Widgets that could be included within a scrollable parent?

Imagine a \"Reports\"

相关标签:
6条回答
  • 2020-12-08 05:19

    Adding physics: ClampingScrollPhysics() and shrinkWrap: true did the trick for me.

    sample code:

    @override
    Widget build(BuildContext context) {
      return Container(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Expanded(
              child: ListView.builder(
                  shrinkWrap: true,
                  itemCount: 123,
                  itemBuilder: (BuildContext context, int index) {
                    return Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text('Parent'),
                        ListView.builder(
                            itemCount: 2,
                            physics: ClampingScrollPhysics(), 
                            shrinkWrap: true,
                            itemBuilder: (BuildContext context, int index) {
                              return Text('Child');
                            }),
                      ],
                    );
                  }),
            )
          ],
        ),
      );
    }
    
    0 讨论(0)
  • 2020-12-08 05:35

    For inner Listview I have just added below code and it solved for me

    shrinkWrap: true,
    physics: ScrollPhysics(),
    
    0 讨论(0)
  • 2020-12-08 05:40

    For child ListView, use that parameter:

    shrinkWrap: true,
    physics: ClampingScrollPhysics(),
    
    0 讨论(0)
  • 2020-12-08 05:40

    Thanks to Serdar Polat:

    ListView.builder( // outer ListView
      itemCount: 4,
      itemBuilder: (_, index) {
        return Column(
          children: [
            Container(
              color: Colors.blue,
              alignment: Alignment.center,
              child: Text('Header $index'),
            ),
            ListView.builder( // inner ListView
              shrinkWrap: true, // 1st add
              physics: ClampingScrollPhysics(), // 2nd add
              itemCount: 10,
              itemBuilder: (_, index) => ListTile(title: Text('Item $index')),
            )
          ],
        );
      },
    )
    
    0 讨论(0)
  • 2020-12-08 05:42

    If you want to have the inner ListView be scrollable independently of the main scroll view, you should use NestedScrollView. Otherwise, use a CustomScrollView.

    Here is some code illustrating the NestedScrollView approach.

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(new MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'Flutter Demo',
          theme: new ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: new MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new NestedScrollView(
            headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
              return <Widget>[
                new SliverAppBar(
                  pinned: true,
                  title: new Text('Flutter Demo'),
                ),
              ];
            },
            body: new Column(
              children: <Widget>[
                new FlutterLogo(size: 100.0, colors: Colors.purple),
                new Container(
                  height: 300.0,
                  child: new ListView.builder(
                    itemCount: 60,
                    itemBuilder: (BuildContext context, int index) {
                      return new Text('Item $index');
                    },
                  ),
                ),
                new FlutterLogo(size: 100.0, colors: Colors.orange),
              ],
            ),
          ),
        );
      }
    }
    
    0 讨论(0)
  • 2020-12-08 05:43

    Screenshot:


    Code:

    var _container = Container(
      height: 200,
      color: Colors.blue,
      margin: EdgeInsets.symmetric(vertical: 10),
    );
    
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(title: Text("ListView")),
        body: Padding(
          padding: const EdgeInsets.all(40.0),
          child: ListView( // parent ListView
            children: <Widget>[
              _container,
              _container,
              Container(
                height: 200, // give it a fixed height constraint
                color: Colors.teal,
                // child ListView
                child: ListView.builder(itemBuilder: (_, i) => ListTile(title: Text("Item ${i}"))),
              ),
              _container,
              _container,
              _container,
            ],
          ),
        ),
      );
    }
    
    0 讨论(0)
提交回复
热议问题