GithubHelp home page GithubHelp logo

hacker1024 / declarative_refresh_indicator.dart Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 7.0 158 KB

A declarative refresh indicator alternative for Flutter that takes inspiration from the Switch and Checkbox APIs.

Home Page: https://pub.dev/packages/declarative_refresh_indicator

License: MIT License

Kotlin 1.02% Swift 9.53% Objective-C 0.31% Dart 76.61% HTML 12.52%
dart flutter refresh-indicator declarative-ui

declarative_refresh_indicator.dart's Introduction

Declarative Refresh Indicator

A declarative RefreshIndicator alternative that takes inspiration from the Switch and Checkbox APIs.

Usage

Much like the Switch and Checkbox widgets, DeclarativeRefreshIndicator takes refreshing and onRefresh arguments to receive state and dispatch events. The widget holds no state of its own.

class _MyWidgetState extends State<MyWidget> {
  var _loading = false;
  
  void _refresh() async {
    setState(() => _loading = true);
    await _getData();
    if (mounted) setState(() => _loading = false);
  }
  
  @override
  Widget build(BuildContext context) {
    return DeclarativeRefreshIndicator(
      refreshing: _loading,
      onRefresh: _refresh,
      child: /* a scrollable widget */,
    );
  }
}

Why do I need this?

While the simplistic example above isn't hard to implement with a regular RefreshIndicator, the regular RefreshIndicator becomes harder to use with event-based state management systems like BLoC and Redux, where events or actions are synchronously dispatched and no future is available.

License

MIT License

Copyright (c) 2021 hacker1024

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

declarative_refresh_indicator.dart's People

Contributors

hacker1024 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

declarative_refresh_indicator.dart's Issues

Error when calling hiding again immediately after showing

I have a loading state that sometimes only exists for a very short time (under one frame as far as I can tell), causing the following exception:

======== Exception caught by widgets library =======================================================
The following assertion was thrown building XXX:
Hide called, but not showing!
'package:declarative_refresh_indicator/declarative_refresh_indicator.dart':
Failed assertion: line 122 pos 12: '_showing'

The relevant error-causing widget was: 
  RefreshDataIndicator RefreshDataIndicator:file:///xxx/lib/something.dart:50:12
When the exception was thrown, this was the stack: 
#2      _DeclarativeRefreshIndicatorState._hide (package:declarative_refresh_indicator/declarative_refresh_indicator.dart:122:12)
#3      _DeclarativeRefreshIndicatorState.didUpdateWidget (package:declarative_refresh_indicator/declarative_refresh_indicator.dart:149:7)
#4      StatefulElement.update (package:flutter/src/widgets/framework.dart:5142:55)
#5      Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#7      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11)
#8      Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#9      StatefulElement.update (package:flutter/src/widgets/framework.dart:5156:5)
#10     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#11     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#12     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11)
#13     Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#14     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2743:19)
#15     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:863:21)
#16     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:381:5)
#17     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1289:15)
#18     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1218:9)
#19     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1076:5)
#20     _invoke (dart:ui/hooks.dart:145:13)
#21     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:338:5)
#22     _drawFrame (dart:ui/hooks.dart:112:31)
(elided 2 frames from class _AssertionError)
====================================================================================================

I think this is caused by _hide() being executed before the asynchronously called _onRefresh() can set the completer.

Not working in web

How do I use this package on web? Is working fine in iOS Mac and Android

Flutter 3.3.1
Dart 2.18
MacOS 12.2
Chrome 105.0.5195.102 (arm64)

Crash when toggling loading state immediately from initState

Hi, thanks for the library!

If I have no await delay in between setting _loading to true and false, the component remains showing the loading spinner indefinitely. If I have a very short delay instead (eg 1-10 ms), the component crashes with:


======== Exception caught by widgets library =======================================================
The following assertion was thrown building _BodyBuilder:
Hide called, but not showing!
'package:declarative_refresh_indicator/declarative_refresh_indicator.dart':
Failed assertion: line 122 pos 12: '_showing'

Example:

@override
  void initState() {
    super.initState();
    _refresh();
  }
 void _refresh() async {
    setState(() => _loading = true);
    //wait anything under ~500ms, or have no `await` at all
    await Future<void>.delayed(Duration(milliseconds: 1));
    if (mounted) setState(() => _loading = false);
  }

This occurs if _refresh is called immediately from initState.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.