From adf7c48d3e12210836a97ec9c5dc5c63a72f56a2 Mon Sep 17 00:00:00 2001 From: Hixie Date: Tue, 15 Sep 2015 11:01:40 -0700 Subject: [PATCH] Be more explicit about what's state in dismissable Move _activeCardDragEndPoint near build() so it's more obvious that it is part of the build state. Make a couple of functions use setState() since they modify variables that are used by build(). Add a more detailed comment to the empty setState() call, since those are dubious in general and need explaining when they occur, lest people start using them as magic incantations to Make Things Work. --- .../flutter/lib/src/widgets/dismissable.dart | 56 +++++++++++-------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/packages/flutter/lib/src/widgets/dismissable.dart b/packages/flutter/lib/src/widgets/dismissable.dart index b887974b87..95758ba6d5 100644 --- a/packages/flutter/lib/src/widgets/dismissable.dart +++ b/packages/flutter/lib/src/widgets/dismissable.dart @@ -75,14 +75,6 @@ class Dismissable extends StatefulComponent { _startResizePerformance(); } - Point get _activeCardDragEndPoint { - if (!_isActive) - return Point.origin; - assert(_size != null); - double extent = _directionIsYAxis ? _size.height : _size.width; - return new Point(_dragExtent.sign * extent * _kDismissCardThreshold, 0.0); - } - bool get _isActive { return _size != null && (_dragUnderway || _fadePerformance.isAnimating); } @@ -120,9 +112,11 @@ class Dismissable extends StatefulComponent { void _handleDragStart() { if (_fadePerformance.isAnimating) return; - _dragUnderway = true; - _dragExtent = 0.0; - _fadePerformance.progress = 0.0; + setState(() { + _dragUnderway = true; + _dragExtent = 0.0; + _fadePerformance.progress = 0.0; + }); } void _handleDragUpdate(double scrollOffset) { @@ -149,8 +143,14 @@ class Dismissable extends StatefulComponent { break; } - if (oldDragExtent.sign != _dragExtent.sign) - setState(() {}); // Rebuild to update the new drag endpoint. + if (oldDragExtent.sign != _dragExtent.sign) { + setState(() { + // Rebuild to update the new drag endpoint. + // The sign of _dragExtent is part of our build state; + // the actual value is not, it's just used to configure + // the performances. + }); + } if (!_fadePerformance.isAnimating) _fadePerformance.progress = _dragExtent.abs() / (_size.width * _kDismissCardThreshold); } @@ -188,16 +188,18 @@ class Dismissable extends StatefulComponent { if (!_isActive || _fadePerformance.isAnimating) return; - _dragUnderway = false; - if (_fadePerformance.isCompleted) { - _startResizePerformance(); - } else if (_isFlingGesture(velocity)) { - double flingVelocity = _directionIsYAxis ? velocity.dy : velocity.dx; - _dragExtent = flingVelocity.sign; - _fadePerformance.fling(velocity: flingVelocity.abs() * _kFlingVelocityScale); - } else { - _fadePerformance.reverse(); - } + setState(() { + _dragUnderway = false; + if (_fadePerformance.isCompleted) { + _startResizePerformance(); + } else if (_isFlingGesture(velocity)) { + double flingVelocity = _directionIsYAxis ? velocity.dy : velocity.dx; + _dragExtent = flingVelocity.sign; + _fadePerformance.fling(velocity: flingVelocity.abs() * _kFlingVelocityScale); + } else { + _fadePerformance.reverse(); + } + }); } void _handleSizeChanged(Size newSize) { @@ -206,6 +208,14 @@ class Dismissable extends StatefulComponent { }); } + Point get _activeCardDragEndPoint { + if (!_isActive) + return Point.origin; + assert(_size != null); + double extent = _directionIsYAxis ? _size.height : _size.width; + return new Point(_dragExtent.sign * extent * _kDismissCardThreshold, 0.0); + } + Widget build() { if (_resizePerformance != null) { AnimatedValue squashAxisExtent = new AnimatedValue(