How to implement Nested ListView in Flutter?

后端 未结 6 1912
我寻月下人不归
我寻月下人不归 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: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 [
                new SliverAppBar(
                  pinned: true,
                  title: new Text('Flutter Demo'),
                ),
              ];
            },
            body: new Column(
              children: [
                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),
              ],
            ),
          ),
        );
      }
    }
    

提交回复
热议问题