xxxxxxxxxx
import 'package:flutter/material.dart';
void main() {
runApp(
RestartWidget(
child: MaterialApp(),
),
);
}
class RestartWidget extends StatefulWidget {
RestartWidget({this.child});
final Widget child;
static void restartApp(BuildContext context) {
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: widget.child,
);
}
}
--------------------------------------------
In this example you can reset your app from everywhere using RestartWidget.restartApp(context).
xxxxxxxxxx
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Restart App Programmatically'),
),
body: Center(
child: RaisedButton(
child: Text('Restart App'),
onPressed: () {
restartApp();
},
),
),
),
);
}
}
void restartApp() {
SystemChannels.platform.invokeMethod('SystemNavigator.pop');
}
xxxxxxxxxx
onPressed: () {
/// Fill webOrigin only when your new origin is different than the app's origin
Restart.restartApp(webOrigin: '[your main route]');
}