Skip to content

Commit

Permalink
feat: add controller file
Browse files Browse the repository at this point in the history
  • Loading branch information
xrr2016 committed Feb 23, 2021
1 parent 763f707 commit 3ff254f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 44 deletions.
76 changes: 32 additions & 44 deletions lib/src/cards.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,13 @@ import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';

import 'animations.dart';
import 'controller.dart';
import 'swip_info.dart';

typedef ForwardCallback(int index, SwipInfo info);
typedef BackCallback(int index);
typedef EndCallback();

/// Card controller
class TCardController {
_TCardState _state;

void _bindState(_TCardState state) {
this._state = state;
}

int get index => _state?._frontCardIndex ?? 0;

forward({SwipDirection direction = SwipDirection.Right}) {
final SwipInfo swipInfo = SwipInfo(_state._frontCardIndex, direction);
_state._swipInfoList.add(swipInfo);
_state._runChangeOrderAnimation();
}

back() {
_state._runReverseOrderAnimation();
}

reset() {
_state._reset();
}

void dispose() {
_state = null;
}
}

/// 卡片列表
class TCard extends StatefulWidget {
/// 卡片尺寸
Expand All @@ -60,7 +32,7 @@ class TCard extends StatefulWidget {

/// 控制Y轴
final bool lockYAxis;

/// How quick should it be slided? less is slower. 10 is a bit slow. 20 is a quick enough.
final double slideSpeed;

Expand All @@ -81,16 +53,20 @@ class TCard extends StatefulWidget {
assert(cards.length > 0);

@override
_TCardState createState() => _TCardState();
TCardState createState() => TCardState();
}

class _TCardState extends State<TCard> with TickerProviderStateMixin {
class TCardState extends State<TCard> with TickerProviderStateMixin {
// 初始的卡片列表
final List<Widget> _cards = [];
// Card swip directions
final List<SwipInfo> _swipInfoList = [];
List<SwipInfo> get swipInfoList => _swipInfoList;

// 最前面卡片的索引
int _frontCardIndex = 0;
int get frontCardIndex => _frontCardIndex;

// 最前面卡片的位置
Alignment _frontCardAlignment = CardAlignments.front;
// 最前面卡片的旋转角度
Expand Down Expand Up @@ -257,7 +233,7 @@ class _TCardState extends State<TCard> with TickerProviderStateMixin {
final simulation = SpringSimulation(spring, 0, 1, -unitVelocity);

_reboundController.animateWith(simulation);
_return();
_resetFrontCard();
}

// 运行卡片向前动画
Expand All @@ -274,6 +250,8 @@ class _TCardState extends State<TCard> with TickerProviderStateMixin {
_cardChangeController.forward();
}

get runChangeOrderAnimation => _runChangeOrderAnimation;

// 运行卡片后退动画
void _runReverseOrderAnimation() {
if (_isAnimating()) {
Expand All @@ -289,10 +267,12 @@ class _TCardState extends State<TCard> with TickerProviderStateMixin {
_cardReverseController.forward();
}

get runReverseOrderAnimation => _runReverseOrderAnimation;

// 向前动画完成后执行
void _forwardCallback() {
_frontCardIndex++;
_return();
_resetFrontCard();
if (widget.onForward != null && widget.onForward is Function) {
widget.onForward(
_frontCardIndex,
Expand All @@ -309,28 +289,35 @@ class _TCardState extends State<TCard> with TickerProviderStateMixin {

// Back animation callback
void _backCallback() {
_return();
_resetFrontCard();
_swipInfoList.removeLast();
if (widget.onBack != null && widget.onBack is Function) {
widget.onBack(_frontCardIndex);
}
}

// 重置最前面卡片的位置
void _return() {
void _resetFrontCard() {
_frontCardRotation = 0.0;
_frontCardAlignment = CardAlignments.front;
setState(() {});
}

// 重置所有卡片
void _reset() {
void _reset({List<Widget> cards}) {
_cards.clear();
_cards.addAll(widget.cards);
if (cards != null) {
_cards.addAll(cards);
} else {
_cards.addAll(widget.cards);
}
_swipInfoList.clear();
_frontCardIndex = 0;
_return();
_resetFrontCard();
}

get reset => _reset;

// Stop animations
void _stop() {
_reboundController.stop();
Expand All @@ -340,13 +327,14 @@ class _TCardState extends State<TCard> with TickerProviderStateMixin {

// 更新最前面卡片的位置
void _updateFrontCardAlignment(DragUpdateDetails details, Size size) {

// 卡片移动速度 widget.slideSpeed
_frontCardAlignment += Alignment(
details.delta.dx / (size.width / 2) * widget.slideSpeed,
widget.lockYAxis ? 0 : details.delta.dy / (size.height / 2) * widget.slideSpeed,
details.delta.dx / (size.width / 2) * widget.slideSpeed,
widget.lockYAxis
? 0
: details.delta.dy / (size.height / 2) * widget.slideSpeed,
);

// 设置最前面卡片的旋转角度
_frontCardRotation = _frontCardAlignment.x;
setState(() {});
Expand Down Expand Up @@ -381,7 +369,7 @@ class _TCardState extends State<TCard> with TickerProviderStateMixin {

// 绑定控制器
if (widget.controller != null && widget.controller is TCardController) {
widget.controller._bindState(this);
widget.controller.bindState(this);
}

// 初始化向前的动画控制器
Expand Down
31 changes: 31 additions & 0 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter/widgets.dart';

import 'cards.dart';
import 'swip_info.dart';

/// Card controller
class TCardController {
TCardState _state;

void bindState(TCardState state) {
this._state = state;
}

int get index => _state?.frontCardIndex ?? 0;

forward({SwipDirection direction = SwipDirection.Right}) {
final SwipInfo swipInfo = SwipInfo(_state.frontCardIndex, direction);
_state.swipInfoList.add(swipInfo);
_state.runChangeOrderAnimation();
}

back() {
_state.runReverseOrderAnimation();
}

get reset => _state.reset;

void dispose() {
_state = null;
}
}
1 change: 1 addition & 0 deletions lib/tcard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ library tcard;

export 'src/cards.dart';
export 'src/swip_info.dart';
export 'src/controller.dart';

0 comments on commit 3ff254f

Please sign in to comment.