基础组件库
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.
 
 
 
 
 

96 rivejä
2.0 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. // const bool inProduction = const bool.fromEnvironment("dart.vm.product");
  53. // if (!inProduction) {
  54. // ///debug阶段可重启
  55. // isFirst=true;
  56. // }
  57. return StreamBuilder(stream: outData,builder: (context,asyn){
  58. if(asyn.data==null&&!isFirst){
  59. return Scaffold(
  60. body: Container(),
  61. );
  62. }else{
  63. isFirst=false;
  64. return widget?.child;
  65. }
  66. });
  67. }
  68. @override
  69. void dispose() {
  70. timer1?.cancel();
  71. timer2?.cancel();
  72. super.dispose();
  73. }
  74. }