123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:liftmanager/res/resources.dart';
- import 'package:liftmanager/utils/toast.dart';
- import 'package:liftmanager/widgets/app_bar.dart';
- class ViewImagePage extends StatefulWidget {
- ViewImagePage({Key key, this.img, this.edit = false, this.width})
- : super(key: key);
- final String img;
- final bool edit;
- final width;
- @override
- ViewImagePageState createState() => ViewImagePageState();
- }
- class ViewImagePageState extends State<ViewImagePage> {
- String image;
- var w;
- var index = 1;
- var _scrollController;
- var down = 0.0; //触开始的时候的left
- var half = false; //是否超过一半
- delete() {
- showAlert(
- context,
- "提示",
- "要删除这张照片吗?",
- "确定",
- () {
- Navigator.pop(context);
- Navigator.pop(context, '删除');
- },
- txt2: "取消",
- onPre2: () {
- Navigator.pop(context);
- });
- }
- @override
- void initState() {
- //页面初始化
- super.initState();
- _scrollController = ScrollController(
- initialScrollOffset: 0,
- );
- setState(() {
- image = widget.img;
- });
- }
- nextPage(w) {
- setState(() {
- index = index + 1;
- _scrollController.animateTo(
- (index - 1) * w,
- duration: Duration(milliseconds: 200),
- curve: Curves.easeIn,
- );
- });
- }
- lastPage(w) {
- setState(() {
- index = index - 1;
- _scrollController.animateTo(
- (index - 1) * w,
- duration: Duration(milliseconds: 200),
- curve: Curves.easeIn,
- );
- });
- }
- moveEnd(e, w, l) {
- var end = e.primaryVelocity;
- if (end > 10 && index > 1) {
- lastPage(w);
- } else if (end < -10 && index < l) {
- nextPage(w);
- } else if (half == true) {
- if (down > w / 2 && index < l) {
- //右边开始滑动超过一半,则下一张
- nextPage(w);
- } else if (down < w / 2 && index > 1) {
- lastPage(w);
- } else {
- _scrollController.animateTo(
- (index - 1) * w,
- duration: Duration(milliseconds: 200),
- curve: Curves.easeIn,
- );
- }
- } else {
- _scrollController.animateTo(
- (index - 1) * w,
- duration: Duration(milliseconds: 200),
- curve: Curves.easeIn,
- );
- }
- }
- imgMove(e, w, l) {
- //down 为起点
- var left = e.position.dx;
- var now = (index - 1) * w;
- var move = left - down; //移动距离
- if (left - down > w / 2 || down - left > w / 2) {
- half = true;
- } else {
- half = false;
- }
- _scrollController.jumpTo(now - move);
- }
- Widget list(w, l) {
- List<Widget> array = [];
- array.add(
- Listener(
- // onPointerMove: (e) {
- // imgMove(e,w,l);
- // },
- // onPointerDown: (e) {
- // down = e.position.dx;
- // },
- child: GestureDetector(
- onTap: () {
- Navigator.pop(context);
- },
- onHorizontalDragEnd: (e) {
- moveEnd(e, w, l);
- },
- child: Container(
- width: w,
- child: image != null
- ? image.contains("http")
- ? Image.network(image)
- : Image.file(File(image))
- : Container(),
- ),
- ),
- ),
- );
- return ListView(
- controller: _scrollController,
- scrollDirection: Axis.horizontal,
- children: array,
- );
- }
- @override
- Widget build(BuildContext context) {
- w = MediaQuery.of(context).size.width;
- return Scaffold(
- appBar: MyAppBar(
- centerTitle: "查看图片",
- actions: <Widget>[
- widget.edit
- ? FlatButton(
- child: Text("删除"),
- textColor: Colours.text,
- highlightColor: Colors.transparent,
- onPressed: () {
- delete();
- },
- )
- : Container()
- ],
- ),
- body: Container(
- color: Colors.black,
- child: Stack(
- children: <Widget>[
- list(w, 1),
- // Positioned(
- // top: 20,
- // child: Container(
- // alignment: Alignment.center,
- // width: w,
- // child: Text('$index/$l',style: TextStyle(
- // decoration: TextDecoration.none,
- // color: Colors.white,fontSize: 16,
- // fontWeight: FontWeight.w400,
- // shadows: [
- // Shadow(color: Colors.black, offset: Offset(1, 1)),
- // ],
- // ))
- // ),
- // ),
- // Positioned(
- // top: 60,
- // right: 20,
- // child: Container(
- // alignment: Alignment.centerLeft,
- // width: 20,
- // child: GestureDetector(
- // onTap: () {delete();},
- // child: Icon(Icons.delete,color: Colors.white),
- // ),
- // ),
- // ),
- ],
- ),
- ));
- }
- }
|