Skip to content

Commit 096ce7f

Browse files
author
Kamil Klyta
committed
Add support for refresh disable
1 parent 5516d06 commit 096ce7f

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

lib/src/custom_refresh_indicator.dart

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ class CustomRefreshIndicator extends StatefulWidget {
1616
final Widget child;
1717
final ChildTransformBuilder builder;
1818
final RefreshCallback onRefresh;
19+
final IndicatorController controller;
1920

2021
CustomRefreshIndicator({
2122
@required this.child,
2223
@required this.onRefresh,
2324
@required this.builder,
25+
this.controller,
2426
this.offsetToArmed,
2527
this.extentPercentageToArmed = defaultExtentPercentageToArmed,
2628
this.dragingToIdleDuration = const Duration(milliseconds: 300),
@@ -37,16 +39,22 @@ class CustomRefreshIndicator extends StatefulWidget {
3739

3840
class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
3941
with TickerProviderStateMixin {
42+
bool __canStart = false;
43+
4044
/// Whether custom refresh indicator can change [IndicatorState] from `idle` to `draging`
41-
bool _canStart = false;
45+
bool get _canStart =>
46+
__canStart && _customRefreshIndicatorController._refreshEnabled;
47+
set _canStart(bool canStart) {
48+
__canStart = canStart;
49+
}
4250

4351
double _dragOffset;
4452

4553
AnimationController _animationController;
46-
IndicatorController _customRefreshIndicatorData;
54+
IndicatorController _customRefreshIndicatorController;
4755

48-
/// Current custom refresh indicator data
49-
IndicatorController get controller => _customRefreshIndicatorData;
56+
/// Keeps current custom refresh indicator data
57+
IndicatorController get controller => _customRefreshIndicatorController;
5058

5159
static const double _kPositionLimit = 1.5;
5260
static const double _kInitialValue = 0.0;
@@ -56,32 +64,30 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
5664
_dragOffset = 0;
5765
_canStart = false;
5866

59-
_customRefreshIndicatorData = IndicatorController(
60-
value: _kInitialValue,
61-
);
67+
_customRefreshIndicatorController =
68+
widget.controller ?? IndicatorController._();
6269

6370
_animationController = AnimationController(
6471
vsync: this,
6572
upperBound: _kPositionLimit,
66-
lowerBound: 0.0,
67-
)
68-
..addListener(_updateCustomRefreshIndicatorData)
69-
..value = _kInitialValue;
73+
lowerBound: _kInitialValue,
74+
value: _kInitialValue,
75+
)..addListener(_updateCustomRefreshIndicatorValue);
7076

7177
super.initState();
7278
}
7379

7480
@override
7581
void dispose() {
7682
_animationController.dispose();
77-
_customRefreshIndicatorData.dispose();
83+
_customRefreshIndicatorController.dispose();
7884
super.dispose();
7985
}
8086

8187
/// Notifies the listeners of the controller
82-
void _updateCustomRefreshIndicatorData() {
83-
_customRefreshIndicatorData.updateAndNotify(
84-
value: _animationController?.value ?? _kInitialValue,
88+
void _updateCustomRefreshIndicatorValue() {
89+
_customRefreshIndicatorController._setValue(
90+
_animationController?.value ?? _kInitialValue,
8591
);
8692
}
8793

@@ -219,9 +225,6 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
219225
controller._setIndicatorState(IndicatorState.idle);
220226
}
221227

222-
static final ChildTransformBuilder noChildTransform =
223-
(context, child, data) => child;
224-
225228
@override
226229
Widget build(BuildContext context) => widget.builder.call(
227230
context,

lib/src/definitions.dart

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,27 @@ class IndicatorController extends ChangeNotifier {
5353
/// Current indicator value / progress
5454
double get value => _value;
5555

56-
IndicatorController({
57-
@required double value,
56+
/// Creates [CustomRefreshIndicator] controller class
57+
factory IndicatorController({
58+
bool refreshEnabled,
59+
}) =>
60+
IndicatorController._(refreshEnabled: refreshEnabled);
61+
62+
IndicatorController._({
63+
double value,
5864
AxisDirection direction,
5965
ScrollDirection scrollingDirection,
6066
IndicatorState state,
67+
bool refreshEnabled,
6168
}) : _state = state ?? IndicatorState.idle,
6269
_scrollingDirection = scrollingDirection ?? ScrollDirection.idle,
6370
_direction = direction ?? AxisDirection.down,
64-
_value = value;
71+
_value = value ?? 0.0,
72+
_refreshEnabled = refreshEnabled ?? true;
6573

6674
@protected
67-
@visibleForTesting
68-
void updateAndNotify({
69-
@required double value,
70-
}) {
75+
void _setValue(double value) {
7176
_value = value;
72-
7377
notifyListeners();
7478
}
7579

@@ -138,4 +142,21 @@ class IndicatorController extends ChangeNotifier {
138142
bool get isLoading => _state == IndicatorState.loading;
139143
bool get isHiding => _state == IndicatorState.hiding;
140144
bool get isIdle => _state == IndicatorState.idle;
145+
146+
bool _refreshEnabled;
147+
148+
/// Whether custom refresh indicator can change [IndicatorState] from `idle` to `draging`
149+
bool get refreshEnabled => _refreshEnabled;
150+
151+
/// Disables list pull to refresh
152+
void disableRefresh() {
153+
_refreshEnabled = false;
154+
notifyListeners();
155+
}
156+
157+
/// Enables list pull to refresh
158+
void enableRefresh() {
159+
_refreshEnabled = true;
160+
notifyListeners();
161+
}
141162
}

0 commit comments

Comments
 (0)