Skip to content

Commit eb202c8

Browse files
Kamil Klytagonuit
authored andcommitted
Add indicator stopping
1 parent 7bd7873 commit eb202c8

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

example/lib/indicators/check_mark_indicator.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:custom_refresh_indicator/custom_refresh_indicator.dart';
22
import 'package:flutter/material.dart';
3+
import 'package:flutter/rendering.dart';
34
import 'package:flutter/widgets.dart';
45

56
class CheckMarkIndicator extends StatefulWidget {
@@ -22,6 +23,9 @@ class _CheckMarkIndicatorState extends State<CheckMarkIndicator>
2223
/// Whether to render check mark instead of spinner
2324
bool _renderCompleteState = false;
2425

26+
27+
ScrollDirection prevScrollDirection = ScrollDirection.idle;
28+
2529
@override
2630
Widget build(BuildContext context) {
2731
return CustomRefreshIndicator(
@@ -41,6 +45,13 @@ class _CheckMarkIndicatorState extends State<CheckMarkIndicator>
4145
builder: (BuildContext context, Widget? _) {
4246
_helper.update(controller.state);
4347

48+
if (controller.scrollingDirection == ScrollDirection.reverse &&
49+
prevScrollDirection == ScrollDirection.forward) {
50+
controller.stopDrag();
51+
}
52+
53+
prevScrollDirection = controller.scrollingDirection;
54+
4455
/// set [_renderCompleteState] to true when controller.state become completed
4556
if (_helper.didStateChange(to: IndicatorState.complete)) {
4657
_renderCompleteState = true;

lib/src/controller.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class IndicatorController extends ChangeNotifier {
7575
_scrollingDirection = scrollingDirection ?? ScrollDirection.idle,
7676
_direction = direction ?? AxisDirection.down,
7777
_value = value ?? 0.0,
78-
_refreshEnabled = refreshEnabled ?? true;
78+
_refreshEnabled = refreshEnabled ?? true,
79+
_shouldStopDrag = false;
7980

8081
@protected
8182
@visibleForTesting
@@ -159,10 +160,22 @@ class IndicatorController extends ChangeNotifier {
159160
bool get isIdle => _currentState == IndicatorState.idle;
160161

161162
bool _refreshEnabled;
163+
bool _shouldStopDrag;
162164

163165
/// Whether custom refresh indicator can change [IndicatorState] from `idle` to `dragging`
164166
bool get isRefreshEnabled => _refreshEnabled;
165167

168+
void stopDrag() {
169+
if (isDragging || isArmed) {
170+
_shouldStopDrag = true;
171+
} else {
172+
throw StateError(
173+
"stopDrag method can be used only during "
174+
"drag or armed indicator state.",
175+
);
176+
}
177+
}
178+
166179
/// Disables list pull to refresh
167180
void disableRefresh() {
168181
_refreshEnabled = false;

lib/src/custom_refresh_indicator.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
118118
__canStart = canStart;
119119
}
120120

121+
/// Indicating that indicator is currently stopping drag.
122+
/// When true, user is not able to performe any action.
123+
bool _isStopingDrag = false;
124+
121125
late double _dragOffset;
122126

123127
late AnimationController _animationController;
@@ -255,6 +259,19 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
255259
/// will not be handled by this widget
256260
if (!widget.notificationPredicate(notification)) return false;
257261

262+
if (_isStopingDrag) {
263+
controller._shouldStopDrag = false;
264+
return false;
265+
} else if (controller._shouldStopDrag) {
266+
controller._shouldStopDrag = false;
267+
_isStopingDrag = true;
268+
269+
_hide().whenComplete(() {
270+
_isStopingDrag = false;
271+
});
272+
return false;
273+
}
274+
258275
if (notification is ScrollStartNotification)
259276
return _handleScrollStartNotification(notification);
260277
if (!_canStart) return false;
@@ -297,7 +314,7 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
297314
controller.setIndicatorState(IndicatorState.idle);
298315
}
299316

300-
void _hide() async {
317+
Future<void> _hide() async {
301318
controller.setIndicatorState(IndicatorState.hiding);
302319
_dragOffset = 0;
303320
_canStart = false;

0 commit comments

Comments
 (0)