Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh widget or page in Flutter without ListView et al

I want refresh my page without having a scrollable content, i.e. without having a ListView et al.

When I want use RefreshIndicator, the documentation says it needs a scrollable widget like ListView.

But if I want to refresh and want to use the refresh animation of RefreshIndicator without using a ListView, GridView or any other scorllable widget, how can i do that?

like image 626
evergreen Avatar asked Jul 12 '19 02:07

evergreen


People also ask

Which widget in flutter helps you refresh screen?

RefreshIndicator is a widget in a flutter. It is used to update the data in the app. RefreshIndicator will trigger a refresh when the list is over-scrolled.


2 Answers

You can simply wrap your content in a SingleChildScrollView, which will allow you to use a RefreshIndicator. In order to make the pull down to refresh interaction work, you will have to use AlwaysScrollableScrollPhysics as your content will most likely not cover more space than available without a scroll view:

RefreshIndicator(
      onRefresh: () async {
        // Handle refresh.
      },
      child: SingleChildScrollView(
        physics: const AlwaysScrollableScrollPhysics(),
        child: /* your content */,
      ),
    );
like image 117
creativecreatorormaybenot Avatar answered Sep 17 '22 06:09

creativecreatorormaybenot


You can just use GestureDetector, I have created a sample for you, but it's not perfect, you can customize it to your own needs, it just detects when you swipe from the top.

class Test extends StatefulWidget {
  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {

  var refresh=false;

  void  refreshData(){
    if(!refresh){
      refresh=true;
      print("Refreshing");
      Future.delayed(Duration(seconds: 4),(){
        refresh =false;
        print("Refreshed");
      });
    }
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: Text("Test"),
          centerTitle: true,
        ),
        body: GestureDetector(
          child: Container(
            color: Colors.yellow,
            height: double.infinity,
            width: double.infinity,
            child: Center(child: Text('TURN LIGHTS ON')),
          ),
          onVerticalDragUpdate: (DragUpdateDetails details){
            print("direction ${details.globalPosition.direction}");
            print("distance ${details.globalPosition.distance}");
            print("dy ${details.globalPosition.dy}");
            if(details.globalPosition.direction < 1 && (details.globalPosition.dy >200 && details.globalPosition.dy < 250)){

              refreshData();

            }
          },
        ));
  }
} 
like image 37
Lakhwinder Singh Avatar answered Sep 17 '22 06:09

Lakhwinder Singh