基础组件库
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

98 rader
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. isFirst = true;
  58. return StreamBuilder(stream: outData,builder: (context,asyn){
  59. if(asyn.data==null&&!isFirst){
  60. return Scaffold(
  61. body: Container(),
  62. );
  63. }else{
  64. isFirst=false;
  65. return widget?.child;
  66. }
  67. // return widget?.child;
  68. });
  69. }
  70. @override
  71. void dispose() {
  72. timer1?.cancel();
  73. timer2?.cancel();
  74. super.dispose();
  75. }
  76. }