import 'dart:async'; import 'package:flutter/material.dart'; import 'package:zhiying_base_widget/dialog/loading/loading.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; class RestartWidget extends StatefulWidget { RestartWidget({this.child}); final Widget child; static void restartApp(BuildContext context) { //查找顶层_RestartWidgetState并重启 context.findAncestorStateOfType<_RestartWidgetState>().restartApp(); } @override _RestartWidgetState createState() => _RestartWidgetState(); } class _RestartWidgetState extends State { bool reStart = false; // int com = 0; Timer timer1; Timer timer2; StreamController streamController=StreamController(); Stream outData; bool isFirst=true; void restartApp() async { if (reStart) { return; } // com++; ///刷新3次后不再刷新 // if (com > 3) { // if (com < 7) { // Fluttertoast.showToast(msg: "网络服务不可用"); // } // return; // } Loading.show(context, msg: "更新数据中..."); streamController.add(null); timer1 = Timer(Duration(milliseconds: 1000), () async { print("重启"); streamController.add("restart"); }); timer2 = Timer(Duration(milliseconds: 1500), () { Loading.dismiss(); }); } @override void initState() { outData=streamController.stream; super.initState(); } @override Widget build(BuildContext context) { // const bool inProduction = const bool.fromEnvironment("dart.vm.product"); // if (!inProduction) { // ///debug阶段可重启 // isFirst=true; // } return StreamBuilder(stream: outData,builder: (context,asyn){ if(asyn.data==null&&!isFirst){ return Scaffold( body: Container(), ); }else{ isFirst=false; return widget?.child; } }); } @override void dispose() { timer1?.cancel(); timer2?.cancel(); super.dispose(); } }