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
Proposal: use "enhanced enums" in the repo for reduced boilerplate #147799
Comments
You showed that currently have something like this: enum AnimationStatus {
/// The animation is stopped at the beginning.
dismissed,
/// The animation is running from beginning to end.
forward,
/// The animation is running backwards, from end to beginning.
reverse,
/// The animation is stopped at the end.
completed,
}
final AnimationStatus status = _controller.status;
final bool returnValue = status == AnimationStatus.completed || status == AnimationStatus.forward;
Would you mind to show the proposed cleaner, simpler and more performant preferred new way that reduces the boilerplate? |
@FusoraTech I forgot to link to this issue in the pull request I made, my bad! I was going to wait to refactor until that PR is approved, but I'd be happy to share some ideas now 🙂 animated_cross_fade.dartbeforebool get _isTransitioning => _controller.status == AnimationStatus.forward || _controller.status == AnimationStatus.reverse;
Widget build(BuildContext context) {
final bool transitioningForwards = _controller.status == AnimationStatus.completed ||
_controller.status == AnimationStatus.forward;
if (transitioningForwards) {
// ...
}
}
afterbool get _isTransitioning => _controller.isRunning;
Widget build(BuildContext context) {
if (_controller.aimedForward) {
// ...
}
}
|
I understand. The idea is to make specialized getters right inside those "smart enums", replacing complex reusable ifs with switches when possible, and reducing the boilerplate replacing those long ifs spread everywhere by those new concise getters. |
Just to be clear, my worry was about exchanging performance in one place by complexity in another place, and performance penalty adding an extra call layer, just to satisfy a code clarity guidance. |
Based on issue #147799, this pull request adds two `AnimationStatus` getters. ```dart bool get isRunning => switch (this) { forward || reverse => true, completed || dismissed => false, }; bool get aimedForward => switch (this) { forward || completed => true, reverse || dismissed => false, }; ``` I also added a `.toggle()` method for animation controllers that makes use of `aimedForward`.
There are a few situations where the same enum logic is implemented over and over again.
A good example would be distinguishing an
AnimationStatus
between forward/complete and reverse/dismissed.15 style guide violations (click to collapse)
flutter/dev/integration_tests/flutter_gallery/lib/demo/material/backdrop_demo.dart
Line 281 in 31847bc
flutter/dev/integration_tests/flutter_gallery/lib/demo/shrine/backdrop.dart
Line 235 in 31847bc
flutter/dev/integration_tests/flutter_gallery/lib/demo/shrine/expanding_bottom_sheet.dart
Line 236 in 31847bc
flutter/dev/integration_tests/flutter_gallery/lib/gallery/backdrop.dart
Line 260 in 31847bc
flutter/dev/integration_tests/new_gallery/lib/pages/splash.dart
Lines 71 to 72 in 31847bc
flutter/dev/integration_tests/new_gallery/lib/studies/reply/adaptive_nav.dart
Lines 525 to 526 in 31847bc
flutter/dev/integration_tests/new_gallery/lib/studies/shrine/app.dart
Lines 122 to 123 in 31847bc
flutter/dev/integration_tests/new_gallery/lib/studies/shrine/backdrop.dart
Lines 214 to 215 in 31847bc
flutter/dev/integration_tests/new_gallery/lib/studies/shrine/expanding_bottom_sheet.dart
Lines 343 to 344 in 31847bc
flutter/packages/flutter/lib/src/material/navigation_bar.dart
Lines 1309 to 1310 in 31847bc
flutter/packages/flutter/lib/src/material/navigation_drawer.dart
Line 691 in 31847bc
flutter/packages/flutter/lib/src/material/scaffold.dart
Lines 610 to 611 in 31847bc
flutter/packages/flutter/lib/src/material/scaffold.dart
Lines 3206 to 3207 in 31847bc
flutter/packages/flutter/lib/src/widgets/animated_cross_fade.dart
Lines 320 to 321 in 31847bc
flutter/packages/flutter/lib/src/widgets/routes.dart
Lines 1939 to 1940 in 31847bc
Rather than refactoring all of these into switch expressions, I think it'd be best to take advantage of enhanced enum features and improve
AnimationStatus
directly.The text was updated successfully, but these errors were encountered: