Skip to content

[Docs] Clarify performacne of SingleTickerProviderStateMixin vs TickerProviderStateMixin #164869

@yakagami

Description

@yakagami

The current wording on the TickerProviderStateMixin docs just says that

If you only have a single Ticker (for example only a single AnimationController) for the lifetime of your State, then using a SingleTickerProviderStateMixin is more efficient. This is the common case.

It is not clear how large this performance cost is, and I assumed that using one TickerProviderStateMixin three times is more expensive than using threeSingleTickerProviderStateMixins once. Based on a discussion on the Flutter Discord with chunhtai, that may not be the case. I had changed my code to avoid using TickerProviderStateMixin like so:

    _animationController = AnimationController.unbounded(vsync: this)
      ..addListener(() {
        _updateScale(_animationController.value);
      });
   
    //could have just set `vsync` to `this` if using `TickerProviderStateMixin`
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _verticalAnimationController =
          AnimationController.unbounded(vsync: _verticalController.position.context.vsync)
            ..addListener(() {
              _verticalController.jumpTo(_verticalAnimationController.value);
            });

      _horizontalAnimationController =
          AnimationController.unbounded(vsync: _horizontalController.position.context.vsync)
            ..addListener(() {
              _horizontalController.jumpTo(_horizontalAnimationController.value);
            });
    });

Instead of just:

    _animationController = AnimationController.unbounded(vsync: this)
      ..addListener(() {
        _updateScale(_animationController.value);
      });

    _verticalAnimationController = AnimationController.unbounded(vsync: this)
      ..addListener(() {
        _verticalController.jumpTo(_verticalAnimationController.value);
      });

    _horizontalAnimationController = AnimationController.unbounded(vsync: this)
      ..addListener(() {
        _horizontalController.jumpTo(_horizontalAnimationController.value);
      });

If the above has less or equal performance than using TickerProviderStateMixin, then that could be made more clear in the docs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    d: api docsIssues with https://api.flutter.dev/frameworkflutter/packages/flutter repository. See also f: labels.r: fixedIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions