Add a currentWidget and currentState accessor to GlobalKeys

This commit is contained in:
Adam Barth 2015-09-25 14:12:08 -07:00
parent d717be946a
commit 80997a68d6

View File

@ -92,6 +92,15 @@ abstract class GlobalKey extends Key {
}
}
Element get currentElement => _registry[this];
Widget get currentWidget => currentElement?.widget;
State get currentState {
Element element = currentElement;
if (element is StatefulComponentElement)
return element.state;
return null;
}
static void registerRemoveListener(GlobalKey key, GlobalKeyRemoveListener listener) {
assert(key != null);
Set<GlobalKeyRemoveListener> listeners =
@ -109,11 +118,6 @@ abstract class GlobalKey extends Key {
assert(removed);
}
static Element getElement(GlobalKey key) {
assert(key != null);
return _registry[key];
}
// TODO(ianh): call this
static void _notifyListeners() {
assert(() {