Flutter: ListView not scrollable, not bouncing

前端 未结 6 2171
醉梦人生
醉梦人生 2021-02-12 21:49

I have the following example (tested on an iPhone X, iOS 11):

import \'package:flutter/material.dart\';

void main() => runApp(new MyApp());

class MyApp exte         


        
6条回答
  •  情歌与酒
    2021-02-12 21:59

    I found a solution how to track the offset with lists that have a smaller content height than the viewport. Use a NotificationListener together with a CustomScrollView in the build() method like this:

    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(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => new _MyHomePageState();
    }
    
    class _MyHomePageState extends State {
      ScrollController _controller = new ScrollController();
    
      @override
      Widget build(BuildContext context) {
        return new NotificationListener(
          onNotification: _handleScrollPosition,
            child: new CustomScrollView(
                slivers: [
                  new SliverList(
                      delegate: new SliverChildListDelegate([
                        new Container(
                          height: 40.0,
                          color: Colors.blue,
                        ),
                        new Container(
                          height: 40.0,
                          color: Colors.red,
                        ),
                        new Container(
                          height: 40.0,
                          color: Colors.green,
                        ),
                      ])
                  )
                ]
            )
        );
      }
    
      bool _handleScrollPosition(ScrollNotification notification) {
        print(notification.metrics.pixels);
        return true;
      }
    }
    

    As long as there is no solution with a ScrollController only (or a "better" (more elegant)) solution, I will accept this as the answer.

提交回复
热议问题