Skip to content

Instantly share code, notes, and snippets.

@samuelematias
Last active July 23, 2020 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelematias/b80a6d995399abee87397b62bddf2df1 to your computer and use it in GitHub Desktop.
Save samuelematias/b80a6d995399abee87397b62bddf2df1 to your computer and use it in GitHub Desktop.
Reset your app from everywhere using
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