dialog_base.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'package:trackoffical_app/logger.dart';
  5. import 'package:trackoffical_app/screen.dart';
  6. import '../../../generated/assets.dart';
  7. class DialogBase extends DialogBaseOffCount {
  8. const DialogBase({super.key, Duration? offAfter, required Widget child}) : super(
  9. offAfter: offAfter, child: child
  10. );
  11. @override
  12. State<StatefulWidget> createState() {
  13. return DialogBaseState();
  14. }
  15. }
  16. class DialogBaseState extends DialogBaseOffCountState<DialogBase> {
  17. @override
  18. Widget build(BuildContext context) {
  19. final style = (context.textTheme.titleLarge ?? const TextStyle())
  20. .copyWith(color: const Color(0xff333333), fontSize: 41.98.rpx);
  21. final children = <Widget>[
  22. Container(
  23. margin: EdgeInsets.only(left: 47.71.rpx, right: 47.71.rpx),
  24. padding:
  25. EdgeInsets.fromLTRB(5.85.wp, 9.67.wp, 5.85.wp, 9.67.wp),
  26. decoration: BoxDecoration(
  27. color: const Color(0xe6ffffff),
  28. borderRadius: BorderRadius.circular(17.18.rpx)),
  29. child: widget.child)
  30. ];
  31. if (widget.offAfter != null) {
  32. children.addAll([
  33. Obx(() => offCount(secondCount.value))
  34. ]);
  35. }
  36. return DefaultTextStyle(
  37. style: style,
  38. child: Container(
  39. width: context.width,
  40. height: context.height,
  41. color: const Color(0xB8000000),
  42. alignment: Alignment.center,
  43. child: Center(
  44. child: Column(
  45. mainAxisSize: MainAxisSize.min,
  46. children: children,
  47. ),
  48. ),
  49. ));
  50. }
  51. }
  52. abstract class DialogBaseOffCount extends StatefulWidget{
  53. const DialogBaseOffCount({
  54. super.key,
  55. this.offAfter,
  56. required this.child});
  57. final Duration? offAfter;
  58. final Widget child;
  59. }
  60. abstract class DialogBaseOffCountState <T extends DialogBaseOffCount> extends State<T>{
  61. final Rx<int?> secondCount = Rx(null);
  62. Timer? _timer;
  63. DateTime _createAt = DateTime.now();
  64. var isActive = true;
  65. void onStop(){
  66. if(mounted){
  67. debug('back: ${widget.runtimeType}');
  68. Get.back();
  69. }
  70. }
  71. @override
  72. void initState() {
  73. super.initState();
  74. final offAfter = widget.offAfter;
  75. _createAt = DateTime.now();
  76. if (offAfter != null) {
  77. _timer = Timer.periodic(100.milliseconds, (timer) {
  78. final d = DateTime.now().difference(_createAt);
  79. secondCount.value =offAfter.inSeconds - (d.inMilliseconds / 1000).floor();
  80. if (d >= offAfter) {
  81. onStop();
  82. }
  83. });
  84. }
  85. }
  86. @override
  87. void dispose() {
  88. super.dispose();
  89. debug('${widget.runtimeType} dispose()');
  90. isActive = false;
  91. _timer?.cancel();
  92. }
  93. }
  94. Widget offCount(int? secondCount){
  95. return Column(
  96. mainAxisSize: MainAxisSize.min,
  97. children: [
  98. SizedBox(height: 50.0.rpx),
  99. Text(
  100. secondCount?.toString() ?? '',
  101. style: TextStyle(fontSize: 150.0.rpx, color: const Color(0xffff870d)),
  102. )
  103. ],
  104. );
  105. }
  106. Widget dialogTitle(String title, Color titleColor, Widget content,
  107. {Duration? offAfter}) {
  108. return DialogBase(
  109. offAfter: offAfter,
  110. child: Column(
  111. mainAxisSize: MainAxisSize.min,
  112. children: [
  113. Text(title, style: TextStyle(color: titleColor, fontSize: 55.07.rpx, fontWeight: FontWeight.w700)),
  114. SizedBox(height: 32.1.rpx),
  115. content
  116. ],
  117. ),
  118. ) ;
  119. }
  120. Widget bean(int count, double fontSize) {
  121. return Row(
  122. mainAxisSize: MainAxisSize.min,
  123. crossAxisAlignment: CrossAxisAlignment.end,
  124. children: [
  125. Image.asset(Assets.imagesIcBean, height: fontSize * 2.2, ),
  126. Text(
  127. ' X $count',
  128. style: TextStyle(color: const Color(0xffaaaaaa), fontSize: fontSize),
  129. )
  130. ],
  131. );
  132. }