Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RefreshIndicator does not disappear when refreshed in background #14619

Closed
najeira opened this issue Feb 10, 2018 · 4 comments
Closed

RefreshIndicator does not disappear when refreshed in background #14619

najeira opened this issue Feb 10, 2018 · 4 comments
Labels
f: material design flutter/packages/flutter/material repository. f: routes Navigator, Router, and related APIs. framework flutter/packages/flutter repository. See also f: labels.

Comments

@najeira
Copy link
Contributor

najeira commented Feb 10, 2018

Steps to Reproduce

  • show RefreshIndicator
  • make the app background by home button
  • wait for refresh to finish
  • make the app foreground

The RefreshIndicator#show's future is completed but indicator does not disappear.

See: https://youtu.be/Ns8yRhq65wM

import 'dart:async';
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',
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

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

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey<RefreshIndicatorState> refreshKey = new GlobalKey<RefreshIndicatorState>();
  bool _refreshing = false;
  
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text("Hello")),
      body: new RefreshIndicator(
        key: refreshKey,
        child: new Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new RaisedButton(
                onPressed: () {
                  refreshKey.currentState?.show()?.then((_) {
                    if (mounted) {
                      setState(() {
                        _refreshing = false;
                      });
                    }
                  });
                },
                child: new Text("show refresh indicator"),
              ),
              new Text(_refreshing ? "refreshing" : "NOT refreshing"),
            ],
          ),
        ),
        onRefresh: () {
          setState(() {
            _refreshing = true;
          });
            return new Future.delayed(const Duration(seconds: 5), () {});
        },
      ),
    );
  }
}

Logs

No error logs.

Flutter Doctor

[✓] Flutter (on Mac OS X 10.13.2 17C88, locale ja-JP, channel master)
    • Flutter version 0.0.25-pre.22 at /Applications/flutter
    • Framework revision f5bbc5bb76 (21 hours ago), 2018-02-09 11:16:58 +0100
    • Engine revision 9bc2efdf47
    • Tools Dart version 2.0.0-dev.20.0
    • Engine Dart version 2.0.0-edge.ea91bc9888184d5a2fc309c36656fdd325ef503c

[✓] Android toolchain - develop for Android devices (Android SDK 25.0.3)
    • Android SDK at /Applications/android-sdk-macosx
    • Android NDK at /Applications/android-sdk-macosx/ndk-bundle
    • Platform android-25, build-tools 25.0.3
    • ANDROID_HOME = /Applications/android-sdk-macosx
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_112-release-b05)

[✓] iOS toolchain - develop for iOS devices (Xcode 9.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 9.2, Build version 9C40b
    • ios-deploy 1.9.2
    • CocoaPods version 1.4.0

[✓] Android Studio (version 2.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Java version OpenJDK Runtime Environment (build 1.8.0_112-release-b05)

[✓] IntelliJ IDEA Ultimate Edition (version 2017.2.6)
    • Flutter plugin version 21.2.2
    • Dart plugin version 172.4343.25

[!] VS Code (version 0.10.11)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Dart Code extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.dart-code

[✓] Connected devices
    • iPhone X • 0EE16512-22F4-43D4-BA22-54D19CFBA86B • ios • iOS 11.2 (simulator)
@Hixie Hixie added the f: routes Navigator, Router, and related APIs. label Feb 13, 2018
@Hixie
Copy link
Contributor

Hixie commented Feb 13, 2018

might be the same underlying cause as #13818

@najeira
Copy link
Contributor Author

najeira commented Mar 19, 2018

This is my workaround:

import 'dart:async';

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

class MyRefreshIndicator extends StatefulWidget {
  const MyRefreshIndicator({
    Key key,
    @required this.child,
    this.displacement: 40.0,
    @required this.onRefresh,
    this.color,
    this.backgroundColor,
    this.notificationPredicate: defaultScrollNotificationPredicate,
  }) : assert(child != null),
       assert(onRefresh != null),
       assert(notificationPredicate != null),
       super(key: key);
  
  final Widget child;

  final double displacement;

  final RefreshCallback onRefresh;

  final Color color;

  final Color backgroundColor;

  final ScrollNotificationPredicate notificationPredicate;
  
  @override
  State<StatefulWidget> createState() {
    return new MyRefreshIndicatorState();
  }
}

class MyRefreshIndicatorState extends State<MyRefreshIndicator> with WidgetsBindingObserver {
  Completer<Null> completer;
  bool foreground = true;
  
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }
  
  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
  
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    foreground = (state == AppLifecycleState.resumed);
    if (foreground && completer != null) {
      //debugPrint("complete on didChangeAppLifecycleState");
      completer.complete();
      completer = null;
    }
  }
  
  @override
  Widget build(BuildContext context) {
    return new RefreshIndicator(
      child: widget.child,
      onRefresh: _onRefresh,
      displacement: widget.displacement,
      color: widget.color,
      backgroundColor: widget.backgroundColor,
      notificationPredicate: widget.notificationPredicate,
    );
  }
  
  Future<Null> _onRefresh() {
    final Completer<Null> completer = new Completer<Null>();
    widget.onRefresh().then((_) {
      if (foreground) {
        //debugPrint("complete on original future");
        completer.complete();
      } else {
        this.completer = completer;
      }
    });
    return completer.future;
  }
}

@zoechi zoechi added the framework flutter/packages/flutter repository. See also f: labels. label Jul 22, 2018
@goderbauer goderbauer added the f: material design flutter/packages/flutter/material repository. label Dec 28, 2018
@zoechi zoechi added this to the Goals milestone Feb 7, 2019
@kf6gpe
Copy link
Contributor

kf6gpe commented Dec 20, 2019

I can't reproduce this on 1.12.13+hotfix.5. Closing. If it's still an issue, please reopen.

@kf6gpe kf6gpe closed this as completed Dec 20, 2019
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
f: material design flutter/packages/flutter/material repository. f: routes Navigator, Router, and related APIs. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

No branches or pull requests

5 participants