Created
June 21, 2019 07:22
-
-
Save creativecreatorormaybenot/6758c92ea0b83e931aef3ee3a51c8bba to your computer and use it in GitHub Desktop.
Hero calls initState three times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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