Skip to content

Instantly share code, notes, and snippets.

@creativecreatorormaybenot
Last active July 3, 2019 22:51
Show Gist options
  • Save creativecreatorormaybenot/505fb58446e41fac140c459039d7029a to your computer and use it in GitHub Desktop.
Save creativecreatorormaybenot/505fb58446e41fac140c459039d7029a to your computer and use it in GitHub Desktop.
Hero inserts after dispose
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(routes: {
'/': (context) => Scaffold(
body: Center(
child: FlatButton(
child: const Text('open'),
onPressed: () => Navigator.of(context).pushNamed('/page'),
),
)),
'/page': (context) => Page()
}, initialRoute: '/'));
}
class Page extends StatefulWidget {
Page({Key key}) : super(key: key);
@override
_PageState createState() => _PageState();
}
class _PageState extends State<Page> {
ValueNotifier notifier;
// FixedValueNotifier notifier;
@override
void initState() {
notifier = ValueNotifier(null);
// notifier = FixedValueNotifier(null);
super.initState();
}
@override
void dispose() {
notifier.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) => Scaffold(
body: Column(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
FlatButton(
child: const Text('push'),
onPressed: () => Navigator.of(context).pushNamed('/page'),
),
FlatButton(
child: const Text('popUntil'),
onPressed: () => Navigator.of(context).popUntil(ModalRoute.withName('/')),
),
Hero(
tag: 'tag',
child: AnimatedBuilder(
animation: notifier,
builder: (context, child) => Container(),
))
]));
}
class FixedValueNotifier<T> extends ValueNotifier<T> {
bool disposed;
FixedValueNotifier(value)
: disposed = false,
super(value);
@override
void dispose() {
disposed = true;
super.dispose();
}
@override
void removeListener(listener) {
if (disposed) return;
super.removeListener(listener);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment