user_avatar_view.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import 'package:cached_network_image/cached_network_image.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/src/widgets/framework.dart';
  4. import 'package:flutter_screenutil/flutter_screenutil.dart';
  5. import 'package:get/get.dart';
  6. import 'package:get/get_core/src/get_main.dart';
  7. import 'package:location/data/repositories/config_repository.dart';
  8. import 'package:location/module/avatar/user_avatar_controller.dart';
  9. import 'package:location/resource/assets.gen.dart';
  10. import 'package:location/resource/string.gen.dart';
  11. import 'package:location/utils/common_expand.dart';
  12. import '../../handler/error_handler.dart';
  13. import '../../resource/colors.gen.dart';
  14. import '../../utils/common_style.dart';
  15. class UserAvatarView extends GetView<UserAvatarController> {
  16. UserAvatarView({super.key, required List<String> avatarList}) {
  17. Get.lazyPut(() => UserAvatarController(avatarList));
  18. }
  19. static void show() {
  20. ConfigRepository.getInstance().requestUserAvatarList().then((list) {
  21. Get.bottomSheet(UserAvatarView(avatarList: list),
  22. isScrollControlled: true,
  23. barrierColor: ColorName.black55,
  24. backgroundColor: ColorName.transparent);
  25. }).catchError((error) {
  26. ErrorHandler.toastError(error);
  27. });
  28. }
  29. @override
  30. Widget build(BuildContext context) {
  31. return Container(
  32. width: 1.sw,
  33. decoration: BoxDecoration(
  34. color: Colors.white,
  35. borderRadius: BorderRadius.only(
  36. topLeft: Radius.circular(10.w),
  37. topRight: Radius.circular(10.w),
  38. ),
  39. ),
  40. child: IntrinsicHeight(
  41. child: Column(
  42. children: [
  43. SizedBox(height: 12.w),
  44. buildTitleView(),
  45. SizedBox(height: 22.w),
  46. buildAvatarListView(),
  47. SizedBox(height: 30.w),
  48. buildSureBtnView(),
  49. SizedBox(height: 20.w),
  50. ],
  51. ),
  52. ),
  53. );
  54. }
  55. Widget buildTitleView() {
  56. return Row(
  57. children: [
  58. SizedBox(width: 37.w),
  59. Spacer(),
  60. Text(
  61. StringName.accountPleaseSelectAvatar,
  62. style: TextStyle(fontSize: 14.sp, color: '#404040'.color),
  63. ),
  64. Spacer(),
  65. Assets.images.iconAvatarClose.image(width: 25.w, height: 25.w),
  66. SizedBox(width: 12.w),
  67. ],
  68. );
  69. }
  70. Widget buildAvatarListView() {
  71. return Container(
  72. margin: EdgeInsets.symmetric(horizontal: 20.w),
  73. child: Builder(builder: (context) {
  74. return Wrap(
  75. spacing: 25.w,
  76. runSpacing: 30.w,
  77. children: controller.avatarList.map((url) {
  78. double itemWidth =
  79. (MediaQuery.of(context).size.width - 25.w * 5) / 4;
  80. return GestureDetector(
  81. onTap: () {
  82. controller.onSelectAvatarClick(url);
  83. },
  84. child: buildAvatarItem(itemWidth, url),
  85. );
  86. }).toList(),
  87. );
  88. }));
  89. }
  90. Widget buildAvatarItem(double itemWidth, String url) {
  91. return Stack(
  92. children: [
  93. Container(
  94. width: itemWidth,
  95. height: itemWidth,
  96. decoration: BoxDecoration(
  97. shape: BoxShape.circle,
  98. border: Border.all(width: 1.w, color: '#F0F0F0'.color)),
  99. child: ClipOval(
  100. child: CachedNetworkImage(
  101. width: double.infinity,
  102. height: double.infinity,
  103. imageUrl: url,
  104. fit: BoxFit.cover,
  105. ),
  106. ),
  107. ),
  108. Obx(() {
  109. return Visibility(
  110. visible: controller.selectedAvatar == url ||
  111. (controller.selectedAvatar == null &&
  112. controller.mineInfo?.avatar == url),
  113. child: Container(
  114. width: itemWidth,
  115. height: itemWidth,
  116. decoration: BoxDecoration(
  117. shape: BoxShape.circle,
  118. border:
  119. Border.all(width: 3.w, color: ColorName.colorPrimary)),
  120. ),
  121. );
  122. }),
  123. Obx(() {
  124. return Visibility(
  125. visible: controller.selectedAvatar == url ||
  126. (controller.selectedAvatar == null &&
  127. controller.mineInfo?.avatar == url),
  128. child: Positioned(
  129. bottom: 0,
  130. right: 0,
  131. child: Assets.images.iconAvatarSelected
  132. .image(width: 20.w, height: 20.w)),
  133. );
  134. })
  135. ],
  136. );
  137. }
  138. Widget buildSureBtnView() {
  139. return GestureDetector(
  140. onTap: controller.onSelectedClick,
  141. child: Container(
  142. width: 312.w,
  143. height: 44.w,
  144. decoration: getPrimaryBtnDecoration(100.r),
  145. child: Center(
  146. child: Text(StringName.accountSelectAvatarBtnTxt,
  147. style: TextStyle(fontSize: 15.sp, color: Colors.white))),
  148. ),
  149. );
  150. }
  151. }