show_update_version_dialog.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import 'dart:io';
  2. import 'dart:ui';
  3. import 'package:electronic_assistant/data/bean/version_update_bean.dart';
  4. import 'package:electronic_assistant/data/consts/constants.dart';
  5. import 'package:electronic_assistant/resource/assets.gen.dart';
  6. import 'package:electronic_assistant/resource/colors.gen.dart';
  7. import 'package:electronic_assistant/utils/common_style.dart';
  8. import 'package:electronic_assistant/utils/expand.dart';
  9. import 'package:electronic_assistant/utils/mmkv_util.dart';
  10. import 'package:electronic_assistant/utils/toast_util.dart';
  11. import 'package:flutter/cupertino.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:flutter_screenutil/flutter_screenutil.dart';
  14. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  15. import '../utils/launcher_url_util.dart';
  16. class UpdateVersionDialog {
  17. static const String tag = 'showUpdateVersionDialog';
  18. static void dismiss() {
  19. SmartDialog.dismiss(tag: tag);
  20. }
  21. static Widget _buildVersionView(String version) {
  22. return Container(
  23. decoration: BoxDecoration(
  24. gradient: LinearGradient(
  25. colors: [
  26. '#D0C5FF'.toColor(),
  27. ColorName.white,
  28. '#647FFF'.toColor()
  29. ],
  30. begin: Alignment.topLeft,
  31. end: Alignment.bottomRight,
  32. ),
  33. borderRadius: BorderRadius.all(Radius.circular(11.w))),
  34. padding: EdgeInsets.all(0.5.w),
  35. height: 22.h,
  36. child: Container(
  37. decoration: getPrimaryBtnDecoration(11.w),
  38. padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 2.w),
  39. child: Text(
  40. version,
  41. style: TextStyle(
  42. fontSize: 13.sp,
  43. color: ColorName.white,
  44. fontWeight: FontWeight.bold,
  45. height: 0),
  46. ),
  47. ));
  48. }
  49. static Widget _buildUpdateBtn({VoidCallback? onUpdateClick}) {
  50. return GestureDetector(
  51. onTap: onUpdateClick,
  52. child: Container(
  53. width: 148.w,
  54. height: 48.w,
  55. decoration: BoxDecoration(
  56. gradient: LinearGradient(
  57. colors: ['#25262A'.toColor(), '#3F424D'.toColor()],
  58. stops: const [0.3, 1.0],
  59. begin: Alignment.topLeft,
  60. end: Alignment.bottomRight,
  61. ),
  62. borderRadius: BorderRadius.circular(100.w),
  63. ),
  64. child: Center(
  65. child: Text(
  66. '立即更新',
  67. style: TextStyle(
  68. fontSize: 14.sp,
  69. color: ColorName.white,
  70. fontWeight: FontWeight.bold),
  71. ),
  72. )),
  73. );
  74. }
  75. static void show(VersionUpdateBean bean) {
  76. if (SmartDialog.checkExist(tag: tag)) {
  77. return;
  78. }
  79. SmartDialog.show(
  80. tag: tag,
  81. backType: SmartBackType.block,
  82. clickMaskDismiss: false,
  83. maskColor: ColorName.black55,
  84. builder: (_) {
  85. return SizedBox(
  86. width: 304.w,
  87. child: IntrinsicHeight(
  88. child: Column(
  89. children: [
  90. Container(
  91. width: 304.w,
  92. decoration: BoxDecoration(
  93. image: DecorationImage(
  94. image: Assets.images.bgUpdateVersion.provider(),
  95. fit: BoxFit.cover,
  96. ),
  97. ),
  98. child: AspectRatio(
  99. aspectRatio: 960 / 1110,
  100. child: Container(
  101. padding: EdgeInsets.symmetric(horizontal: 42.w),
  102. child: Column(
  103. crossAxisAlignment: CrossAxisAlignment.start,
  104. children: [
  105. SizedBox(height: 57.h),
  106. Assets.images.iconUpdateNewVersionTitle
  107. .image(height: 29.h),
  108. SizedBox(height: 8.h),
  109. _buildVersionView(bean.version),
  110. SizedBox(height: 20.h),
  111. Expanded(
  112. child:
  113. _buildUpdateView(bean.title, bean.desc)),
  114. SizedBox(height: 20.h),
  115. _buildUpdateBtn(onUpdateClick: () {
  116. onUpdate(bean.url);
  117. }),
  118. SizedBox(height: 48.h),
  119. ],
  120. ),
  121. )),
  122. ),
  123. SizedBox(height: 12.h),
  124. Visibility(
  125. visible: !bean.force,
  126. child: GestureDetector(
  127. onTap: () {
  128. KVUtil.putString(
  129. Constants.recordServerVersion, bean.version);
  130. SmartDialog.dismiss(tag: tag);
  131. },
  132. child: Assets.images.iconUpdateAppClose
  133. .image(width: 40.w, height: 40.w)),
  134. )
  135. ],
  136. ),
  137. ),
  138. );
  139. });
  140. }
  141. static Widget _buildUpdateView(String? title, String? desc) {
  142. return SingleChildScrollView(
  143. child: Column(
  144. crossAxisAlignment: CrossAxisAlignment.start,
  145. children: [
  146. Text(
  147. title ?? '',
  148. style: TextStyle(fontSize: 14.sp, color: '#202A4D'.toColor()),
  149. ),
  150. Visibility(
  151. visible: title != null && title.isNotEmpty,
  152. child: SizedBox(
  153. height: 6.h,
  154. )),
  155. Text(
  156. desc ?? '',
  157. style: TextStyle(fontSize: 14.sp, color: '#202A4D'.toColor()),
  158. ),
  159. ],
  160. ),
  161. );
  162. }
  163. static void onUpdate(String? url) {
  164. if (Platform.isAndroid) {
  165. if (url == null) {
  166. return;
  167. }
  168. try {
  169. LauncherUrlUtil.launchHttpUrl(url);
  170. } catch (e) {
  171. ToastUtil.showToast('更新失败');
  172. }
  173. } else if (Platform.isIOS) {
  174. // ios 跳转苹果商店
  175. LauncherUrlUtil.launchHttpUrl(
  176. 'itms-apps://itunes.apple.com/us/app/apple-store/6683306440');
  177. }
  178. }
  179. }