有些场景需要app重启才生效的,那么flutter如何重启呢?
接下来就看下功能代码吧
import 'package:flutter/material.dart';
void main() {
runApp(new RestartWidget(
child: // new MaterialApp,
));
}
class RestartWidget extends StatefulWidget {
final Widget child;
RestartWidget({this.child});
static restartApp(BuildContext context) {
final _RestartWidgetState state =
context.ancestorStateOfType(const TypeMatcher<_RestartWidgetState>());
state.restartApp();
}
@override
_RestartWidgetState createState() => new _RestartWidgetState();
}
class _RestartWidgetState extends State<RestartWidget> {
Key key = new UniqueKey();
void restartApp() {
this.setState(() {
key = new UniqueKey();
});
}
@override
Widget build(BuildContext context) {
return new Container(
key: key,
child: widget.child,
);
}
}
然后需要重启是调用以下代码
RestartWidget.restartApp(context);
这样就可以重启App了
转载地址()
发现真的得好好学习下英语了,好多功能,问题,国内都找不到,哈哈激励下自己,不断进步