Skip to content

Commit

Permalink
## [0.3.0]
Browse files Browse the repository at this point in the history
* fix issue that AnimationController.stop() called after AnimationController.dispose().
* show how to build a double tap scale animation.
  • Loading branch information
zmtzawqlp committed Apr 24, 2019
1 parent 10f8213 commit 59ed7be
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [0.3.0]

* fix issue that AnimationController.stop() called after AnimationController.dispose().
* show how to build a double tap scale animation.

## [0.2.9]

* add handleDoubleTap method to support zoom image base on double tap position.
Expand Down
38 changes: 31 additions & 7 deletions example/lib/common/pic_swiper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ class PicSwiper extends StatefulWidget {
_PicSwiperState createState() => _PicSwiperState();
}

class _PicSwiperState extends State<PicSwiper> {
class _PicSwiperState extends State<PicSwiper>
with SingleTickerProviderStateMixin {
var rebuild = StreamController<int>.broadcast();
AnimationController _animationController;
Animation<double> _animation;
Function animationListener;
// CancellationToken _cancelToken;
// CancellationToken get cancelToken {
// if (_cancelToken == null || _cancelToken.isCanceled)
Expand All @@ -31,13 +35,16 @@ class _PicSwiperState extends State<PicSwiper> {
@override
void initState() {
currentIndex = widget.index;
_animationController = AnimationController(
duration: const Duration(milliseconds: 150), vsync: this);
// TODO: implement initState
super.initState();
}

@override
void dispose() {
rebuild.close();
_animationController?.dispose();
clearGestureDetailsCache();
//cancelToken?.cancel();
// TODO: implement dispose
Expand Down Expand Up @@ -95,15 +102,32 @@ class _PicSwiperState extends State<PicSwiper> {
///you can use define pointerDownPosition as you can,
///default value is double tap pointer down postion.
var pointerDownPosition = state.pointerDownPosition;
if (state.gestureDetails.totalScale == doubleTapScales[0]) {
state.handleDoubleTap(
scale: doubleTapScales[1],
doubleTapPosition: pointerDownPosition);
double begin = state.gestureDetails.totalScale;
double end;
_animationController.stop();

_animationController.reset();

if (begin == doubleTapScales[0]) {
end = doubleTapScales[1];
} else {
end = doubleTapScales[0];
}
//remove old
_animation?.removeListener(animationListener);

animationListener = () {
//print(_animation.value);
state.handleDoubleTap(
scale: doubleTapScales[0],
scale: _animation.value,
doubleTapPosition: pointerDownPosition);
}
};
_animation = _animationController
.drive(Tween<double>(begin: begin, end: end));

_animation.addListener(animationListener);

_animationController.forward();
},
);
image = Container(
Expand Down
39 changes: 23 additions & 16 deletions lib/src/gesture/extended_image_gesture.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,25 @@ class _ExtendedImageGestureState extends State<ExtendedImageGesture>
);

_gestureAnimation = GestureAnimation(this, offsetCallBack: (Offset value) {
setState(() {
_gestureDetails = GestureDetails(
offset: value,
totalScale: _gestureDetails.totalScale,
gestureDetails: _gestureDetails);
});
if (mounted) {
setState(() {
_gestureDetails = GestureDetails(
offset: value,
totalScale: _gestureDetails.totalScale,
gestureDetails: _gestureDetails);
});
}
}, scaleCallBack: (double scale) {
setState(() {
_gestureDetails = GestureDetails(
offset: _gestureDetails.offset,
totalScale: scale,
gestureDetails: _gestureDetails,
zooming: true,
userOffset: false);
});
if (mounted) {
setState(() {
_gestureDetails = GestureDetails(
offset: _gestureDetails.offset,
totalScale: scale,
gestureDetails: _gestureDetails,
zooming: true,
userOffset: false);
});
}
});

super.initState();
Expand Down Expand Up @@ -102,8 +106,9 @@ class _ExtendedImageGestureState extends State<ExtendedImageGesture>
((details.scale == 1.0 ? details.focalPoint : _startingOffset) -
_normalizedOffset * scale);

if (offset != _gestureDetails.offset ||
scale != _gestureDetails.totalScale) {
if (mounted &&
(offset != _gestureDetails.offset ||
scale != _gestureDetails.totalScale)) {
setState(() {
_gestureDetails = GestureDetails(
offset: offset,
Expand Down Expand Up @@ -159,6 +164,8 @@ class _ExtendedImageGestureState extends State<ExtendedImageGesture>
return;
}

if (!mounted) return;

setState(() {
_gestureDetails = GestureDetails(
offset: Offset.zero,
Expand Down
8 changes: 5 additions & 3 deletions lib/src/gesture/extended_image_gesture_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ class GestureAnimation {
}

void animationOffset(Offset begin, Offset end) {
if (_offsetController == null) return;
_offsetAnimation =
_offsetController.drive(Tween<Offset>(begin: begin, end: end));
_offsetController
Expand All @@ -353,6 +354,7 @@ class GestureAnimation {
}

void animationScale(double begin, double end, double velocity) {
if (_scaleController == null) return;
_scaletAnimation =
_scaleController.drive(Tween<double>(begin: begin, end: end));
_scaleController
Expand All @@ -362,14 +364,14 @@ class GestureAnimation {

void dispose() {
_offsetController?.dispose();
_offsetController = null;

_scaleController?.dispose();
_scaleController = null;
}

void stop() {
_offsetController?.stop();
// if (_scaletAnimation != null) {
// _scaleController.value = 1.0;
// }
_scaleController?.stop();
}
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: extended_image
description: extended official image with loading/failed state,cache network,zoom/pan,photo view,crop,save,clip,paint custom.
version: 0.2.9
version: 0.3.0
author: zmtzapxh <zmtzapxhlp@live.com>
homepage: https://github.com/fluttercandies/extended_image

Expand Down

0 comments on commit 59ed7be

Please sign in to comment.