custom_character_another_add_dialog.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import 'package:cached_network_image/cached_network_image.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  5. import 'package:get/get_rx/src/rx_types/rx_types.dart';
  6. import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
  7. import 'package:keyboard/data/bean/keyboard_info.dart';
  8. import 'package:keyboard/data/repository/characters_repository.dart';
  9. import 'package:keyboard/data/repository/keyboard_repository.dart';
  10. import 'package:keyboard/utils/toast_util.dart';
  11. import '../../data/consts/event_report.dart';
  12. import '../../handler/event_handler.dart';
  13. import '../../resource/assets.gen.dart';
  14. import '../../resource/string.gen.dart';
  15. import '../../utils/http_handler.dart';
  16. import '../../utils/styles.dart';
  17. class CustomCharacterAnotherAddDialog {
  18. static const tag = "CustomCharacterAnotherAddDialog";
  19. static RxInt selectedIndex = RxInt(0);
  20. static Rx<KeyboardInfo> selectKeyboard = KeyboardInfo().obs;
  21. static show({required String customCharacterId}) {
  22. SmartDialog.show(
  23. backType: SmartBackType.block,
  24. clickMaskDismiss: true,
  25. alignment: Alignment.bottomCenter,
  26. animationType: SmartAnimationType.centerScale_otherSlide,
  27. tag: tag,
  28. keepSingle: true,
  29. builder:
  30. (_) => Container(
  31. height: 472.h,
  32. decoration: ShapeDecoration(
  33. color: Color(0xFFF6F5FA),
  34. shape: RoundedRectangleBorder(
  35. borderRadius: BorderRadius.only(
  36. topLeft: Radius.circular(18.r),
  37. topRight: Radius.circular(18.r),
  38. ),
  39. ),
  40. ),
  41. child: Column(
  42. children: [
  43. Container(
  44. padding: EdgeInsets.only(
  45. left: 16.w,
  46. right: 16.w,
  47. top: 22.w,
  48. bottom: 20.w,
  49. ),
  50. child: Row(
  51. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  52. children: [
  53. Assets.images.iconCustomCharacterAddDialogTitle.image(
  54. width: 85.w,
  55. height: 25.w,
  56. fit: BoxFit.contain,
  57. ),
  58. GestureDetector(
  59. onTap: () {
  60. SmartDialog.dismiss(tag: tag);
  61. },
  62. child: Assets.images.iconCustomCharacterAddDialogClose
  63. .image(
  64. width: 24.w,
  65. height: 24.w,
  66. fit: BoxFit.contain,
  67. ),
  68. ),
  69. ],
  70. ),
  71. ),
  72. Expanded(
  73. child: Stack(
  74. children: [
  75. ListView.separated(
  76. padding: EdgeInsets.only(bottom: 100.h),
  77. itemCount:
  78. KeyboardRepository.getInstance()
  79. .keyboardInfoList
  80. .length,
  81. itemBuilder: (context, index) {
  82. return _buildListItem(
  83. keyboardInfo:
  84. KeyboardRepository.getInstance()
  85. .keyboardInfoList[index],
  86. index: index,
  87. );
  88. },
  89. separatorBuilder: (BuildContext context, int index) {
  90. if (index ==
  91. KeyboardRepository.getInstance()
  92. .keyboardInfoList
  93. .length -
  94. 1) {
  95. return SizedBox(
  96. width: double.infinity,
  97. height: 50.h, // 最后一个元素后面加50空白
  98. );
  99. } else {
  100. return SizedBox(
  101. width: double.infinity,
  102. height: 10.h,
  103. );
  104. }
  105. },
  106. ),
  107. Positioned(
  108. bottom: 20.h,
  109. left: 16.w,
  110. right: 16.w,
  111. child: InkWell(
  112. onTap: () async {
  113. if (selectKeyboard.value.id == null) {
  114. return ToastUtil.show("请选择键盘");
  115. }
  116. EventHandler.report(EventId.event_12010);
  117. try {
  118. await CharactersRepository.getInstance()
  119. .addCustomCharacter(
  120. characterId: customCharacterId,
  121. keyboardId: selectKeyboard.value.id!,
  122. );
  123. ToastUtil.show("添加成功");
  124. SmartDialog.dismiss(tag: tag);
  125. } catch (error) {
  126. if (error is ServerErrorException) {
  127. ToastUtil.show(error.message);
  128. }
  129. }
  130. },
  131. child: Container(
  132. height: 48.h,
  133. alignment: Alignment.center,
  134. decoration: Styles.getActivateButtonDecoration(
  135. 31.r,
  136. ),
  137. child: Text(
  138. StringName.confirmAdd,
  139. style: Styles.getTextStyleWhiteW500(16.sp),
  140. ),
  141. ),
  142. ),
  143. ),
  144. ],
  145. ),
  146. ),
  147. ],
  148. ),
  149. ),
  150. );
  151. }
  152. static Widget _buildListItem({
  153. required KeyboardInfo keyboardInfo,
  154. required int index,
  155. }) {
  156. return GestureDetector(
  157. onTap: () {
  158. selectKeyboard.value = keyboardInfo;
  159. selectedIndex.value = index;
  160. },
  161. child: Obx(() {
  162. return Container(
  163. margin: EdgeInsets.only(left: 16.w, right: 16.w),
  164. decoration: ShapeDecoration(
  165. color: Colors.white,
  166. shape: RoundedRectangleBorder(
  167. // 只有选中的项目才显示紫色边框
  168. side: BorderSide(
  169. width: selectedIndex.value == index ? 2 : 0,
  170. color:
  171. selectedIndex.value == index
  172. ? const Color(0xFF7D46FC)
  173. : Colors.transparent,
  174. ),
  175. borderRadius: BorderRadius.circular(12.r),
  176. ),
  177. ),
  178. height: 88.h,
  179. padding: EdgeInsets.symmetric(horizontal: 16.w),
  180. child: Row(
  181. children: [
  182. _buildAvatar(imageUrl: keyboardInfo.imageUrl),
  183. SizedBox(width: 8.w),
  184. _buildKeyboardInfo(keyboardInfo),
  185. selectedIndex.value == index
  186. ? Assets.images.iconCustomCharacterAddDialogSelect.image(
  187. width: 26.w,
  188. height: 26.w,
  189. fit: BoxFit.contain,
  190. )
  191. : SizedBox(),
  192. ],
  193. ),
  194. );
  195. }),
  196. );
  197. }
  198. /// 角色头像
  199. static Widget _buildAvatar({required String? imageUrl}) {
  200. return Container(
  201. width: 60.r,
  202. height: 60.r,
  203. decoration: BoxDecoration(
  204. borderRadius: BorderRadius.circular(8.r),
  205. gradient: LinearGradient(
  206. begin: Alignment.topCenter,
  207. end: Alignment.bottomCenter,
  208. colors: [Color(0xffebe6ff), Color(0xffffe6fe)],
  209. ),
  210. ),
  211. child: ClipRRect(
  212. borderRadius: BorderRadius.circular(8.r), // 可调整圆角大小
  213. child: CachedNetworkImage(
  214. imageUrl: imageUrl ?? "",
  215. width: 60.r,
  216. height: 60.r,
  217. fit: BoxFit.contain,
  218. errorWidget:
  219. (_, __, ___) => Assets.images.iconSystemKeyboard.image(
  220. width: 60.r,
  221. height: 60.r,
  222. fit: BoxFit.contain,
  223. ),
  224. ),
  225. ),
  226. );
  227. }
  228. static Widget _buildKeyboardInfo(KeyboardInfo keyboardInfo) {
  229. return Expanded(
  230. child: Column(
  231. mainAxisAlignment: MainAxisAlignment.center,
  232. crossAxisAlignment: CrossAxisAlignment.start,
  233. children: [
  234. Row(
  235. children: [
  236. Text(
  237. keyboardInfo.name ?? "",
  238. style: TextStyle(
  239. color: Colors.black.withAlpha(204),
  240. fontSize: 15.sp,
  241. fontWeight: FontWeight.w500,
  242. ),
  243. ),
  244. SizedBox(width: 4.w),
  245. ],
  246. ),
  247. ],
  248. ),
  249. );
  250. }
  251. }