Skip to content

Instantly share code, notes, and snippets.

@creativecreatorormaybenot
Created June 21, 2019 07:22
Show Gist options
  • Save creativecreatorormaybenot/6758c92ea0b83e931aef3ee3a51c8bba to your computer and use it in GitHub Desktop.
Save creativecreatorormaybenot/6758c92ea0b83e931aef3ee3a51c8bba to your computer and use it in GitHub Desktop.
Hero calls initState three times
import 'package:flutter/material.dart';
main() {
runApp(MaterialApp(home: const Page()));
}
class Page extends StatelessWidget {
const Page({Key key}) : super(key: key);
@override
Widget build(BuildContext context) => WillPopScope(
onWillPop: () async {
initStateCalled = 0;
return true;
},
child: Scaffold(
body: Stack(children: [
Hero(tag: 'tag', child: HeroContent()),
Center(
child: FlatButton(
child: const Text('navigate'),
onPressed: () {
initStateCalled = 0;
Navigator.of(context).push(MaterialPageRoute(builder: (_) => const Page()));
},
),
),
Align(
alignment: Alignment.bottomCenter,
child: FlatButton(
child: const Text('back'),
onPressed: () {
Navigator.of(context).maybePop();
},
),
)
])),
);
}
int initStateCalled = 0;
class HeroContent extends StatefulWidget {
HeroContent({Key key}) : super(key: key);
@override
createState() => _HeroContentState();
}
class _HeroContentState extends State<HeroContent> {
@override
void initState() {
print('initState called: ${++initStateCalled}');
super.initState();
}
@override
Widget build(BuildContext context) => Container();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment