view_image_page.dart 4.8 KB

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