Flutter: ListView not scrollable, not bouncing

前端 未结 6 2165
醉梦人生
醉梦人生 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 22:21

    To always have the scroll enabled on a ListView you can wrap the original scroll phisics you want with the AlwaysScrollableScrollPhysics class. More details here. If you want you can specify a parent or rely on the default.

    Here is your example with the option added:

    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 ListView(
            physics: const AlwaysScrollableScrollPhysics(), // new
            controller: _controller,
            children: [
              new Container(
                height: 40.0,
                color: Colors.blue,
              ),
              new Container(
                height: 40.0,
                color: Colors.red,
              ),
              new Container(
                height: 40.0,
                color: Colors.green,
              ),
            ]
        );
      }
    }
    

提交回复
热议问题