From c9ec61ee9078574e3feba1c0fabc7d7975a663b3 Mon Sep 17 00:00:00 2001 From: Nate Wilson Date: Tue, 2 Jul 2024 11:31:53 -0600 Subject: [PATCH] `dismissible.dart` code cleanup (#150276) While I was working on #149966, I noticed a couple of ways that `_DismissibleState` could be tweaked to be easier to follow. Changes are described in comments below, and we're currently [awaiting a test exemption](https://discord.com/channels/608014603317936148/608018585025118217/1251255349277626490). --- .../flutter/lib/src/widgets/dismissible.dart | 67 ++++++++++--------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/packages/flutter/lib/src/widgets/dismissible.dart b/packages/flutter/lib/src/widgets/dismissible.dart index 041d378150..c47545c837 100644 --- a/packages/flutter/lib/src/widgets/dismissible.dart +++ b/packages/flutter/lib/src/widgets/dismissible.dart @@ -299,13 +299,16 @@ class _DismissibleState extends State with TickerProviderStateMixin @override void initState() { super.initState(); - _moveController = AnimationController(duration: widget.movementDuration, vsync: this) + _moveController ..addStatusListener(_handleDismissStatusChanged) ..addListener(_handleDismissUpdateValueChanged); _updateMoveAnimation(); } - AnimationController? _moveController; + late final AnimationController _moveController = AnimationController( + duration: widget.movementDuration, + vsync: this, + ); late Animation _moveAnimation; AnimationController? _resizeController; @@ -320,11 +323,11 @@ class _DismissibleState extends State with TickerProviderStateMixin final GlobalKey _contentKey = GlobalKey(); @override - bool get wantKeepAlive => (_moveController?.isAnimating ?? false) || (_resizeController?.isAnimating ?? false); + bool get wantKeepAlive => _moveController.isAnimating || (_resizeController?.isAnimating ?? false); @override void dispose() { - _moveController!.dispose(); + _moveController.dispose(); _resizeController?.dispose(); super.dispose(); } @@ -351,9 +354,7 @@ class _DismissibleState extends State with TickerProviderStateMixin DismissDirection get _dismissDirection => _extentToDirection(_dragExtent); - bool get _isActive { - return _dragUnderway || _moveController!.isAnimating; - } + double get _dismissThreshold => widget.dismissThresholds[_dismissDirection] ?? _kDismissThreshold; double get _overallDragAxisExtent { final Size size = context.size!; @@ -365,12 +366,12 @@ class _DismissibleState extends State with TickerProviderStateMixin return; } _dragUnderway = true; - if (_moveController!.isAnimating) { - _dragExtent = _moveController!.value * _overallDragAxisExtent * _dragExtent.sign; - _moveController!.stop(); + if (_moveController.isAnimating) { + _dragExtent = _moveController.value * _overallDragAxisExtent * _dragExtent.sign; + _moveController.stop(); } else { _dragExtent = 0.0; - _moveController!.value = 0.0; + _moveController.value = 0.0; } setState(() { _updateMoveAnimation(); @@ -378,7 +379,7 @@ class _DismissibleState extends State with TickerProviderStateMixin } void _handleDragUpdate(DragUpdateDetails details) { - if (!_isActive || _moveController!.isAnimating) { + if (!_dragUnderway || _moveController.isAnimating) { return; } @@ -431,20 +432,20 @@ class _DismissibleState extends State with TickerProviderStateMixin _updateMoveAnimation(); }); } - if (!_moveController!.isAnimating) { - _moveController!.value = _dragExtent.abs() / _overallDragAxisExtent; + if (!_moveController.isAnimating) { + _moveController.value = _dragExtent.abs() / _overallDragAxisExtent; } } void _handleDismissUpdateValueChanged() { if (widget.onUpdate != null) { final bool oldDismissThresholdReached = _dismissThresholdReached; - _dismissThresholdReached = _moveController!.value > (widget.dismissThresholds[_dismissDirection] ?? _kDismissThreshold); + _dismissThresholdReached = _moveController.value > _dismissThreshold; final DismissUpdateDetails details = DismissUpdateDetails( direction: _dismissDirection, reached: _dismissThresholdReached, previousReached: oldDismissThresholdReached, - progress: _moveController!.value, + progress: _moveController.value, ); widget.onUpdate!(details); } @@ -452,7 +453,7 @@ class _DismissibleState extends State with TickerProviderStateMixin void _updateMoveAnimation() { final double end = _dragExtent.sign; - _moveAnimation = _moveController!.drive( + _moveAnimation = _moveController.drive( Tween( begin: Offset.zero, end: _directionIsXAxis @@ -495,11 +496,11 @@ class _DismissibleState extends State with TickerProviderStateMixin } void _handleDragEnd(DragEndDetails details) { - if (!_isActive || _moveController!.isAnimating) { + if (!_dragUnderway || _moveController.isAnimating) { return; } _dragUnderway = false; - if (_moveController!.isCompleted) { + if (_moveController.isCompleted) { _handleMoveCompleted(); return; } @@ -507,24 +508,24 @@ class _DismissibleState extends State with TickerProviderStateMixin switch (_describeFlingGesture(details.velocity)) { case _FlingGestureKind.forward: assert(_dragExtent != 0.0); - assert(!_moveController!.isDismissed); - if ((widget.dismissThresholds[_dismissDirection] ?? _kDismissThreshold) >= 1.0) { - _moveController!.reverse(); + assert(!_moveController.isDismissed); + if (_dismissThreshold >= 1.0) { + _moveController.reverse(); break; } _dragExtent = flingVelocity.sign; - _moveController!.fling(velocity: flingVelocity.abs() * _kFlingVelocityScale); + _moveController.fling(velocity: flingVelocity.abs() * _kFlingVelocityScale); case _FlingGestureKind.reverse: assert(_dragExtent != 0.0); - assert(!_moveController!.isDismissed); + assert(!_moveController.isDismissed); _dragExtent = flingVelocity.sign; - _moveController!.fling(velocity: -flingVelocity.abs() * _kFlingVelocityScale); + _moveController.fling(velocity: -flingVelocity.abs() * _kFlingVelocityScale); case _FlingGestureKind.none: - if (!_moveController!.isDismissed) { // we already know it's not completed, we check that above - if (_moveController!.value > (widget.dismissThresholds[_dismissDirection] ?? _kDismissThreshold)) { - _moveController!.forward(); + if (!_moveController.isDismissed) { // we already know it's not completed, we check that above + if (_moveController.value > _dismissThreshold) { + _moveController.forward(); } else { - _moveController!.reverse(); + _moveController.reverse(); } } } @@ -540,8 +541,8 @@ class _DismissibleState extends State with TickerProviderStateMixin } Future _handleMoveCompleted() async { - if ((widget.dismissThresholds[_dismissDirection] ?? _kDismissThreshold) >= 1.0) { - _moveController!.reverse(); + if (_dismissThreshold >= 1.0) { + _moveController.reverse(); return; } final bool result = await _confirmStartResizeAnimation(); @@ -549,7 +550,7 @@ class _DismissibleState extends State with TickerProviderStateMixin if (result) { _startResizeAnimation(); } else { - _moveController!.reverse(); + _moveController.reverse(); } } } @@ -568,7 +569,7 @@ class _DismissibleState extends State with TickerProviderStateMixin } void _startResizeAnimation() { - assert(_moveController!.isCompleted); + assert(_moveController.isCompleted); assert(_resizeController == null); assert(_sizePriorToCollapse == null); if (widget.resizeDuration == null) {