0

我正在使用this SO answer中的技术来“重新启动”我的应用程序。问题是我的“主页”页面MaterialApp没有被重新创建(它initState没有在重新启动时执行)。

为了实现这一点,我必须在我的应用程序状态中简要返回MaterialAppfrom方法以外的其他内容。build

class _RestartWidgetState extends State<RestartWidget> {
  Key key = new UniqueKey();

  static bool isRestarting = false;

  void restartApp() {
    this.setState(() {
      isRestarting = true;
      key = new UniqueKey();
    });

    Timer(Duration(milliseconds: 500), (){
      this.setState(() {
        isRestarting = false;
        key = new UniqueKey();
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
      key: key,
      child: widget.child,
    );
  }
}


class MyAppState extends State<MyApp>{

  @override
  Widget build(BuildContext context) {
    if( _RestartWidgetState.isRestarting ) {
      return Container( ... );
    }
    else{
      return ScopedModel<MyModel>(
        model: myModel,
        child: MaterialApp(
          home: MainScreen(),

          ...

        )
      );
    }
  }
}

有另一种/更好的方法吗?感觉有点粗略。

4

0 回答 0