Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[framework] create animation from value listenable #108644

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

Merged

Conversation

jonahwilliams
Copy link
Member

I've noticed a number of patterns in a certain customer app, where there is a desire to animate some part of the UI (opacity for example) based on something that is available via a value listenable (scroll position). Because there is no easy way to create an animation from a value listenable, I see a lot of ValueListenableBuilders used to animate things that could be FadeTransitions. This has the distinct disadvantage of bypassing the repaint boundary mechanisms introduced into animated opacities.

@flutter-dashboard flutter-dashboard bot added a: animation Animation APIs framework flutter/packages/flutter repository. See also f: labels. labels Jul 29, 2022
@jonahwilliams jonahwilliams requested a review from goderbauer July 29, 2022 21:45
@jonahwilliams
Copy link
Member Author

The idea with this is that instead of:

ValueListenableBuilder(
  listenable: scrollPos.
  builder: (_, value, child) {
    var opacity = computeOpacity(value);
    return Opacity(child: child, opacity: opacity);
  }
)

You can do:

FadeTransition(child: child, opacity: Animation.fromValueListenable(scrollPos, transformer: computeOpacity)

Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

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

LGTM

Seems like a useful API.

import 'package:flutter_test/flutter_test.dart';

void main() {

Copy link
Member

Choose a reason for hiding this comment

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

nit: remove blank line?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

///
/// The returned animation will always have an animations status of
/// [AnimationStatus.forward]. The value of the provided listenable can
/// be optionally transformed using the [transformer] function.
Copy link
Member

Choose a reason for hiding this comment

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

Bonus points if you add the example from this comment here to showcase the API and how it can simplify things.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

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

Here are the bonus points: 🌟🌟🌟🌟🌟

/// },
/// child: Container(
/// child: Text('Hello, Animation'),
/// ),
Copy link
Member

Choose a reason for hiding this comment

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

nit: the analyzer will hopefully tell you, but I think this is missing a closing ) for the ValueListenableBuilder?

/// })
/// child: Container(
/// child: Text('Hello, Animation'),
/// ),
Copy link
Member

Choose a reason for hiding this comment

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

same.

@jonahwilliams jonahwilliams added the autosubmit Merge PR when tree becomes green via auto submit App label Jul 30, 2022
@auto-submit auto-submit bot merged commit 40940de into flutter:master Jul 30, 2022
@jonahwilliams jonahwilliams deleted the make_animation_from_listener branch July 30, 2022 00:41
engine-flutter-autoroll added a commit to engine-flutter-autoroll/plugins that referenced this pull request Jul 30, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 30, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/plugins that referenced this pull request Jul 30, 2022
camsim99 pushed a commit to camsim99/flutter that referenced this pull request Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: animation Animation APIs autosubmit Merge PR when tree becomes green via auto submit App framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants