view_image_page.dart 5.1 KB

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