Skip to content

Commit ebcba9a

Browse files
committed
Migrate to onStateChanged function
1 parent 2cc8da8 commit ebcba9a

File tree

6 files changed

+59
-50
lines changed

6 files changed

+59
-50
lines changed

example/lib/indicators/check_mark_indicator.dart

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class CheckMarkIndicator extends StatefulWidget {
1717
class _CheckMarkIndicatorState extends State<CheckMarkIndicator>
1818
with SingleTickerProviderStateMixin {
1919
static const _indicatorSize = 150.0;
20-
final _helper = IndicatorStateHelper();
2120

2221
/// Whether to render check mark instead of spinner
2322
bool _renderCompleteState = false;
@@ -31,6 +30,20 @@ class _CheckMarkIndicatorState extends State<CheckMarkIndicator>
3130
onRefresh: () => Future.delayed(const Duration(seconds: 2)),
3231
child: widget.child,
3332
completeStateDuration: const Duration(seconds: 2),
33+
onStateChanged: (change) {
34+
/// set [_renderCompleteState] to true when controller.state become completed
35+
if (change.didChange(to: IndicatorState.complete)) {
36+
setState(() {
37+
_renderCompleteState = true;
38+
});
39+
40+
/// set [_renderCompleteState] to false when controller.state become idle
41+
} else if (change.didChange(to: IndicatorState.idle)) {
42+
setState(() {
43+
_renderCompleteState = false;
44+
});
45+
}
46+
},
3447
builder: (
3548
BuildContext context,
3649
Widget child,
@@ -41,23 +54,13 @@ class _CheckMarkIndicatorState extends State<CheckMarkIndicator>
4154
AnimatedBuilder(
4255
animation: controller,
4356
builder: (BuildContext context, Widget? _) {
44-
_helper.update(controller.state);
45-
4657
if (controller.scrollingDirection == ScrollDirection.reverse &&
4758
prevScrollDirection == ScrollDirection.forward) {
4859
controller.stopDrag();
4960
}
5061

5162
prevScrollDirection = controller.scrollingDirection;
5263

53-
/// set [_renderCompleteState] to true when controller.state become completed
54-
if (_helper.didStateChange(to: IndicatorState.complete)) {
55-
_renderCompleteState = true;
56-
57-
/// set [_renderCompleteState] to false when controller.state become idle
58-
} else if (_helper.didStateChange(to: IndicatorState.idle)) {
59-
_renderCompleteState = false;
60-
}
6164
final containerHeight = controller.value * _indicatorSize;
6265

6366
return Container(

example/lib/indicators/warp_indicator.dart

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ typedef StarColorGetter = Color Function(int index);
1515
class WarpIndicator extends StatefulWidget {
1616
final Widget child;
1717
final int starsCount;
18+
final OnRefresh onRefresh;
19+
final IndicatorController? controller;
1820
final Color skyColor;
1921
final StarColorGetter starColorGetter;
22+
final Key? indicatorKey;
2023

2124
const WarpIndicator({
2225
Key? key,
26+
this.indicatorKey,
27+
this.controller,
28+
required this.onRefresh,
2329
required this.child,
2430
this.starsCount = 30,
2531
this.skyColor = Colors.black,
@@ -37,7 +43,6 @@ class _WarpIndicatorState extends State<WarpIndicator>
3743
with SingleTickerProviderStateMixin {
3844
static const _indicatorSize = 150.0;
3945
final _random = Random();
40-
final _helper = IndicatorStateHelper();
4146
WarpAnimationState _state = WarpAnimationState.stopped;
4247

4348
List<Star> stars = [];
@@ -112,10 +117,19 @@ class _WarpIndicatorState extends State<WarpIndicator>
112117
@override
113118
Widget build(BuildContext context) {
114119
return CustomRefreshIndicator(
120+
key: widget.indicatorKey,
121+
controller: widget.controller,
115122
offsetToArmed: _indicatorSize,
116123
leadingGlowVisible: false,
117124
trailingGlowVisible: false,
118-
onRefresh: () => Future.delayed(const Duration(seconds: 2)),
125+
onRefresh: widget.onRefresh,
126+
onStateChanged: (change) {
127+
if (change.didChange(to: IndicatorState.loading)) {
128+
_startShakeAnimation();
129+
} else if (change.didChange(to: IndicatorState.idle)) {
130+
_stopShakeAnimation();
131+
}
132+
},
119133
child: widget.child,
120134
builder: (
121135
BuildContext context,
@@ -144,18 +158,6 @@ class _WarpIndicatorState extends State<WarpIndicator>
144158
AnimatedBuilder(
145159
animation: animation,
146160
builder: (context, _) {
147-
_helper.update(controller.state);
148-
if (_helper.didStateChange(
149-
to: IndicatorState.loading,
150-
)) {
151-
SchedulerBinding.instance
152-
?.addPostFrameCallback((_) => _startShakeAnimation());
153-
} else if (_helper.didStateChange(
154-
to: IndicatorState.idle,
155-
)) {
156-
SchedulerBinding.instance
157-
?.addPostFrameCallback((_) => _stopShakeAnimation());
158-
}
159161
return Transform.scale(
160162
scale: _scaleTween.transform(controller.value),
161163
child: Builder(builder: (context) {

example/lib/screens/warp_indicator_screen.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class _WarpIndicatorScreenState extends State<WarpIndicatorScreen> {
1717
appBar: const ExampleAppBar(),
1818
body: SafeArea(
1919
child: WarpIndicator(
20+
onRefresh: () => Future.delayed(const Duration(seconds: 2)),
2021
child: const ExampleList(),
2122
),
2223
),

lib/custom_refresh_indicator.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
library custom_refresh_indicator;
22

3+
import 'package:flutter/foundation.dart';
34
import 'package:flutter/material.dart';
45
import 'package:flutter/rendering.dart';
56
import 'package:flutter/widgets.dart';
@@ -8,3 +9,4 @@ part 'src/helpers/positioned_indicator_container.dart';
89
part 'src/custom_refresh_indicator.dart';
910
part 'src/controller.dart';
1011
part 'src/helpers/indicator_state_helper.dart';
12+
part 'src/helpers/indicator_state_change.dart';
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
part of custom_refresh_indicator;
2+
3+
/// Describes [IndicatorState] change.
4+
@immutable
5+
class IndicatorStateChange {
6+
final IndicatorState currentState;
7+
final IndicatorState newState;
8+
9+
const IndicatorStateChange(this.currentState, this.newState);
10+
11+
/// - When [from] and [to] are provided - returns `true` when state did change [from] to [to].
12+
/// - When only [from] is provided - returns `true` when state did change from [from].
13+
/// - When only [to] is provided - returns `true` when state did change to [to].
14+
/// - When [from] and [to] equals `null` - returns `true` for any state change.
15+
bool didChange({IndicatorState? from, IndicatorState? to}) {
16+
final stateChanged = currentState != newState;
17+
if (to == null && from != null) return currentState == from && stateChanged;
18+
if (to != null && from == null) return newState == to && stateChanged;
19+
if (to == null && from == null) return stateChanged;
20+
return currentState == from && newState == to;
21+
}
22+
23+
@override
24+
String toString() =>
25+
"$runtimeType(${describeEnum(currentState)} → ${describeEnum(newState)})";
26+
}

lib/src/helpers/indicator_state_helper.dart

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,28 +59,3 @@ class IndicatorStateHelper {
5959
return _previousState == from && _currentState == to;
6060
}
6161
}
62-
63-
/// Describes [IndicatorState] change.
64-
@immutable
65-
class IndicatorStateChange {
66-
final IndicatorState currentState;
67-
final IndicatorState newState;
68-
69-
const IndicatorStateChange(this.currentState, this.newState);
70-
71-
/// - When [from] and [to] are provided - returns `true` when state did change [from] to [to].
72-
/// - When only [from] is provided - returns `true` when state did change from [from].
73-
/// - When only [to] is provided - returns `true` when state did change to [to].
74-
/// - When [from] and [to] equals `null` - returns `true` for any state change.
75-
bool didChange({IndicatorState? from, IndicatorState? to}) {
76-
final stateChanged = currentState != newState;
77-
if (to == null && from != null) return currentState == from && stateChanged;
78-
if (to != null && from == null) return newState == to && stateChanged;
79-
if (to == null && from == null) return stateChanged;
80-
return currentState == from && newState == to;
81-
}
82-
83-
@override
84-
String toString() =>
85-
"$runtimeType(${describeEnum(currentState)} → ${describeEnum(newState)})";
86-
}

0 commit comments

Comments
 (0)