Skip to content

Commit

Permalink
change param in controller to private
Browse files Browse the repository at this point in the history
  • Loading branch information
BakerJQ committed Apr 2, 2019
1 parent ed93810 commit 32eba53
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 102 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [1.0.1]

- change controller params to private
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ flutter run
### Add dependencies
```yaml
dependencies:
infinite_cards: ^1.0.0
infinite_cards: ^1.0.1
```
### Build controller in initState
Expand Down
2 changes: 1 addition & 1 deletion README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ flutter run
### 添加依赖
```yaml
dependencies:
infinite_cards: ^1.0.0
infinite_cards: ^1.0.1
```
### 构建Controller
Expand Down
10 changes: 8 additions & 2 deletions example/lib/home_page.dart
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -62,6 +63,9 @@ class _HomeState extends State<Home> {
children: <Widget>[
RaisedButton(
onPressed: () {
_controller.reset(
animType:
_isTypeSwitch ? AnimType.SWITCH : AnimType.TO_FRONT);
_controller.previous();
},
child: Text("Pre"),
Expand All @@ -74,6 +78,7 @@ class _HomeState extends State<Home> {
),
RaisedButton(
onPressed: () {
_controller.reset(animType: AnimType.TO_END);
_controller.next();
},
child: Text("Next"),
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 13 additions & 22 deletions lib/src/anim_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand All @@ -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]);
}
Expand All @@ -80,7 +80,7 @@ class AnimHelper {
return;
}
if (controller.animType == AnimType.TO_END) {
controller.animType = AnimType.TO_FRONT;
return;
}
_cardAnim(index, _cardList[index]);
}
Expand Down Expand Up @@ -318,15 +318,18 @@ class AnimHelper {
// get card widgets
List<Widget> 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);
}
}
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/infinite_card_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class _InfiniteCardsState extends State<InfiniteCards>
});
_helper.init(this, context);
if (widget.controller != null) {
widget.controller.setAnimHelper(_helper);
widget.controller.animHelper = _helper;
}
}

Expand Down
182 changes: 109 additions & 73 deletions lib/src/infinite_cards_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 32eba53

Please sign in to comment.