custom_character_another_add_dialog.dart 9.0 KB

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