ViewDialog.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //图片查看
  2. import 'package:flutter/material.dart';
  3. //import 'dart:async';
  4. /*
  5. * imgs 格式[{url:'',file:File}]
  6. * img 当前点击进入的图片
  7. * width 屏幕宽度
  8. */
  9. class ViewDialog extends StatefulWidget {
  10. ViewDialog({super.key, this.img, this.imgs, this.width});
  11. final img;
  12. final imgs;
  13. final width;
  14. @override
  15. _PageStatus createState() => _PageStatus();
  16. }
  17. class _PageStatus extends State<ViewDialog> {
  18. var image;
  19. var w;
  20. var index = 1;
  21. var _scrollController;
  22. var down = 0.0; //触开始的时候的left
  23. var half = false; //是否超过一半
  24. last() {
  25. if(1 == index) {
  26. }else {
  27. setState(() {
  28. image = widget.imgs[index - 2];
  29. index = index - 1;
  30. });
  31. }
  32. }
  33. next() {
  34. if(widget.imgs.length == index) {
  35. }else {
  36. setState(() {
  37. image = widget.imgs[index];
  38. index = index + 1;
  39. });
  40. }
  41. }
  42. delete() {
  43. Navigator.pop(context);
  44. }
  45. @override
  46. void initState() {
  47. //页面初始化
  48. super.initState();
  49. var n = 0;
  50. var nn;
  51. widget.imgs.forEach((i) {
  52. n = n + 1;
  53. if(i == widget.img) {
  54. nn = n;
  55. }
  56. });
  57. print(nn);
  58. if(nn > 1) {
  59. print(widget.width);
  60. _scrollController = ScrollController(
  61. initialScrollOffset: widget.width * (nn - 1)
  62. );
  63. }else {
  64. _scrollController = ScrollController(
  65. initialScrollOffset: 0,
  66. );
  67. }
  68. setState(() {
  69. image = widget.img;
  70. index = nn;
  71. });
  72. }
  73. nextPage(w) {
  74. setState(() {
  75. index = index + 1;
  76. _scrollController.animateTo(
  77. (index -1) * w,
  78. duration: Duration(milliseconds: 200),
  79. curve: Curves.easeIn,
  80. );
  81. });
  82. }
  83. lastPage(w) {
  84. setState(() {
  85. index = index - 1;
  86. _scrollController.animateTo(
  87. (index - 1) * w,
  88. duration: Duration(milliseconds: 200),
  89. curve: Curves.easeIn,
  90. );
  91. });
  92. }
  93. moveEnd(e,w,l) {
  94. var end = e.primaryVelocity;
  95. if(end > 10 && index > 1) {
  96. lastPage(w);
  97. }else if(end < -10 && index < l){
  98. nextPage(w);
  99. }else if(half == true){
  100. if(down > w/2 && index < l) {
  101. //右边开始滑动超过一半,则下一张
  102. nextPage(w);
  103. }else if(down < w/2 && index > 1){
  104. lastPage(w);
  105. }else {
  106. _scrollController.animateTo(
  107. (index -1) * w,
  108. duration: Duration(milliseconds: 200),
  109. curve: Curves.easeIn,
  110. );
  111. }
  112. }else {
  113. _scrollController.animateTo(
  114. (index -1) * w,
  115. duration: Duration(milliseconds: 200),
  116. curve: Curves.easeIn,
  117. );
  118. }
  119. }
  120. imgMove(e,w,l) {
  121. //down 为起点
  122. var left = e.position.dx;
  123. var now = (index -1 ) * w;
  124. var move = left - down;//移动距离
  125. if(left - down > w/2 || down - left > w/2) {
  126. half = true;
  127. }else {
  128. half = false;
  129. }
  130. _scrollController.jumpTo(now - move);
  131. }
  132. Widget list(w,l) {
  133. List<Widget> array = [];
  134. widget.imgs.forEach((i) {
  135. array.add(
  136. Listener(
  137. onPointerMove: (e) {
  138. imgMove(e,w,l);
  139. },
  140. onPointerDown: (e) {
  141. down = e.position.dx;
  142. },
  143. child: GestureDetector(
  144. onTap: (){
  145. Navigator.pop(context);
  146. },
  147. onHorizontalDragEnd: (e) {moveEnd(e,w,l);},
  148. child: Container(
  149. width: w,
  150. child: Image.network(i),
  151. ),
  152. ),
  153. ),
  154. );
  155. });
  156. return ListView(
  157. controller: _scrollController,
  158. scrollDirection: Axis.horizontal,
  159. children: array,
  160. );
  161. }
  162. @override
  163. Widget build(BuildContext context) {
  164. w = MediaQuery.of(context).size.width;
  165. var l = widget.imgs.length;
  166. return Container(
  167. color: Colors.black,
  168. child: Stack(
  169. children: <Widget>[
  170. list(w,l),
  171. Positioned(
  172. top: 20,
  173. child: Container(
  174. alignment: Alignment.center,
  175. width: w,
  176. child: Text('$index/$l',style: TextStyle(
  177. decoration: TextDecoration.none,
  178. color: Colors.white,fontSize: 16,
  179. fontWeight: FontWeight.w400,
  180. shadows: [
  181. Shadow(color: Colors.black, offset: Offset(1, 1)),
  182. ],
  183. ))
  184. ),
  185. ),
  186. Positioned(
  187. top: 20,
  188. right: 20,
  189. child: Container(
  190. alignment: Alignment.centerLeft,
  191. width: 20,
  192. child: GestureDetector(
  193. onTap: () {delete();},
  194. child: Icon(Icons.delete,color: Colors.white),
  195. ),
  196. ),
  197. ),
  198. ],
  199. ),
  200. );
  201. }
  202. }