Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new AnimationStatus getters #148570

Merged
merged 4 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions dev/benchmarks/macrobenchmarks/lib/src/cubic_bezier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,10 @@ class AnimatedBezierState extends State<AnimatedBezier>
..addListener(() {
setState(() {});
})
..addStatusListener((AnimationStatus state) {
if (state == AnimationStatus.completed) {
..addStatusListener((AnimationStatus status) {
if (status.isCompleted) {
reverseAnimation();
} else if (state == AnimationStatus.dismissed) {
} else if (status.isDismissed) {
playAnimation();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ColumnOfTextState extends State<ColumnOfText> with SingleTickerProviderSta
duration: const Duration(milliseconds: 300),
)
..addStatusListener((AnimationStatus status) {
if (status == AnimationStatus.completed) {
if (status.isCompleted) {
setState(() {
_showText = !_showText;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,8 @@ class _BackdropDemoState extends State<BackdropDemo> with SingleTickerProviderSt
});
}

bool get _backdropPanelVisible {
final AnimationStatus status = _controller.status;
return status == AnimationStatus.completed || status == AnimationStatus.forward;
}

void _toggleBackdropPanelVisibility() {
_controller.fling(velocity: _backdropPanelVisible ? -2.0 : 2.0);
_controller.fling(velocity: _controller.isForwardOrCompleted ? -2.0 : 2.0);
}

double get _backdropHeight {
Expand All @@ -294,15 +289,15 @@ class _BackdropDemoState extends State<BackdropDemo> with SingleTickerProviderSt
// the user must either tap its heading or the backdrop's menu icon.

void _handleDragUpdate(DragUpdateDetails details) {
if (_controller.isAnimating || _controller.status == AnimationStatus.completed) {
if (!_controller.isDismissed) {
return;
}

_controller.value -= details.primaryDelta! / _backdropHeight;
}

void _handleDragEnd(DragEndDetails details) {
if (_controller.isAnimating || _controller.status == AnimationStatus.completed) {
if (!_controller.isDismissed) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,7 @@ class ExpandingBottomSheetState extends State<ExpandingBottomSheet> with TickerP
}

// Returns true if the cart is open or opening and false otherwise.
bool get _isOpen {
final AnimationStatus status = _controller.status;
return status == AnimationStatus.completed || status == AnimationStatus.forward;
}
bool get _isOpen => _controller.isForwardOrCompleted;

// Opens the ExpandingBottomSheet if it's closed, otherwise does nothing.
void open() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class _BackdropState extends State<Backdrop> with SingleTickerProviderStateMixin
}

void _handleDragEnd(DragEndDetails details) {
if (_controller!.isAnimating || _controller!.status == AnimationStatus.completed) {
if (!_controller!.isDismissed) {
return;
}

Expand All @@ -256,9 +256,7 @@ class _BackdropState extends State<Backdrop> with SingleTickerProviderStateMixin
}

void _toggleFrontLayer() {
final AnimationStatus status = _controller!.status;
final bool isOpen = status == AnimationStatus.completed || status == AnimationStatus.forward;
_controller!.fling(velocity: isOpen ? -2.0 : 2.0);
_controller!.fling(velocity: _controller!.isForwardOrCompleted ? -2.0 : 2.0);
}

Widget _buildStack(BuildContext context, BoxConstraints constraints) {
Expand Down Expand Up @@ -295,7 +293,7 @@ class _BackdropState extends State<Backdrop> with SingleTickerProviderStateMixin
AnimationStatus.dismissed,
controller: _controller,
child: Visibility(
visible: _controller!.status != AnimationStatus.completed,
visible: !_controller!.isCompleted,
maintainState: true,
child: widget.backLayer!,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class _FadeScaleTransitionDemoState extends State<FadeScaleTransitionDemo>
);
},
child: Visibility(
visible: _controller.status != AnimationStatus.dismissed,
visible: !_controller.isDismissed,
child: FloatingActionButton(
onPressed: () {},
child: const Icon(Icons.add),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,14 @@ class _FeatureDiscoveryState extends State<FeatureDiscovery>
setState(() {});
})
..addStatusListener((AnimationStatus animationStatus) {
if (animationStatus == AnimationStatus.forward) {
setState(() => status = FeatureDiscoveryStatus.open);
} else if (animationStatus == AnimationStatus.completed) {
rippleController.forward(from: 0.0);
switch (animationStatus) {
case AnimationStatus.forward:
setState(() => status = FeatureDiscoveryStatus.open);
case AnimationStatus.completed:
rippleController.forward(from: 0.0);
case AnimationStatus.reverse:
case AnimationStatus.dismissed:
break;
Comment on lines -290 to +297
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't an isForward getter, so I decided to do a switch statement here (for readability and to follow the style guide).

}
});

Expand All @@ -302,10 +306,14 @@ class _FeatureDiscoveryState extends State<FeatureDiscovery>
setState(() {});
})
..addStatusListener((AnimationStatus animationStatus) {
if (animationStatus == AnimationStatus.forward) {
setState(() => status = FeatureDiscoveryStatus.ripple);
} else if (animationStatus == AnimationStatus.completed) {
rippleController.forward(from: 0.0);
switch (animationStatus) {
case AnimationStatus.forward:
setState(() => status = FeatureDiscoveryStatus.ripple);
case AnimationStatus.completed:
rippleController.forward(from: 0.0);
case AnimationStatus.reverse:
case AnimationStatus.dismissed:
break;
}
});

Expand All @@ -317,11 +325,15 @@ class _FeatureDiscoveryState extends State<FeatureDiscovery>
setState(() {});
})
..addStatusListener((AnimationStatus animationStatus) {
if (animationStatus == AnimationStatus.forward) {
setState(() => status = FeatureDiscoveryStatus.tap);
} else if (animationStatus == AnimationStatus.completed) {
widget.onTap?.call();
cleanUponOverlayClose();
switch (animationStatus) {
case AnimationStatus.forward:
setState(() => status = FeatureDiscoveryStatus.tap);
case AnimationStatus.completed:
widget.onTap?.call();
cleanUponOverlayClose();
case AnimationStatus.reverse:
case AnimationStatus.dismissed:
break;
}
});

Expand All @@ -333,11 +345,15 @@ class _FeatureDiscoveryState extends State<FeatureDiscovery>
setState(() {});
})
..addStatusListener((AnimationStatus animationStatus) {
if (animationStatus == AnimationStatus.forward) {
setState(() => status = FeatureDiscoveryStatus.dismiss);
} else if (animationStatus == AnimationStatus.completed) {
widget.onDismiss?.call();
cleanUponOverlayClose();
switch (animationStatus) {
case AnimationStatus.forward:
setState(() => status = FeatureDiscoveryStatus.dismiss);
case AnimationStatus.completed:
widget.onDismiss?.call();
cleanUponOverlayClose();
case AnimationStatus.reverse:
case AnimationStatus.dismissed:
break;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,8 @@ class _CategoryListItemState extends State<CategoryListItem>
super.dispose();
}

bool _shouldOpenList() {
switch (_controller.status) {
case AnimationStatus.completed:
case AnimationStatus.forward:
case AnimationStatus.reverse:
return false;
case AnimationStatus.dismissed:
return true;
}
}

void _handleTap() {
if (_shouldOpenList()) {
if (_controller.isDismissed) {
_controller.forward();
if (widget.onTap != null) {
widget.onTap!(true);
Expand Down Expand Up @@ -148,7 +137,7 @@ class _CategoryListItemState extends State<CategoryListItem>
return AnimatedBuilder(
animation: _controller.view,
builder: _buildHeaderWithChildren,
child: _shouldOpenList()
child: _controller.isDismissed
? null
: _ExpandedCategoryDemos(
category: widget.category,
Expand Down
2 changes: 1 addition & 1 deletion dev/integration_tests/new_gallery/lib/pages/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class _SettingsPageState extends State<SettingsPage> {
}

void _closeSettingId(AnimationStatus status) {
if (status == AnimationStatus.dismissed) {
if (status.isDismissed) {
setState(() {
_expandedSettingId = null;
});
Expand Down
10 changes: 3 additions & 7 deletions dev/integration_tests/new_gallery/lib/pages/splash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class _SplashPageState extends State<SplashPage>
return true;
},
child: SplashPageAnimation(
isFinished: _controller.status == AnimationStatus.dismissed,
isFinished: _controller.isDismissed,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
final Animation<RelativeRect> animation = _getPanelAnimation(context, constraints);
Expand All @@ -122,9 +122,7 @@ class _SplashPageState extends State<SplashPage>
cursor: SystemMouseCursors.click,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
_controller.reverse();
},
onTap: _controller.reverse,
onVerticalDragEnd: (DragEndDetails details) {
if (details.velocity.pixelsPerSecond.dy < -200) {
_controller.reverse();
Expand Down Expand Up @@ -168,9 +166,7 @@ class _SplashPageState extends State<SplashPage>
_SplashBackLayer(
isSplashCollapsed: !_isSplashVisible,
effect: _effect,
onTap: () {
_controller.forward();
},
onTap: _controller.forward,
),
PositionedTransition(
rect: animation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,7 @@ class _MobileNavState extends State<_MobileNav> with TickerProviderStateMixin {
super.dispose();
}

bool get _bottomDrawerVisible {
final AnimationStatus status = _drawerController.status;
return status == AnimationStatus.completed ||
status == AnimationStatus.forward;
}
bool get _bottomDrawerVisible => _drawerController.isForwardOrCompleted;

void _toggleBottomDrawerVisibility() {
if (_drawerController.value < 0.4) {
Expand All @@ -550,8 +546,7 @@ class _MobileNavState extends State<_MobileNav> with TickerProviderStateMixin {
}

void _handleDragEnd(DragEndDetails details) {
if (_drawerController.isAnimating ||
_drawerController.status == AnimationStatus.completed) {
if (!_drawerController.isDismissed) {
return;
}

Expand Down
10 changes: 3 additions & 7 deletions dev/integration_tests/new_gallery/lib/studies/shrine/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ class _ShrineAppState extends State<ShrineApp>
// Save state restoration animation values only when the cart page
// fully opens or closes.
_controller.addStatusListener((AnimationStatus status) {
if (status == AnimationStatus.completed ||
status == AnimationStatus.dismissed) {
if (!status.isAnimating) {
_tabIndex.value = _controller.value;
}
});
Expand All @@ -83,8 +82,7 @@ class _ShrineAppState extends State<ShrineApp>
// Save state restoration animation values only when the menu page
// fully opens or closes.
_expandingController.addStatusListener((AnimationStatus status) {
if (status == AnimationStatus.completed ||
status == AnimationStatus.dismissed) {
if (!status.isAnimating) {
_expandingTabIndex.value = _expandingController.value;
}
});
Expand Down Expand Up @@ -118,9 +116,7 @@ class _ShrineAppState extends State<ShrineApp>

// Closes the bottom sheet if it is open.
Future<bool> _onWillPop() async {
final AnimationStatus status = _expandingController.status;
if (status == AnimationStatus.completed ||
status == AnimationStatus.forward) {
if (_expandingController.isForwardOrCompleted) {
await _expandingController.reverse();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,7 @@ class ExpandingBottomSheetState extends State<ExpandingBottomSheet> {
}

// Returns true if the cart is open or opening and false otherwise.
bool get _isOpen {
final AnimationStatus status = _controller.status;
return status == AnimationStatus.completed ||
status == AnimationStatus.forward;
}
bool get _isOpen => _controller.isForwardOrCompleted;

// Opens the ExpandingBottomSheet if it's closed, otherwise does nothing.
void open() {
Expand Down
41 changes: 17 additions & 24 deletions dev/integration_tests/new_gallery/lib/studies/shrine/scrim.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,28 @@ class Scrim extends StatelessWidget {

@override
Widget build(BuildContext context) {
final Size deviceSize = MediaQuery.of(context).size;
return ExcludeSemantics(
child: AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget? child) {
final Color color =
const Color(0xFFFFF0EA).withOpacity(controller.value * 0.87);
final Widget scrimRectangle = ColoredBox(
color: Color.fromRGBO(0xFF, 0xF0, 0xEA, controller.value * 0.87),
child: SizedBox.fromSize(size: MediaQuery.sizeOf(context)),
);

final Widget scrimRectangle = Container(
width: deviceSize.width, height: deviceSize.height, color: color);

final bool ignorePointer =
(controller.status == AnimationStatus.dismissed);
final bool tapToRevert = (controller.status == AnimationStatus.completed);

if (tapToRevert) {
return MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
onTap: () {
controller.reverse();
},
child: scrimRectangle,
),
);
} else if (ignorePointer) {
return IgnorePointer(child: scrimRectangle);
} else {
return scrimRectangle;
switch (controller.status) {
case AnimationStatus.completed:
return MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
onTap: controller.reverse,
child: scrimRectangle,
),
);
case AnimationStatus.dismissed:
return IgnorePointer(child: scrimRectangle);
case AnimationStatus.forward || AnimationStatus.reverse:
return scrimRectangle;
}
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class _AnimatedDigitState extends State<AnimatedDigit> with SingleTickerProvider
}

void handleAnimationCompleted(AnimationStatus status) {
if (status == AnimationStatus.completed) {
if (status.isCompleted) {
if (pendingValues.isNotEmpty) {
// Display the next pending value. The duration was scaled down
// in didUpdateWidget by the total number of pending values so
Expand Down