基础组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

92 lines
1.8 KiB

  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:zhiying_base_widget/dialog/loading/loading.dart';
  4. import 'package:zhiying_comm/zhiying_comm.dart';
  5. class RestartWidget extends StatefulWidget {
  6. RestartWidget({this.child});
  7. final Widget child;
  8. static void restartApp(BuildContext context) {
  9. //查找顶层_RestartWidgetState并重启
  10. context.findAncestorStateOfType<_RestartWidgetState>().restartApp();
  11. }
  12. @override
  13. _RestartWidgetState createState() => _RestartWidgetState();
  14. }
  15. class _RestartWidgetState extends State<RestartWidget> {
  16. bool reStart = false;
  17. int com = 0;
  18. Timer timer1;
  19. Timer timer2;
  20. StreamController streamController=StreamController();
  21. Stream outData;
  22. bool isFirst=true;
  23. void restartApp() async {
  24. if (reStart) {
  25. return;
  26. }
  27. com++;
  28. ///刷新3次后不再刷新
  29. if (com > 3) {
  30. if (com < 7) {
  31. Fluttertoast.showToast(msg: "网络服务不可用");
  32. }
  33. return;
  34. }
  35. Loading.show(context, msg: "更新数据中...");
  36. streamController.add(null);
  37. timer1 = Timer(Duration(milliseconds: 1000), () async {
  38. print("重启");
  39. streamController.add("restart");
  40. });
  41. timer2 = Timer(Duration(milliseconds: 1500), () {
  42. Loading.dismiss();
  43. });
  44. }
  45. @override
  46. void initState() {
  47. outData=streamController.stream;
  48. super.initState();
  49. }
  50. @override
  51. Widget build(BuildContext context) {
  52. return StreamBuilder(stream: outData,builder: (context,asyn){
  53. if(asyn.data==null&&!isFirst){
  54. return Scaffold(
  55. body: Container(),
  56. );
  57. }else{
  58. isFirst=false;
  59. return widget?.child;
  60. }
  61. });
  62. }
  63. @override
  64. void dispose() {
  65. timer1?.cancel();
  66. timer2?.cancel();
  67. super.dispose();
  68. }
  69. }