Skip to content

Commit

Permalink
Update useSingleTickerProvider based on SingleTickerProviderStateMixin (
Browse files Browse the repository at this point in the history
#432)

Co-authored-by: Remi Rousselet <[email protected]>
  • Loading branch information
dev-tatsuya and rrousselGit authored Jul 8, 2024
1 parent 3cadf75 commit b4ed644
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions packages/flutter_hooks/lib/src/animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class _TickerProviderHookState
extends HookState<TickerProvider, _SingleTickerProviderHook>
implements TickerProvider {
Ticker? _ticker;
ValueListenable<bool>? _tickerModeNotifier;

@override
Ticker createTicker(TickerCallback onTick) {
Expand All @@ -184,7 +185,10 @@ class _TickerProviderHookState
'If you need multiple Ticker, consider using useSingleTickerProvider multiple times '
'to create as many Tickers as needed.');
}(), '');
return _ticker = Ticker(onTick, debugLabel: 'created by $context');
_ticker = Ticker(onTick, debugLabel: 'created by $context');
_updateTickerModeNotifier();
_updateTicker();
return _ticker!;
}

@override
Expand All @@ -199,14 +203,32 @@ class _TickerProviderHookState
' by AnimationControllers should be disposed by calling dispose() on '
' the AnimationController itself. Otherwise, the ticker will leak.\n');
}(), '');
_tickerModeNotifier?.removeListener(_updateTicker);
_tickerModeNotifier = null;
super.dispose();
}

@override
TickerProvider build(BuildContext context) {
_updateTickerModeNotifier();
_updateTicker();
return this;
}

void _updateTicker() {
if (_ticker != null) {
_ticker!.muted = !TickerMode.of(context);
_ticker!.muted = !_tickerModeNotifier!.value;
}
return this;
}

void _updateTickerModeNotifier() {
final newNotifier = TickerMode.getNotifier(context);
if (newNotifier == _tickerModeNotifier) {
return;
}
_tickerModeNotifier?.removeListener(_updateTicker);
newNotifier.addListener(_updateTicker);
_tickerModeNotifier = newNotifier;
}

@override
Expand Down

0 comments on commit b4ed644

Please sign in to comment.