@@ -11,9 +11,9 @@ class PageIndicatorPaged extends BaseScene {
1111 PageIndicatorPaged ();
1212
1313 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 ;
1717 static const dotUnselectedColor = Color (0xffC4C4C4 );
1818 static const dotSelectedColor = Colors .red;
1919 static const dotPreColor = Colors .blue;
@@ -25,14 +25,20 @@ class PageIndicatorPaged extends BaseScene {
2525 final int _visibleItems = 5 ;
2626 final int _numItems = 10 ;
2727 int _scroll = 0 ;
28+ double _allDotsWidth = 0 ;
29+
2830 double dotSep = 3 ;
2931
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 ;
3137
3238 @override
3339 void addedToStage () {
3440 super .addedToStage ();
35- stage.color = Color (0xffD1D1D );
41+ // stage.color = Color(0xffD1D1D);
3642
3743 container = GSprite ();
3844 scrollContainer = GSprite ();
@@ -47,8 +53,8 @@ class PageIndicatorPaged extends BaseScene {
4753 });
4854
4955 var itemWS = dotSize + dotSep;
50- allDotsWidth =
51- itemWS * (_visibleItems - 2 ) + (dotPreW + dotSep) * 2 + dotSelectedW;
56+ _allDotsWidth =
57+ itemWS * (_visibleItems - 3 ) + (dotPreW + dotSep) * 2 + dotSelectedW;
5258
5359 /// debug size.
5460 // container.graphics
@@ -58,15 +64,15 @@ class PageIndicatorPaged extends BaseScene {
5864 // .endFill();
5965
6066 /// -- masking.
61-
6267 // var maskOff = 3.0;
6368 // container.maskRect = GRect(
6469 // -maskOff,
6570 // -maskOff,
66- // allDotsWidth + maskOff * 2,
71+ // _allDotsWidth + maskOff * 2,
6772 // dotSize + maskOff * 2,
6873 // );
6974 // container.maskRect.corners.allTo(dotSize);
75+
7076 /// -- end masking.
7177
7278 /// only for testing.
@@ -80,7 +86,7 @@ class PageIndicatorPaged extends BaseScene {
8086 });
8187
8288 stage.onResized.add (() {
83- var ratioScale = sw / allDotsWidth ;
89+ var ratioScale = sw / _allDotsWidth ;
8490 scale = ratioScale;
8591
8692 /// center it (not using alignPivot())
@@ -104,8 +110,7 @@ class PageIndicatorPaged extends BaseScene {
104110 void update (double delta) {
105111 super .update (delta);
106112 _updateScroll ();
107- _resizeItems ();
108- _positionItems ();
113+ _layoutDots ();
109114 }
110115
111116 set currentIndex (int value) {
@@ -114,8 +119,9 @@ class PageIndicatorPaged extends BaseScene {
114119 _currentIndex = value.clamp (0 , _numItems - 1 );
115120
116121 /// 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;
119125 _scroll = offset;
120126 if (_currentIndex == _numItems - 1 ) {
121127 _scroll += 1 ;
@@ -127,48 +133,52 @@ class PageIndicatorPaged extends BaseScene {
127133 }
128134 var firstIndex = - _scroll;
129135 _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 ;
133139 }
134140 dot.tween (
135141 duration: .3 ,
136- colorize: dotUnselectedColor,
137142 alpha: targetAlpha,
143+ /// for a zoom in effect
144+ // scale: targetAlpha == 1 ? 1 : .25,
145+ // y: targetAlpha == 1 ? 0.0 : dotSize / 2,
138146 );
147+ dot.targetColor = dotUnselectedColor;
139148 dot.targetSize = dotSize;
140149 });
141150
142151 prevDot? .targetSize = dotPreW;
143152 nextDot? .targetSize = dotPreW;
144153
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;
150158 currDot? .targetSize = dotSelectedW;
151- currDot? .tween (duration: .3 , colorize: dotSelectedColor, overwrite: 0 );
152- // _positionItems();
159+ currDot? .targetColor = dotSelectedColor;
153160 }
154161
155- void _positionItems () {
162+ void _layoutDots () {
156163 var lastX = 0.0 ;
157164 for (var i = 0 ; i < _dots.length; ++ i) {
158165 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+ }
161178 lastX += dot.size + dotSep;
162179 }
163180 }
164181
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-
172182 PageDot get currDot {
173183 return _dots[_currentIndex];
174184 }
@@ -185,10 +195,10 @@ class PageIndicatorPaged extends BaseScene {
185195
186196 void _updateScroll () {
187197 var distance = _targetScroll - scrollContainer.x;
188- if (distance.abs () < .5 ) {
198+ if (distance.abs () < .1 ) {
189199 scrollContainer.x = _targetScroll;
190200 return ;
191201 }
192- scrollContainer.x += distance / 12 ;
202+ scrollContainer.x += distance / scrollEase ;
193203 }
194204}
0 commit comments