From c9c87b6903c637b16ed67969da9f6927275a31a9 Mon Sep 17 00:00:00 2001 From: xuelongqy Date: Thu, 14 Jul 2022 18:01:39 +0800 Subject: [PATCH] feat: Indicator add [triggerWhenReach] [#348](https://github.com/xuelongqy/flutter_easy_refresh/pull/348). Trigger immediately when reaching the [triggerOffset]. --- CHANGELOG.md | 4 ++- example/lib/l10n/translations/en.dart | 1 + example/lib/l10n/translations/zh_cn.dart | 1 + example/lib/page/style/classical_page.dart | 14 ++++++++++ example/pubspec.yaml | 2 +- lib/src/indicator/footer/footer.dart | 9 ++++++ lib/src/indicator/header/header.dart | 9 ++++++ lib/src/indicator/indicator.dart | 4 +++ lib/src/notifier/indicator_notifier.dart | 28 +++++++++++++------ .../styles/classic/footer/classic_footer.dart | 2 ++ .../styles/classic/header/classic_header.dart | 2 ++ pubspec.yaml | 2 +- 12 files changed, 66 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 227e4aff..ce4727a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,6 @@ -# Change log +## V 3.0.2 +> feat: Indicator add [triggerWhenReach] [#348](https://github.com/xuelongqy/flutter_easy_refresh/pull/348). Trigger immediately when reaching the [triggerOffset]. +> feat: CupertinoIndicator support horizontal. ## V 3.0.1+1 > fix: CupertinoActivityIndicator radius == 0. diff --git a/example/lib/l10n/translations/en.dart b/example/lib/l10n/translations/en.dart index d72858e5..a064ba19 100644 --- a/example/lib/l10n/translations/en.dart +++ b/example/lib/l10n/translations/en.dart @@ -86,4 +86,5 @@ const en = { 'Tron chain, TRX, USDT, USDC and other tokens', 'Dogecoin donation': 'Dogecoin donation', '%s copied!': '%s copied!', + 'Trigger immediately': 'Trigger immediately', }; diff --git a/example/lib/l10n/translations/zh_cn.dart b/example/lib/l10n/translations/zh_cn.dart index 920da6e0..57ff35cb 100644 --- a/example/lib/l10n/translations/zh_cn.dart +++ b/example/lib/l10n/translations/zh_cn.dart @@ -84,4 +84,5 @@ const zhCN = { 'Tron chain, TRX, USDT, USDC and other tokens': '波场链,TRX、USDT、USDC及其他代币', 'Dogecoin donation': '狗狗币捐赠', '%s copied!': '%s 已复制!', + 'Trigger immediately': '立即触发', }; diff --git a/example/lib/page/style/classical_page.dart b/example/lib/page/style/classical_page.dart index 3547d45f..ed4837e5 100644 --- a/example/lib/page/style/classical_page.dart +++ b/example/lib/page/style/classical_page.dart @@ -62,6 +62,7 @@ class _ClassicPageState extends State { showMessage: _headerProperties.message, showText: _headerProperties.text, infiniteOffset: _headerProperties.infinite ? 70 : null, + triggerWhenReach: _headerProperties.immediately, dragText: 'Pull to refresh'.tr, armedText: 'Release ready'.tr, readyText: 'Refreshing...'.tr, @@ -80,6 +81,7 @@ class _ClassicPageState extends State { showMessage: _footerProperties.message, showText: _footerProperties.text, infiniteOffset: _footerProperties.infinite ? 70 : null, + triggerWhenReach: _footerProperties.immediately, dragText: 'Pull to load'.tr, armedText: 'Release ready'.tr, readyText: 'Loading...'.tr, @@ -290,6 +292,17 @@ class _ClassicPageState extends State { ), ), ), + ListTile( + title: Text('Trigger immediately'.tr), + trailing: Switch( + value: properties.immediately, + onChanged: (value) { + setState(() { + properties.immediately = value; + }); + }, + ), + ), ListTile( title: Text('Infinite'.tr), trailing: Switch( @@ -351,6 +364,7 @@ class _CIProperties { bool message = true; bool text = true; bool infinite; + bool immediately = false; _CIProperties({ required this.name, diff --git a/example/pubspec.yaml b/example/pubspec.yaml index ce1526fc..8839afa9 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -7,7 +7,7 @@ description: EasyRefresh example. # Both the version and the builder number may be overridden in flutter # build by specifying --build-name and --build-number, respectively. # Read more about versioning at semver.org. -version: 3.0.0+45 +version: 3.0.2+47 environment: sdk: ">=2.13.0 <3.0.0" diff --git a/lib/src/indicator/footer/footer.dart b/lib/src/indicator/footer/footer.dart index 74e09be1..b6069e42 100644 --- a/lib/src/indicator/footer/footer.dart +++ b/lib/src/indicator/footer/footer.dart @@ -25,6 +25,7 @@ abstract class Footer extends Indicator { double secondaryCloseTriggerOffset = kDefaultSecondaryCloseTriggerOffset, bool notifyWhenInvisible = false, IndicatorStateListenable? listenable, + bool triggerWhenReach = false, }) : super( triggerOffset: triggerOffset, clamping: clamping, @@ -48,6 +49,7 @@ abstract class Footer extends Indicator { secondaryCloseTriggerOffset: secondaryCloseTriggerOffset, notifyWhenInvisible: notifyWhenInvisible, listenable: listenable, + triggerWhenReach: triggerWhenReach, ); } @@ -80,6 +82,7 @@ class BuilderFooter extends Footer { double secondaryCloseTriggerOffset = kDefaultSecondaryCloseTriggerOffset, bool notifyWhenInvisible = false, IndicatorStateListenable? listenable, + bool triggerWhenReach = false, }) : super( triggerOffset: triggerOffset, clamping: clamping, @@ -103,6 +106,7 @@ class BuilderFooter extends Footer { secondaryCloseTriggerOffset: secondaryCloseTriggerOffset, notifyWhenInvisible: notifyWhenInvisible, listenable: listenable, + triggerWhenReach: triggerWhenReach, ); @override @@ -136,6 +140,7 @@ class ListenerFooter extends Footer { double? secondaryDimension, double secondaryCloseTriggerOffset = kDefaultSecondaryCloseTriggerOffset, bool notifyWhenInvisible = false, + bool triggerWhenReach = false, }) : super( triggerOffset: triggerOffset, clamping: clamping, @@ -159,6 +164,7 @@ class ListenerFooter extends Footer { secondaryCloseTriggerOffset: secondaryCloseTriggerOffset, notifyWhenInvisible: notifyWhenInvisible, listenable: listenable, + triggerWhenReach: triggerWhenReach, ); @override @@ -203,6 +209,7 @@ abstract class SecondaryFooter extends Footer { secondaryCloseTriggerOffset: secondaryCloseTriggerOffset, notifyWhenInvisible: footer.notifyWhenInvisible, listenable: listenable ?? footer.listenable, + triggerWhenReach: footer.triggerWhenReach, ); @override @@ -300,6 +307,7 @@ class OverrideFooter extends Footer { double? secondaryCloseTriggerOffset, bool? notifyWhenInvisible, IndicatorStateListenable? listenable, + bool triggerWhenReach = false, }) : super( triggerOffset: triggerOffset ?? footer.triggerOffset, clamping: clamping ?? footer.clamping, @@ -328,6 +336,7 @@ class OverrideFooter extends Footer { notifyWhenInvisible: notifyWhenInvisible ?? footer.notifyWhenInvisible, listenable: listenable ?? footer.listenable, + triggerWhenReach: triggerWhenReach, ); @override diff --git a/lib/src/indicator/header/header.dart b/lib/src/indicator/header/header.dart index 4b2f2806..ddc8d648 100644 --- a/lib/src/indicator/header/header.dart +++ b/lib/src/indicator/header/header.dart @@ -25,6 +25,7 @@ abstract class Header extends Indicator { double secondaryCloseTriggerOffset = kDefaultSecondaryCloseTriggerOffset, bool notifyWhenInvisible = false, IndicatorStateListenable? listenable, + bool triggerWhenReach = false, }) : super( triggerOffset: triggerOffset, clamping: clamping, @@ -48,6 +49,7 @@ abstract class Header extends Indicator { secondaryCloseTriggerOffset: secondaryCloseTriggerOffset, notifyWhenInvisible: notifyWhenInvisible, listenable: listenable, + triggerWhenReach: triggerWhenReach, ); } @@ -80,6 +82,7 @@ class BuilderHeader extends Header { double secondaryCloseTriggerOffset = kDefaultSecondaryCloseTriggerOffset, bool notifyWhenInvisible = false, IndicatorStateListenable? listenable, + bool triggerWhenReach = false, }) : super( triggerOffset: triggerOffset, clamping: clamping, @@ -103,6 +106,7 @@ class BuilderHeader extends Header { secondaryCloseTriggerOffset: secondaryCloseTriggerOffset, notifyWhenInvisible: notifyWhenInvisible, listenable: listenable, + triggerWhenReach: triggerWhenReach, ); @override @@ -136,6 +140,7 @@ class ListenerHeader extends Header { double? secondaryDimension, double secondaryCloseTriggerOffset = kDefaultSecondaryCloseTriggerOffset, bool notifyWhenInvisible = false, + bool triggerWhenReach = false, }) : super( triggerOffset: triggerOffset, clamping: clamping, @@ -159,6 +164,7 @@ class ListenerHeader extends Header { secondaryCloseTriggerOffset: secondaryCloseTriggerOffset, notifyWhenInvisible: notifyWhenInvisible, listenable: listenable, + triggerWhenReach: triggerWhenReach, ); @override @@ -203,6 +209,7 @@ abstract class SecondaryHeader extends Header { secondaryCloseTriggerOffset: secondaryCloseTriggerOffset, notifyWhenInvisible: header.notifyWhenInvisible, listenable: listenable ?? header.listenable, + triggerWhenReach: header.triggerWhenReach, ); @override @@ -300,6 +307,7 @@ class OverrideHeader extends Header { double? secondaryCloseTriggerOffset, bool? notifyWhenInvisible, IndicatorStateListenable? listenable, + bool triggerWhenReach = false, }) : super( triggerOffset: triggerOffset ?? header.triggerOffset, clamping: clamping ?? header.clamping, @@ -328,6 +336,7 @@ class OverrideHeader extends Header { notifyWhenInvisible: notifyWhenInvisible ?? header.notifyWhenInvisible, listenable: listenable ?? header.listenable, + triggerWhenReach: triggerWhenReach, ); @override diff --git a/lib/src/indicator/indicator.dart b/lib/src/indicator/indicator.dart index bcff4fa0..4b07cd3e 100644 --- a/lib/src/indicator/indicator.dart +++ b/lib/src/indicator/indicator.dart @@ -339,6 +339,9 @@ abstract class Indicator { /// Monitor state changes in real time. final IndicatorStateListenable? listenable; + /// Trigger immediately when reaching the [triggerOffset]. + final bool triggerWhenReach; + const Indicator({ required this.triggerOffset, required this.clamping, @@ -362,6 +365,7 @@ abstract class Indicator { this.secondaryCloseTriggerOffset = kDefaultSecondaryCloseTriggerOffset, this.notifyWhenInvisible = false, this.listenable, + this.triggerWhenReach = false, }) : hitOver = hitOver ?? infiniteOffset != null, infiniteHitOver = infiniteHitOver ?? infiniteOffset == null, assert(infiniteOffset == null || infiniteOffset >= 0, diff --git a/lib/src/notifier/indicator_notifier.dart b/lib/src/notifier/indicator_notifier.dart index b2849b8b..9aabeb2b 100644 --- a/lib/src/notifier/indicator_notifier.dart +++ b/lib/src/notifier/indicator_notifier.dart @@ -553,11 +553,17 @@ abstract class IndicatorNotifier extends ChangeNotifier { } } else if (_offset == actualTriggerOffset) { // Must be exceeded to trigger the task - _mode = userOffsetNotifier.value - ? (_releaseOffset > actualTriggerOffset + if (userOffsetNotifier.value) { + if (_indicator.triggerWhenReach) { + _mode = IndicatorMode.processing; + } else { + _mode = (_releaseOffset > actualTriggerOffset ? IndicatorMode.ready - : IndicatorMode.armed) - : IndicatorMode.processing; + : IndicatorMode.armed); + } + } else { + _mode = IndicatorMode.processing; + } } else if (_offset > actualTriggerOffset) { if (hasSecondary && _offset >= actualSecondaryTriggerOffset && @@ -577,11 +583,15 @@ abstract class IndicatorNotifier extends ChangeNotifier { // Process // If the user is scrolling // (the task is not executed if it is not released) - _mode = userOffsetNotifier.value - ? IndicatorMode.armed - : (_releaseOffset > actualTriggerOffset - ? IndicatorMode.ready - : IndicatorMode.armed); + if (userOffsetNotifier.value) { + _mode = _indicator.triggerWhenReach + ? IndicatorMode.processing + : IndicatorMode.armed; + } else { + _mode = (_releaseOffset > actualTriggerOffset + ? IndicatorMode.ready + : IndicatorMode.armed); + } } } // Execute the task. diff --git a/lib/src/styles/classic/footer/classic_footer.dart b/lib/src/styles/classic/footer/classic_footer.dart index 4ec1593c..a263b726 100644 --- a/lib/src/styles/classic/footer/classic_footer.dart +++ b/lib/src/styles/classic/footer/classic_footer.dart @@ -105,6 +105,7 @@ class ClassicFooter extends Footer { bool? hitOver, bool? infiniteHitOver, bool hapticFeedback = false, + bool triggerWhenReach = false, this.mainAxisAlignment = MainAxisAlignment.start, this.backgroundColor, this.dragText, @@ -146,6 +147,7 @@ class ClassicFooter extends Footer { infiniteHitOver: infiniteHitOver, position: position, hapticFeedback: hapticFeedback, + triggerWhenReach: triggerWhenReach, ); @override diff --git a/lib/src/styles/classic/header/classic_header.dart b/lib/src/styles/classic/header/classic_header.dart index 43a31930..b6a04893 100644 --- a/lib/src/styles/classic/header/classic_header.dart +++ b/lib/src/styles/classic/header/classic_header.dart @@ -105,6 +105,7 @@ class ClassicHeader extends Header { bool? hitOver, bool? infiniteHitOver, bool hapticFeedback = false, + bool triggerWhenReach = false, this.mainAxisAlignment = MainAxisAlignment.center, this.backgroundColor, this.dragText, @@ -146,6 +147,7 @@ class ClassicHeader extends Header { infiniteHitOver: infiniteHitOver, position: position, hapticFeedback: hapticFeedback, + triggerWhenReach: triggerWhenReach, ); @override diff --git a/pubspec.yaml b/pubspec.yaml index 7f16e17c..1c0f514d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: easy_refresh description: A flutter widget that provides pull-down refresh and pull-up load. -version: 3.0.1+1 +version: 3.0.2 homepage: https://xuelongqy.github.io/flutter_easy_refresh repository: https://github.com/xuelongqy/flutter_easy_refresh issue_tracker: https://github.com/xuelongqy/flutter_easy_refresh/issues