Skip to content

Commit 10e7a43

Browse files
author
Kamil Klyta
committed
Extract state to controller
1 parent f8d4716 commit 10e7a43

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

lib/src/custom_refresh_indicator.dart

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,12 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
4646
/// The direction in which list scrolls
4747
AxisDirection _axisDirection;
4848
double _dragOffset;
49-
IndicatorState _indicatorState;
5049

5150
AnimationController _animationController;
5251
IndicatorController _customRefreshIndicatorData;
5352

5453
/// Current custom refresh indicator data
55-
IndicatorController get data => _customRefreshIndicatorData;
54+
IndicatorController get controller => _customRefreshIndicatorData;
5655

5756
static const double _kPositionLimit = 1.5;
5857
static const double _kInitialValue = 0.0;
@@ -61,15 +60,13 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
6160
void initState() {
6261
_dragOffset = 0;
6362
_canStart = false;
64-
_indicatorState = IndicatorState.idle;
6563
_axisDirection = AxisDirection.down;
6664
_userScrollingDirection = ScrollDirection.idle;
6765

6866
_customRefreshIndicatorData = IndicatorController(
6967
value: _kInitialValue,
7068
direction: _axisDirection,
7169
scrollingDirection: _userScrollingDirection,
72-
state: _indicatorState,
7370
);
7471

7572
_animationController = AnimationController(
@@ -96,7 +93,6 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
9693
value: _animationController?.value ?? _kInitialValue,
9794
direction: _axisDirection,
9895
scrollingDirection: _userScrollingDirection,
99-
state: _indicatorState,
10096
);
10197
}
10298

@@ -112,25 +108,25 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
112108

113109
bool _handleScrollStartNotification(ScrollStartNotification notification) {
114110
_canStart = notification.metrics.extentBefore == 0 &&
115-
_indicatorState == IndicatorState.idle;
111+
controller.state == IndicatorState.idle;
116112

117-
if (_canStart) _indicatorState = IndicatorState.draging;
113+
if (_canStart) controller._setIndicatorState(IndicatorState.draging);
118114

119115
_axisDirection = notification.metrics.axisDirection;
120116
return false;
121117
}
122118

123119
bool _handleScrollUpdateNotification(ScrollUpdateNotification notification) {
124120
/// hide when list starts to scroll
125-
if (_indicatorState == IndicatorState.draging ||
126-
_indicatorState == IndicatorState.armed) {
121+
if (controller.state == IndicatorState.draging ||
122+
controller.state == IndicatorState.armed) {
127123
if (notification.metrics.extentBefore > 0.0) {
128124
_hide();
129125
} else {
130126
_dragOffset -= notification.scrollDelta;
131127
_calculateDragOffset(notification.metrics.viewportDimension);
132128
}
133-
if (_indicatorState == IndicatorState.armed &&
129+
if (controller.state == IndicatorState.armed &&
134130
notification.dragDetails == null) {
135131
_start();
136132
}
@@ -146,7 +142,7 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
146142

147143
bool _handleScrollEndNotification(ScrollEndNotification notification) {
148144
if (_animationController.value >= CustomRefreshIndicator.armedFromValue) {
149-
if (_indicatorState == IndicatorState.armed) {
145+
if (controller.state == IndicatorState.armed) {
150146
_start();
151147
}
152148
} else {
@@ -161,8 +157,8 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
161157
}
162158

163159
void _calculateDragOffset(double containerExtent) {
164-
if (_indicatorState == IndicatorState.hiding ||
165-
_indicatorState == IndicatorState.loading) return;
160+
if (controller.state == IndicatorState.hiding ||
161+
controller.state == IndicatorState.loading) return;
166162

167163
double newValue;
168164

@@ -176,9 +172,9 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
176172
}
177173

178174
if (newValue >= CustomRefreshIndicator.armedFromValue) {
179-
_indicatorState = IndicatorState.armed;
175+
controller._setIndicatorState(IndicatorState.armed);
180176
} else if (newValue > 0.0) {
181-
_indicatorState = IndicatorState.draging;
177+
controller._setIndicatorState(IndicatorState.draging);
182178
}
183179

184180
/// triggers indicator update
@@ -204,24 +200,23 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
204200
void _start() async {
205201
_dragOffset = 0;
206202

207-
_indicatorState = IndicatorState.loading;
203+
controller._setIndicatorState(IndicatorState.loading);
208204
await _animationController.animateTo(1.0,
209205
duration: widget.armedToLoadingDuration);
210206
await widget.onRefresh();
211207

212208
if (!mounted) return;
213-
_indicatorState = IndicatorState.hiding;
209+
controller._setIndicatorState(IndicatorState.hiding);
214210
await _animationController.animateTo(0.0,
215211
duration: widget.loadingToIdleDuration);
216212

217213
if (!mounted) return;
218214

219-
_indicatorState = IndicatorState.idle;
220-
_updateCustomRefreshIndicatorData();
215+
controller._setIndicatorState(IndicatorState.idle);
221216
}
222217

223218
void _hide() async {
224-
_indicatorState = IndicatorState.hiding;
219+
controller._setIndicatorState(IndicatorState.hiding);
225220
_dragOffset = 0;
226221
_canStart = false;
227222
await _animationController.animateTo(
@@ -232,8 +227,7 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
232227

233228
if (!mounted) return;
234229

235-
_indicatorState = IndicatorState.idle;
236-
_updateCustomRefreshIndicatorData();
230+
controller._setIndicatorState(IndicatorState.idle);
237231
}
238232

239233
static final ChildTransformBuilder noChildTransform =
@@ -249,6 +243,6 @@ class _CustomRefreshIndicatorState extends State<CustomRefreshIndicator>
249243
child: widget.child,
250244
),
251245
),
252-
data,
246+
controller,
253247
);
254248
}

lib/src/definitions.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ class IndicatorController extends ChangeNotifier {
5555

5656
IndicatorController({
5757
@required double value,
58-
@required AxisDirection direction,
58+
AxisDirection direction,
5959
@required ScrollDirection scrollingDirection,
60-
@required IndicatorState state,
61-
}) : _state = state,
60+
IndicatorState state,
61+
}) : _state = state ?? IndicatorState.idle,
6262
_scrollingDirection = scrollingDirection,
63-
_direction = direction,
63+
_direction = direction ?? AxisDirection.down,
6464
_value = value;
6565

6666
@protected
@@ -69,12 +69,10 @@ class IndicatorController extends ChangeNotifier {
6969
@required double value,
7070
@required AxisDirection direction,
7171
@required ScrollDirection scrollingDirection,
72-
@required IndicatorState state,
7372
}) {
7473
_value = value;
7574
_direction = direction;
7675
_scrollingDirection = scrollingDirection;
77-
_state = state;
7876

7977
notifyListeners();
8078
}
@@ -121,6 +119,12 @@ class IndicatorController extends ChangeNotifier {
121119

122120
IndicatorState _state;
123121

122+
/// sets indicator state and notifies listeners
123+
void _setIndicatorState(IndicatorState state) {
124+
_state = state;
125+
notifyListeners();
126+
}
127+
124128
/// Describes current [CustomRefreshIndicator] state
125129
IndicatorState get state => _state;
126130
bool get isArmed => _state == IndicatorState.armed;

0 commit comments

Comments
 (0)