@@ -11,9 +11,9 @@ class PageIndicatorPaged extends BaseScene {
11
11
PageIndicatorPaged ();
12
12
13
13
List <PageDot > _dots;
14
- static const double dotSize = 27 / 2 ;
15
- static const double dotPreW = 37 / 2 ;
16
- static const double dotSelectedW = 69 / 2 ;
14
+ static const double dotSize = 13 ;
15
+ static const double dotPreW = 18 ;
16
+ static const double dotSelectedW = 34 ;
17
17
static const dotUnselectedColor = Color (0xffC4C4C4 );
18
18
static const dotSelectedColor = Colors .red;
19
19
static const dotPreColor = Colors .blue;
@@ -25,14 +25,20 @@ class PageIndicatorPaged extends BaseScene {
25
25
final int _visibleItems = 5 ;
26
26
final int _numItems = 10 ;
27
27
int _scroll = 0 ;
28
+ double _allDotsWidth = 0 ;
29
+
28
30
double dotSep = 3 ;
29
31
30
- double allDotsWidth = 0 ;
32
+ /// Easing for euler integration.
33
+ /// The bigger number, the slower transitions.
34
+ double scrollEase = 10 ;
35
+ double dotSizeEase = 10 ;
36
+ double dotPositionEase = 3 ;
31
37
32
38
@override
33
39
void addedToStage () {
34
40
super .addedToStage ();
35
- stage.color = Color (0xffD1D1D );
41
+ // stage.color = Color(0xffD1D1D);
36
42
37
43
container = GSprite ();
38
44
scrollContainer = GSprite ();
@@ -47,8 +53,8 @@ class PageIndicatorPaged extends BaseScene {
47
53
});
48
54
49
55
var itemWS = dotSize + dotSep;
50
- allDotsWidth =
51
- itemWS * (_visibleItems - 2 ) + (dotPreW + dotSep) * 2 + dotSelectedW;
56
+ _allDotsWidth =
57
+ itemWS * (_visibleItems - 3 ) + (dotPreW + dotSep) * 2 + dotSelectedW;
52
58
53
59
/// debug size.
54
60
// container.graphics
@@ -58,15 +64,15 @@ class PageIndicatorPaged extends BaseScene {
58
64
// .endFill();
59
65
60
66
/// -- masking.
61
-
62
67
// var maskOff = 3.0;
63
68
// container.maskRect = GRect(
64
69
// -maskOff,
65
70
// -maskOff,
66
- // allDotsWidth + maskOff * 2,
71
+ // _allDotsWidth + maskOff * 2,
67
72
// dotSize + maskOff * 2,
68
73
// );
69
74
// container.maskRect.corners.allTo(dotSize);
75
+
70
76
/// -- end masking.
71
77
72
78
/// only for testing.
@@ -80,7 +86,7 @@ class PageIndicatorPaged extends BaseScene {
80
86
});
81
87
82
88
stage.onResized.add (() {
83
- var ratioScale = sw / allDotsWidth ;
89
+ var ratioScale = sw / _allDotsWidth ;
84
90
scale = ratioScale;
85
91
86
92
/// center it (not using alignPivot())
@@ -104,8 +110,7 @@ class PageIndicatorPaged extends BaseScene {
104
110
void update (double delta) {
105
111
super .update (delta);
106
112
_updateScroll ();
107
- _resizeItems ();
108
- _positionItems ();
113
+ _layoutDots ();
109
114
}
110
115
111
116
set currentIndex (int value) {
@@ -114,8 +119,9 @@ class PageIndicatorPaged extends BaseScene {
114
119
_currentIndex = value.clamp (0 , _numItems - 1 );
115
120
116
121
/// offset scroll.
117
- if (_currentIndex > _visibleItems - 1 ) {
118
- var offset = _visibleItems - 1 - _currentIndex;
122
+ /// -2 = take off mid size items.
123
+ if (_currentIndex > _visibleItems - 2 ) {
124
+ var offset = _visibleItems - 2 - _currentIndex;
119
125
_scroll = offset;
120
126
if (_currentIndex == _numItems - 1 ) {
121
127
_scroll += 1 ;
@@ -127,48 +133,52 @@ class PageIndicatorPaged extends BaseScene {
127
133
}
128
134
var firstIndex = - _scroll;
129
135
_dots.forEach ((dot) {
130
- double targetAlpha = 1.0 ;
131
- if (dot.id < firstIndex || dot.id > firstIndex + _visibleItems) {
132
- targetAlpha = 0.3 ;
136
+ var targetAlpha = 1.0 ;
137
+ if (dot.id < firstIndex || dot.id > firstIndex + ( _visibleItems - 1 ) ) {
138
+ targetAlpha = 0 ;
133
139
}
134
140
dot.tween (
135
141
duration: .3 ,
136
- colorize: dotUnselectedColor,
137
142
alpha: targetAlpha,
143
+ /// for a zoom in effect
144
+ // scale: targetAlpha == 1 ? 1 : .25,
145
+ // y: targetAlpha == 1 ? 0.0 : dotSize / 2,
138
146
);
147
+ dot.targetColor = dotUnselectedColor;
139
148
dot.targetSize = dotSize;
140
149
});
141
150
142
151
prevDot? .targetSize = dotPreW;
143
152
nextDot? .targetSize = dotPreW;
144
153
145
- nextDot? .tween (duration: .3 , colorize: dotPreColor, overwrite: 0 );
146
- prevDot? .tween (duration: .3 , colorize: dotPreColor, overwrite: 0 );
147
- // nextDot?.color = dotPreColor;
148
- // prevDot?.color = dotPreColor;
149
-
154
+ /// color is computed internally in the dot to avoid
155
+ /// the expensize "colorize".
156
+ nextDot? .targetColor = dotPreColor;
157
+ prevDot? .targetColor = dotPreColor;
150
158
currDot? .targetSize = dotSelectedW;
151
- currDot? .tween (duration: .3 , colorize: dotSelectedColor, overwrite: 0 );
152
- // _positionItems();
159
+ currDot? .targetColor = dotSelectedColor;
153
160
}
154
161
155
- void _positionItems () {
162
+ void _layoutDots () {
156
163
var lastX = 0.0 ;
157
164
for (var i = 0 ; i < _dots.length; ++ i) {
158
165
var dot = _dots[i];
159
- // dot.x = lastX;
160
- dot.x += (lastX - dot.x) / 4 ;
166
+ var sizeDistance = dot.targetSize - dot.size;
167
+ if (sizeDistance.abs () < .1 ) {
168
+ dot.size = dot.targetSize;
169
+ } else {
170
+ dot.size += sizeDistance / dotSizeEase;
171
+ }
172
+ var posDistance = lastX - dot.x;
173
+ if (posDistance.abs () < .1 ) {
174
+ dot.x = lastX;
175
+ } else {
176
+ dot.x += posDistance / dotPositionEase;
177
+ }
161
178
lastX += dot.size + dotSep;
162
179
}
163
180
}
164
181
165
- void _resizeItems () {
166
- for (var i = 0 ; i < _dots.length; ++ i) {
167
- var dot = _dots[i];
168
- dot.size += (dot.targetSize - dot.size) / 10 ;
169
- }
170
- }
171
-
172
182
PageDot get currDot {
173
183
return _dots[_currentIndex];
174
184
}
@@ -185,10 +195,10 @@ class PageIndicatorPaged extends BaseScene {
185
195
186
196
void _updateScroll () {
187
197
var distance = _targetScroll - scrollContainer.x;
188
- if (distance.abs () < .5 ) {
198
+ if (distance.abs () < .1 ) {
189
199
scrollContainer.x = _targetScroll;
190
200
return ;
191
201
}
192
- scrollContainer.x += distance / 12 ;
202
+ scrollContainer.x += distance / scrollEase ;
193
203
}
194
204
}
0 commit comments