Search code examples
flutterdartrefreshpull-to-refreshindicator

flutter: no refresh indicator when using RefreshIndicator


I added the RefreshIndicator to my page, but there is no indicator visible when pull to refresh. The code is below:

class HomePage extends StatefulWidget {
  HomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<HomePage> {

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: LocalGalleryTab(),
    );
  }
}

class LocalGalleryTab extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LocalGalleryState();
  }
}

class _LocalGalleryState extends State<LocalGalleryTab> {
  @override
  Widget build(BuildContext context) {
    return new Container(child: new Center(
      child: new RefreshIndicator(
        child: Text("Local Gallery"),
        onRefresh: _refreshLocalGallery,
      ),
    ));
  }

  Future<Null> _refreshLocalGallery() async{
    print('refreshing stocks...');

  }
}

How to use the RefreshIndicator? The flutter doc does not give much infomation.


Solution

  • You need to add scroll child inside RefreshIndicator see example below

    enter image description here

    class HomePage extends StatefulWidget {
      HomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => new _MyHomePageState();
    }
    
    class _MyHomePageState extends State<HomePage> {
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text(widget.title),
          ),
          body: LocalGalleryTab(),
        );
      }
    }
    
    class LocalGalleryTab extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return _LocalGalleryState();
      }
    }
    
    class _LocalGalleryState extends State<LocalGalleryTab> {
      @override
      Widget build(BuildContext context) {
        return RefreshIndicator(
            child: ListView(
              children: List.generate(50,  (f) => Text("Item $f")),
            ),
            onRefresh: _refreshLocalGallery,
          );
      }
    
      Future<Null> _refreshLocalGallery() async{
        print('refreshing stocks...');
    
      }
    }