UserProfileCard.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'dart:ui';
  4. import 'package:dio/dio.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:image_cropper/image_cropper.dart';
  7. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  8. import '../../basic/Common.dart';
  9. import '../../basic/Entities.dart';
  10. import '../../basic/Method.dart';
  11. import 'Avatar.dart';
  12. import 'Images.dart';
  13. import 'ItemBuilder.dart';
  14. const double _cardHeight = 180;
  15. // 用户信息卡
  16. class UserProfileCard extends StatefulWidget {
  17. const UserProfileCard({Key? key, required this.load}) : super(key: key);
  18. final Future<Profile?> Function() load;
  19. @override
  20. State<StatefulWidget> createState() => _UserProfileCardState();
  21. }
  22. class _UserProfileCardState extends State<UserProfileCard> {
  23. late Future<Profile?> _future = widget.load();
  24. // Future<UserProfile?> _load() async {
  25. // var profile = await method.getProfile(widget.name);
  26. // return profile;
  27. // }
  28. @override
  29. Widget build(BuildContext context) {
  30. var nameStyle = const TextStyle(
  31. fontSize: 14,
  32. fontWeight: FontWeight.bold,
  33. );
  34. var nameStrutStyle = const StrutStyle(
  35. fontSize: 14,
  36. forceStrutHeight: true,
  37. fontWeight: FontWeight.bold,
  38. );
  39. return ItemBuilder(
  40. future: _future,
  41. onRefresh: () async {
  42. setState(() => _future = widget.load());
  43. },
  44. height: _cardHeight,
  45. successBuilder:
  46. (BuildContext context, AsyncSnapshot<Profile?> snapshot) {
  47. Profile profile = snapshot.data!;
  48. return Stack(
  49. children: [
  50. SizedBox(
  51. height: _cardHeight,
  52. child: Column(
  53. children: [
  54. Expanded(child: Container()),
  55. GestureDetector(
  56. onTap: () async {
  57. if (Platform.isAndroid || Platform.isIOS) {
  58. await _updateAvatarPhone();
  59. }
  60. },
  61. child: Avatar(profile.avatar, size: 65),
  62. ),
  63. Container(height: 5),
  64. Text(
  65. profile.name,
  66. style: nameStyle,
  67. strutStyle: nameStrutStyle,
  68. ),
  69. Container(height: 8),
  70. Expanded(child: Container()),
  71. ],
  72. ),
  73. )
  74. ],
  75. );
  76. },
  77. );
  78. }
  79. void _reload() {
  80. setState(() {
  81. _future = widget.load();
  82. });
  83. }
  84. Future _updateAvatarPhone() async {
  85. final assets = await AssetPicker.pickAssets(context,
  86. pickerConfig: const AssetPickerConfig(maxAssets: 1));
  87. if (assets == null || assets.isEmpty) return;
  88. final image = await assets.first.file;
  89. // final ImagePicker _picker = ImagePicker();
  90. // final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
  91. if (image != null) {
  92. final cropper = ImageCropper();
  93. final croppedFile = await cropper.cropImage(
  94. sourcePath: image.path,
  95. aspectRatioPresets: [
  96. CropAspectRatioPreset.square,
  97. ],
  98. aspectRatio: const CropAspectRatio(ratioX: 200, ratioY: 200),
  99. maxWidth: 200,
  100. maxHeight: 200,
  101. uiSettings: [AndroidUiSettings(
  102. toolbarTitle: "修改头像",
  103. toolbarColor: Colors.blue,
  104. toolbarWidgetColor: Colors.white,
  105. initAspectRatio: CropAspectRatioPreset.original,
  106. lockAspectRatio: true,
  107. ),
  108. IOSUiSettings(
  109. resetAspectRatioEnabled: true,
  110. aspectRatioLockEnabled: true,
  111. title: "修改头像",
  112. ),]
  113. );
  114. if (croppedFile != null) {
  115. final path = image.path;
  116. final suf = path.substring(path.lastIndexOf('.'));
  117. var buff = await croppedFile.readAsBytes();
  118. // var data = base64Encode(buff);
  119. await method.updateAvatar(buff, suf);
  120. _reload();
  121. }
  122. // final buff = image.readAsBytesSync();
  123. // await method.updateAvatar(buff);
  124. // _reload();
  125. }
  126. }
  127. }