Hibok
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

180 rindas
5.2 KiB

  1. import 'dart:io';
  2. import 'package:chat/generated/i18n.dart';
  3. import 'package:chat/utils/CustomUI.dart';
  4. import 'package:chat/utils/LoadingDialog.dart';
  5. import 'package:chat/utils/MessageMgr.dart';
  6. import 'package:chat/utils/screen.dart';
  7. import 'package:extended_image/extended_image.dart';
  8. import 'package:flutter/cupertino.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:image_cropper/image_cropper.dart';
  11. import 'package:image_picker/image_picker.dart';
  12. import '../utils/HttpUtil.dart';
  13. import 'package:dio/dio.dart';
  14. import "../data/UserData.dart";
  15. import '../utils/TokenMgr.dart';
  16. class MyHeadViewPage extends StatefulWidget {
  17. MyHeadViewPage({Key key}) : super(key: key);
  18. @override
  19. _MyHeadViewPageState createState() => new _MyHeadViewPageState();
  20. }
  21. class _MyHeadViewPageState extends State<MyHeadViewPage>
  22. with SingleTickerProviderStateMixin {
  23. AnimationController _animationController;
  24. Animation<double> _animation;
  25. Function animationListener;
  26. List<double> doubleTapScales = <double>[1.0, 2.0];
  27. @override
  28. void initState() {
  29. super.initState();
  30. _animationController = AnimationController(
  31. duration: const Duration(milliseconds: 150), vsync: this);
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. return Scaffold(
  36. body: SafeArea(child: _buildBody()),
  37. backgroundColor: Colors.black,
  38. );
  39. }
  40. Widget _buildHeadView() {
  41. return ExtendedImageGesturePageView(
  42. children: <Widget>[
  43. InkWell(
  44. onTap: () {
  45. Navigator.pop(context);
  46. },
  47. child: ExtendedImage.network(
  48. UserData().basicInfo.headimgurl,
  49. fit: BoxFit.contain,
  50. mode: ExtendedImageMode.gesture,
  51. initGestureConfigHandler: (state) {
  52. return GestureConfig(
  53. inPageView: true,
  54. maxScale: 5,
  55. initialAlignment: InitialAlignment.topCenter,
  56. animationMaxScale: 5,
  57. cacheGesture: false);
  58. },
  59. onDoubleTap: (ExtendedImageGestureState state) {
  60. var pointerDownPosition = state.pointerDownPosition;
  61. double begin = state.gestureDetails.totalScale;
  62. double end;
  63. //remove old
  64. _animation?.removeListener(animationListener);
  65. //stop pre
  66. _animationController.stop();
  67. //reset to use
  68. _animationController.reset();
  69. if (begin == doubleTapScales[0]) {
  70. end = doubleTapScales[1];
  71. } else {
  72. end = doubleTapScales[0];
  73. }
  74. animationListener = () {
  75. //print(_animation.value);
  76. state.handleDoubleTap(
  77. scale: _animation.value,
  78. doubleTapPosition: pointerDownPosition);
  79. };
  80. _animation = _animationController
  81. .drive(Tween<double>(begin: begin, end: end));
  82. _animation.addListener(animationListener);
  83. _animationController.forward();
  84. },
  85. ))
  86. ],
  87. );
  88. }
  89. Widget _buildBody() {
  90. return Stack(
  91. children: <Widget>[
  92. _buildHeadView(),
  93. Positioned(
  94. child: _buildChangeButton(),
  95. bottom: 40,
  96. )
  97. ],
  98. );
  99. }
  100. Widget _buildChangeButton() {
  101. return InkWell(
  102. onTap: _sendPicture,
  103. child: Container(
  104. alignment: Alignment.center,
  105. width: Screen.width,
  106. child: Container(
  107. height: 47.5,
  108. alignment: Alignment.center,
  109. width: 230,
  110. child: Text(
  111. I18n.of(context).upload_avatar,
  112. style: TextStyle(color: Colors.white, fontSize: 17.5),
  113. ),
  114. decoration: BoxDecoration(
  115. borderRadius: BorderRadius.circular(24),
  116. color: Color(0xFF181818)),
  117. )));
  118. }
  119. void _sendPicture() async {
  120. if (await CustomUI.showPhotoPermissionSetting(context)) {
  121. showDialog(
  122. context: context,
  123. barrierDismissible: false,
  124. builder: (BuildContext context) {
  125. return LoadingDialog(
  126. text: "",
  127. );
  128. });
  129. var tempFile = await ImagePicker.pickImage(source: ImageSource.gallery);
  130. Navigator.of(context).pop();
  131. if (tempFile != null) {
  132. _cropPicture(tempFile);
  133. }
  134. }
  135. }
  136. void _cropPicture(tempFile) async {
  137. File croppedFile = await ImageCropper.cropImage(
  138. sourcePath: tempFile.path,
  139. aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1),
  140. );
  141. if (croppedFile != null) {
  142. Map data = {"type": 1, "userId": UserData().basicInfo.userId};
  143. data['sign'] = TokenMgr().getSign(data);
  144. Response res = await HttpUtil()
  145. .uploadFile(croppedFile, data, 'upload/file/postflie', 'image');
  146. var resData = res.data;
  147. if (resData['code'] == 0) {
  148. UserData().basicInfo.headimgurl = resData['msg'];
  149. MessageMgr().emit('change_my_headview');
  150. if (mounted) {
  151. setState(() {});
  152. }
  153. }
  154. }
  155. }
  156. @override
  157. void dispose() {
  158. super.dispose();
  159. }
  160. }