diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100755 index 0000000..a366c58 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## [1.0.1] + +- change controller params to private diff --git a/README.md b/README.md index 699baf6..814b1d1 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ flutter run ### Add dependencies ```yaml dependencies: - infinite_cards: ^1.0.0 + infinite_cards: ^1.0.1 ``` ### Build controller in initState diff --git a/README_cn.md b/README_cn.md index 538a809..db89538 100644 --- a/README_cn.md +++ b/README_cn.md @@ -23,7 +23,7 @@ flutter run ### 添加依赖 ```yaml dependencies: - infinite_cards: ^1.0.0 + infinite_cards: ^1.0.1 ``` ### 构建Controller diff --git a/example/lib/home_page.dart b/example/lib/home_page.dart index 820be9e..ce00cc6 100644 --- a/example/lib/home_page.dart +++ b/example/lib/home_page.dart @@ -1,6 +1,7 @@ +import 'dart:math' as math; + import 'package:flutter/material.dart'; import 'package:infinite_cards/infinite_cards.dart'; -import 'dart:math' as math; class Home extends StatefulWidget { @override @@ -62,6 +63,9 @@ class _HomeState extends State { children: [ RaisedButton( onPressed: () { + _controller.reset( + animType: + _isTypeSwitch ? AnimType.SWITCH : AnimType.TO_FRONT); _controller.previous(); }, child: Text("Pre"), @@ -74,6 +78,7 @@ class _HomeState extends State { ), RaisedButton( onPressed: () { + _controller.reset(animType: AnimType.TO_END); _controller.next(); }, child: Text("Next"), @@ -108,7 +113,8 @@ Transform _customToBackTransform( double interpolatorScale = 0.8 - 0.1 * fromPosition + (0.1 * interpolatorFraction * positionCount); double translationY = -cardHeight * (0.8 - interpolatorScale) * 0.5 - - cardHeight * (0.02 * fromPosition - 0.02 * interpolatorFraction * positionCount); + cardHeight * + (0.02 * fromPosition - 0.02 * interpolatorFraction * positionCount); return Transform.translate( offset: Offset(translationX, translationY), child: Transform.scale( diff --git a/example/pubspec.yaml b/example/pubspec.yaml index ae307b6..e17407a 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -17,7 +17,7 @@ environment: sdk: ">=2.1.0 <3.0.0" dependencies: - infinite_cards: ^1.0.0 + infinite_cards: ^1.0.1 flutter: sdk: flutter diff --git a/lib/src/anim_helper.dart b/lib/src/anim_helper.dart index 2112111..d7fe8e6 100644 --- a/lib/src/anim_helper.dart +++ b/lib/src/anim_helper.dart @@ -57,7 +57,7 @@ class AnimHelper { } //not support for TO_END type if (controller.animType == AnimType.TO_END) { - controller.animType = AnimType.TO_FRONT; + return; } _cardAnim(controller.itemCount - 1, _cardList[controller.itemCount - 1]); } @@ -69,7 +69,7 @@ class AnimHelper { } //only support for TO_END type if (controller.animType != AnimType.TO_END) { - controller.animType = AnimType.TO_END; + return; } _cardAnim(0, _cardList[0]); } @@ -80,7 +80,7 @@ class AnimHelper { return; } if (controller.animType == AnimType.TO_END) { - controller.animType = AnimType.TO_FRONT; + return; } _cardAnim(index, _cardList[index]); } @@ -318,15 +318,18 @@ class AnimHelper { // get card widgets List getCardList(double width, double height) { for (int i = 0; i < controller.itemCount; i++) { - if (_isAddRemoveAnim) {//perform add remove anim + if (_isAddRemoveAnim) { + //perform add remove anim if (_isAddAnim) { _addTransform(i, width, height); } else { _removeTransform(i, width, height); } - } else if (_isSwitchAnim) {//perform switch anim + } else if (_isSwitchAnim) { + //perform switch anim _switchTransform(width, height, i); - } else {//perform common anim + } else { + //perform common anim _commonTransform(width, height, i, i); } } @@ -344,14 +347,8 @@ class AnimHelper { void _addTransform(int position, double width, double height) { CardItem cardItem = _cardList[position]; Animation animation = _animationAddRemoveList[position]; - controller.zIndexTransformCommon( - _cardList[position], - animation.value, - _getCurveValue(animation.value), - width, - height, - position + 1, - position); + controller.zIndexTransformCommon(_cardList[position], animation.value, + _getCurveValue(animation.value), width, height, position + 1, position); cardItem.transformWidget = controller.transformAdd( cardItem.widget, animation.value, @@ -366,14 +363,8 @@ class AnimHelper { void _removeTransform(int position, double width, double height) { CardItem cardItem = _cardList[position]; Animation animation = _animationAddRemoveList[position]; - controller.zIndexTransformCommon( - _cardList[position], - animation.value, - _getCurveValue(animation.value), - width, - height, - position, - position); + controller.zIndexTransformCommon(_cardList[position], animation.value, + _getCurveValue(animation.value), width, height, position, position); cardItem.transformWidget = controller.transformRemove( cardItem.widget, animation.value, diff --git a/lib/src/infinite_card_view.dart b/lib/src/infinite_card_view.dart index 95d1936..7433162 100644 --- a/lib/src/infinite_card_view.dart +++ b/lib/src/infinite_card_view.dart @@ -39,7 +39,7 @@ class _InfiniteCardsState extends State }); _helper.init(this, context); if (widget.controller != null) { - widget.controller.setAnimHelper(_helper); + widget.controller.animHelper = _helper; } } diff --git a/lib/src/infinite_cards_controller.dart b/lib/src/infinite_cards_controller.dart index 55c010d..43261b4 100644 --- a/lib/src/infinite_cards_controller.dart +++ b/lib/src/infinite_cards_controller.dart @@ -6,50 +6,67 @@ import 'infinite_card_view.dart'; class InfiniteCardsController { //widget item builder - IndexedWidgetBuilder itemBuilder; + IndexedWidgetBuilder _itemBuilder; + //item count - int itemCount; + int _itemCount; + //switch anim duration - Duration animDuration; + Duration _animDuration; + //true: switch animation while click on item, false: item not clickable - bool clickItemToSwitch; + bool _clickItemToSwitch; + //anim transforms - AnimTransform transformToFront, - transformToBack, - transformCommon, - transformAdd, - transformRemove; + AnimTransform _transformToFront, + _transformToBack, + _transformCommon, + _transformAdd, + _transformRemove; + //zIndex transforms - ZIndexTransform zIndexTransformCommon, - zIndexTransformToFront, - zIndexTransformToBack; + ZIndexTransform _zIndexTransformCommon, + _zIndexTransformToFront, + _zIndexTransformToBack; + //animation type - AnimType animType; + AnimType _animType; + //curve - Curve curve; + Curve _curve; + //helper AnimHelper _animHelper; InfiniteCardsController({ - @required this.itemBuilder, - @required this.itemCount, - this.animDuration, - this.clickItemToSwitch = true, - this.transformToFront = DefaultToFrontTransform, - this.transformToBack = DefaultToBackTransform, - this.transformCommon = DefaultCommonTransform, - this.transformAdd = DefaultAddTransform, - this.transformRemove = DefaultRemoveTransform, - this.zIndexTransformCommon = DefaultCommonZIndexTransform, - this.zIndexTransformToFront = DefaultToFrontZIndexTransform, - this.zIndexTransformToBack = DefaultCommonZIndexTransform, - this.animType = AnimType.TO_FRONT, - this.curve = DefaultCurve, - }); - - void setAnimHelper(AnimHelper helper) { - _animHelper = helper; - } + @required IndexedWidgetBuilder itemBuilder, + @required int itemCount, + Duration animDuration, + bool clickItemToSwitch = true, + AnimTransform transformToFront = DefaultToFrontTransform, + AnimTransform transformToBack = DefaultToBackTransform, + AnimTransform transformCommon = DefaultCommonTransform, + AnimTransform transformAdd = DefaultAddTransform, + AnimTransform transformRemove = DefaultRemoveTransform, + ZIndexTransform zIndexTransformCommon = DefaultCommonZIndexTransform, + ZIndexTransform zIndexTransformToFront = DefaultToFrontZIndexTransform, + ZIndexTransform zIndexTransformToBack = DefaultCommonZIndexTransform, + AnimType animType = AnimType.TO_FRONT, + Curve curve = DefaultCurve, + }) : _itemBuilder = itemBuilder, + _itemCount = itemCount, + _animDuration = animDuration, + _clickItemToSwitch = clickItemToSwitch, + _transformToFront = transformToFront, + _transformToBack = transformToBack, + _transformCommon = transformCommon, + _transformAdd = transformAdd, + _transformRemove = transformRemove, + _zIndexTransformCommon = zIndexTransformCommon, + _zIndexTransformToFront = zIndexTransformToFront, + _zIndexTransformToBack = zIndexTransformToBack, + _animType = animType, + _curve = curve; void previous() { _animHelper.previous(); @@ -88,50 +105,69 @@ class InfiniteCardsController { if (itemBuilder != null || itemCount != null) { forceReset = true; } - if (forceReset) { - //reset params while remove anim comes to an end - _animHelper.animCallback = (AnimStatus status) { - if (status == AnimStatus.RemoveEnd) { - this.itemBuilder = itemBuilder ?? this.itemBuilder; - this.itemCount = itemCount ?? this.itemCount; - this.animDuration = animDuration ?? this.animDuration; - this.clickItemToSwitch = clickItemToSwitch ?? this.clickItemToSwitch; - this.transformToFront = transformToFront ?? this.transformToFront; - this.transformToBack = transformToBack ?? this.transformToBack; - this.transformCommon = transformCommon ?? this.transformCommon; - this.transformAdd = transformAdd ?? this.transformAdd; - this.transformRemove = transformRemove ?? this.transformRemove; - this.zIndexTransformCommon = - zIndexTransformCommon ?? this.zIndexTransformCommon; - this.zIndexTransformToFront = - zIndexTransformToFront ?? this.zIndexTransformToFront; - this.zIndexTransformToBack = - zIndexTransformToBack ?? this.zIndexTransformToBack; - this.animType = animType ?? this.animType; - this.curve = curve ?? this.curve; + //reset params while remove anim comes to an end + _animHelper.animCallback = (AnimStatus status) { + if (status == AnimStatus.RemoveEnd) { + this._itemBuilder = itemBuilder ?? this._itemBuilder; + this._itemCount = itemCount ?? this._itemCount; + this._animDuration = animDuration ?? this._animDuration; + this._clickItemToSwitch = clickItemToSwitch ?? this._clickItemToSwitch; + this._transformToFront = transformToFront ?? this._transformToFront; + this._transformToBack = transformToBack ?? this._transformToBack; + this._transformCommon = transformCommon ?? this._transformCommon; + this._transformAdd = transformAdd ?? this._transformAdd; + this._transformRemove = transformRemove ?? this._transformRemove; + this._zIndexTransformCommon = + zIndexTransformCommon ?? this._zIndexTransformCommon; + this._zIndexTransformToFront = + zIndexTransformToFront ?? this._zIndexTransformToFront; + this._zIndexTransformToBack = + zIndexTransformToBack ?? this._zIndexTransformToBack; + this._animType = animType ?? this._animType; + this._curve = curve ?? this._curve; + if (forceReset) { _animHelper.resetWidgets(); - _animHelper.animCallback = null; } - }; + _animHelper.animCallback = null; + } + }; + if (forceReset) { _animHelper.reset(); return; } - this.itemBuilder = itemBuilder ?? this.itemBuilder; - this.itemCount = itemCount ?? this.itemCount; - this.animDuration = animDuration ?? this.animDuration; - this.clickItemToSwitch = clickItemToSwitch ?? this.clickItemToSwitch; - this.transformToFront = transformToFront ?? this.transformToFront; - this.transformToBack = transformToBack ?? this.transformToBack; - this.transformCommon = transformCommon ?? this.transformCommon; - this.transformAdd = transformAdd ?? this.transformAdd; - this.transformRemove = transformRemove ?? this.transformRemove; - this.zIndexTransformCommon = - zIndexTransformCommon ?? this.zIndexTransformCommon; - this.zIndexTransformToFront = - zIndexTransformToFront ?? this.zIndexTransformToFront; - this.zIndexTransformToBack = - zIndexTransformToBack ?? this.zIndexTransformToBack; - this.animType = animType ?? this.animType; - this.curve = curve ?? this.curve; + //direct set params + _animHelper.animCallback(AnimStatus.RemoveEnd); + } + + Curve get curve => _curve; + + AnimType get animType => _animType; + + get zIndexTransformToBack => _zIndexTransformToBack; + + get zIndexTransformToFront => _zIndexTransformToFront; + + ZIndexTransform get zIndexTransformCommon => _zIndexTransformCommon; + + get transformRemove => _transformRemove; + + get transformAdd => _transformAdd; + + get transformCommon => _transformCommon; + + get transformToBack => _transformToBack; + + AnimTransform get transformToFront => _transformToFront; + + bool get clickItemToSwitch => _clickItemToSwitch; + + Duration get animDuration => _animDuration; + + int get itemCount => _itemCount; + + IndexedWidgetBuilder get itemBuilder => _itemBuilder; + + set animHelper(AnimHelper value) { + _animHelper = value; } } diff --git a/pubspec.yaml b/pubspec.yaml index 2f60151..f330782 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: infinite_cards description: An infinite card switching UI for Flutter, support custom animation -version: 1.0.0 +version: 1.0.1 author: BakerJ <305317218@qq.com> homepage: /~https://github.com/BakerJQ/Flutter-InfiniteCards