edit_avatar.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import 'dart:ui';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:get/get_connect/http/src/utils/utils.dart';
  5. import 'package:head_image_cropper/head_image_cropper.dart';
  6. import 'package:link/view/component/link_btn.dart';
  7. import 'package:photo_view/photo_view.dart';
  8. import 'package:screenshot/screenshot.dart';
  9. import '../constants.dart';
  10. import '../utils/size_fit.dart';
  11. import 'component/top_container.dart';
  12. class EditAvatar extends StatefulWidget {
  13. Uint8List image;
  14. var callback;
  15. EditAvatar({Key? key, required this.image,required this.callback}) : super(key: key);
  16. @override
  17. State<EditAvatar> createState() => _EditAvatarState();
  18. }
  19. class _EditAvatarState extends State<EditAvatar> {
  20. var _controller = CropperController();
  21. @override
  22. void initState() {
  23. // TODO: implement initState
  24. super.initState();
  25. }
  26. @override
  27. void dispose() {
  28. // TODO: implement dispose
  29. super.dispose();
  30. }
  31. clip() {
  32. _controller.outImage().then((image) async{
  33. var bytes = (await (image.toByteData(format: ImageByteFormat.png)))
  34. ?.buffer
  35. .asUint8List();
  36. widget.callback(bytes);
  37. Navigator.of(context).pop();
  38. });
  39. }
  40. @override
  41. Widget build(BuildContext context) {
  42. SizeFit.initialize(context);
  43. double width = 300.px;
  44. double height = 300.px;
  45. double left = (MediaQuery.of(context).size.width - width) / 2.0;
  46. double top = (MediaQuery.of(context).size.height - width) / 2.0;
  47. return Material(
  48. color: kBgColor,
  49. child: TopContainer(
  50. child: Stack(
  51. children: [
  52. Container(
  53. padding: EdgeInsets.zero,
  54. child: CropperImage(
  55. MemoryImage(widget.image),
  56. controller: _controller,
  57. ),
  58. ),
  59. Positioned(
  60. left: 0,
  61. bottom: 0,
  62. right: 0,
  63. height: 110.px,
  64. child: Container(
  65. alignment: Alignment.center,
  66. child: LinkButton(
  67. title: '完成',
  68. disable: false,
  69. isBlack: false,
  70. callback: () {
  71. clip();
  72. })),
  73. )
  74. ],
  75. )
  76. // child: Screenshot(
  77. // controller: screenshotController,
  78. // child: Stack(children: [
  79. // PhotoView(
  80. // maxScale: 1.5,
  81. // minScale: 0.1,
  82. // filterQuality: FilterQuality.medium,
  83. // scaleStateChangedCallback: ((value) {}),
  84. // imageProvider: MemoryImage(widget.image)),
  85. // Positioned(
  86. // left: left,
  87. // top: top,
  88. // width: width,
  89. // height: height,
  90. // child: IgnorePointer(
  91. // child: Container(
  92. // decoration: BoxDecoration(
  93. // color: Colors.transparent,
  94. // border: Border.all(color: Colors.white, width: 2.px)),
  95. // ))),
  96. // Positioned(
  97. // left: 0,
  98. // right: 0,
  99. // top: 0,
  100. // height: top,
  101. // child: IgnorePointer(
  102. // child: Container(color: const Color(0x80000000)),
  103. // )),
  104. // Positioned(
  105. // left: 0,
  106. // right: 0,
  107. // bottom: 0,
  108. // height: top,
  109. // child: IgnorePointer(
  110. // child: Container(color: const Color(0x80000000)),
  111. // )),
  112. // Positioned(
  113. // left: 0,
  114. // width: left,
  115. // top: top,
  116. // height: height,
  117. // child: IgnorePointer(
  118. // child: Container(color: const Color(0x80000000)),
  119. // )),
  120. // Positioned(
  121. // width: left,
  122. // right: 0,
  123. // top: top,
  124. // height: height,
  125. // child: IgnorePointer(
  126. // child: Container(color: const Color(0x80000000)),
  127. // )),
  128. // Positioned(
  129. // left: 0,
  130. // bottom: 0,
  131. // right: 0,
  132. // height: 110.px,
  133. // child: Container(
  134. // alignment: Alignment.center,
  135. // child: LinkButton(
  136. // title: '完成',
  137. // disable: false,
  138. // isBlack: false,
  139. // callback: () {
  140. // clip();
  141. // })),
  142. // )
  143. // ]),
  144. // )
  145. ));
  146. }
  147. }
  148. class MyClipper extends CustomClipper<RRect> {
  149. @override
  150. RRect getClip(Size size) => RRect.fromLTRBXY(10.0, 15.0, 380.0, 380.0, 0.0,
  151. 0.0); //RRect.fromLTWH(10.0, 15.0, 40.0, 30.0);
  152. @override
  153. bool shouldReclip(CustomClipper<RRect> oldClipper) => true;
  154. }