How to flag (Enable / Disable) Flutter RefreshIndicator

Viewed 902

I want to flag my RefreshIndicator with a boolean param.

Is it possible? What is the trick? Because there are no many options...

return RefreshIndicator(
    onRefresh: () async {
        return Future.value();
    },
    backgroundColor: Colors.white,
    color: Colors.pink,
    strokeWidth: 2.75,
    child: SingleChildScrollView(...)
)

enter image description here

3 Answers

To disable RefreshIndicator pass notificationPredicate function that returns false.

After doing that refresh indicator won't be shown or onRefresh called.

RefreshIndicator(
  notificationPredicate: enabled ? (_) => true : (_) => false,
  ...
);

Create a boolean called _showRefreshIndicator and based on that either render SingleChildScrollView with RefreshIndicator or render only SingleChildScrollView.

return _showRefreshIndicator ? RefreshIndicator(
    onRefresh: () async {
        return Future.value();
    },
    backgroundColor: Colors.white,
    color: Colors.pink,
    strokeWidth: 2.75,
    child: SingleChildScrollView(...)
) : SingleChildScrollView(...);

You could use a ternary operator in the onRefresh function itself since ternary operators resolve to true or false you can set a boolean variable and toggle between behaviors in your onRefresh function like this.

bool refresh = true;
return RefreshIndicator(
    onRefresh: () async {
        refresh ? return Future.value() : doSomeOtherThing;
    },
    backgroundColor: Colors.white,
    color: Colors.pink,
    strokeWidth: 2.75,
    child: SingleChildScrollView(...)
)
Related