Skip to content

Instantly share code, notes, and snippets.

@creativecreatorormaybenot
Last active July 26, 2019 04:54
Show Gist options
  • Save creativecreatorormaybenot/0b8d7710a34a394b7e226813a40b77ba to your computer and use it in GitHub Desktop.
Save creativecreatorormaybenot/0b8d7710a34a394b7e226813a40b77ba to your computer and use it in GitHub Desktop.
If you want your State.didChangeDependencies method to be called when a new route is pushed or popped above, just use this mixin.
import 'package:flutter/material.dart';
/// When using this mixin by appending `with NavigationNotifierMixin` to
/// your [State], your [State.didChangeDependencies] method will be called
/// every time the [Navigator] pushes a route or pops any route above the
/// route your [StatefulWidget] lives in.
mixin NavigationNotifierMixin<T extends StatefulWidget> on State<T> {
@override
void didChangeDependencies() {
// The TickerMode InheritedWidget changes when navigating,
// causing didChangeDependencies of your State to be called.
TickerMode.of(context);
// This happens because you called inheritFromWidgetOfExactType
// on the BuildContext in here, which causes it to notify you
// when that InheritWidget changes.
super.didChangeDependencies();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment