|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- 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<RestartWidget> {
- 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;
- // }
- isFirst = true;
- return StreamBuilder(stream: outData,builder: (context,asyn){
- if(asyn.data==null&&!isFirst){
- return Scaffold(
- body: Container(),
- );
- }else{
- isFirst=false;
- return widget?.child;
- }
- // return widget?.child;
- });
-
- }
-
- @override
- void dispose() {
- timer1?.cancel();
- timer2?.cancel();
- super.dispose();
- }
- }
|