Last active
July 23, 2020 21:16
-
-
Save samuelematias/b80a6d995399abee87397b62bddf2df1 to your computer and use it in GitHub Desktop.
Reset your app from everywhere using
This file contains hidden or 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'; | |
class RestartWidget extends StatefulWidget { | |
RestartWidget({this.child}); | |
final Widget child; | |
static void restartApp(BuildContext context) { | |
Future.microtask(() => | |
context.findAncestorStateOfType<_RestartWidgetState>().restartApp()); | |
} | |
@override | |
_RestartWidgetState createState() => _RestartWidgetState(); | |
} | |
class _RestartWidgetState extends State<RestartWidget> { | |
Key key = UniqueKey(); | |
void restartApp() { | |
setState(() { | |
key = UniqueKey(); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return KeyedSubtree( | |
key: key, | |
child: Container( | |
color: Colors.white, | |
child: widget.child, | |
), | |
); | |
} | |
} | |
//you can reset your app from everywhere using: | |
//RestartWidget.restartApp(context). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment