Flutter - 图片点击全屏浏览

2023-11-18

##### demo 地址: https://github.com/iotjin/jh_flutter_demo

flutter好用的轮子推荐四-可定制的图片预览查看器photo_view
flutter九宫格图片查看器

效果图

九宫格全屏展示效果长按效果

关于九宫格布局实现

JhPhotoAllScreenShow 代码

import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

const Color selColor =Colors.white;
const Color otherColor = Colors.grey;

class FadeRoute extends PageRouteBuilder {
  final Widget page;
  FadeRoute({this.page}): super(
    pageBuilder: (
        BuildContext context,
        Animation<double> animation,
        Animation<double> secondaryAnimation,
        ) =>page,transitionsBuilder: (
      BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
      Widget child,
      ) =>FadeTransition(
    opacity: animation,
    child: child,
  ),
  );
}




class JhPhotoAllScreenShow extends StatefulWidget {

      List imgDataArr=[];
      int index=0;
      String heroTag;
      PageController controller;
      GestureTapCallback onLongPress;

      JhPhotoAllScreenShow({
        Key key,
        @required this.imgDataArr,
        this.index,
        this.onLongPress,
        this.controller,
        this.heroTag
      }) : super(key: key){
        controller=PageController(initialPage: index);
      }

  @override
  _JhPhotoAllScreenShowState createState() => _JhPhotoAllScreenShowState();
}

class _JhPhotoAllScreenShowState extends State<JhPhotoAllScreenShow> {
  int currentIndex=0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    currentIndex=widget.index;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Positioned(
              top: 0,
              left: 0,
              bottom: 0,
              right: 0,
              child:
              GestureDetector(
                child:
                Container(
                    color: Colors.black,
                    child: PhotoViewGallery.builder(
                      scrollPhysics: const BouncingScrollPhysics(),
                      builder: (BuildContext context, int index) {
                        return PhotoViewGalleryPageOptions(
                          imageProvider: NetworkImage(widget.imgDataArr[index]),
                          heroAttributes: widget.heroTag !=null?PhotoViewHeroAttributes(tag: widget.heroTag):null,

                        );
                      },
                      itemCount: widget.imgDataArr.length,
                      loadingChild: Container(),
                      backgroundDecoration: null,
                      pageController: widget.controller,
                      enableRotation: true,
                      onPageChanged: (index){
                        setState(() {
                          currentIndex=index;
                        });
                      },
                    )
                ),
                onTap: (){
                  Navigator.of(context).pop();
                },
                onLongPress: widget.onLongPress,

              )

          ),
          Positioned(
            top: MediaQuery.of(context).padding.top+30,
            width: MediaQuery.of(context).size.width,
            child: Center(
              child: Text("${currentIndex+1}/${widget.imgDataArr.length}",style: TextStyle(color: Colors.white,fontSize: 16)),
            ),
          ),
          Positioned(
            right: 10,
            top: MediaQuery.of(context).padding.top+15,
            child: IconButton(
              icon: Icon(Icons.close,size: 30,color: Colors.white,),
              onPressed: (){
                Navigator.of(context).pop();
              },
            ),
          ),
          
          Align(
              alignment: Alignment.bottomCenter,
              child:
              Container(
//                color: Colors.red,
                width: widget.imgDataArr.length>=6 ? 200:widget.imgDataArr.length<3 ? 50:100,
                height: widget.imgDataArr.length==1 ? 0:50,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: List.generate(
                    widget.imgDataArr.length,
                        (i) => GestureDetector(
                      child: CircleAvatar(
//                      foregroundColor: Theme.of(context).primaryColor,
                        radius: 5.0,
                        backgroundColor: currentIndex == i ? selColor : otherColor,
                      ),
                    ),
                  ).toList(),
                ),
              )
          )
          
          
        ],
      ),
    );
  }
}

JhPhotoAllScreenShow2 代码

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';

const Color selColor =Colors.white;
const Color otherColor = Colors.grey;

// 底部显示小圆点
class NinePictureAllScreenShow<T> extends PopupRoute<T> {
  final String barrierLabel;
  final List picList;
  final int index;
  int startX;
  int endX;

  NinePictureAllScreenShow(this.picList, this.index, {this.barrierLabel});

  @override
  Duration get transitionDuration => Duration(milliseconds: 2000);

  @override
  Color get barrierColor => Colors.black;  //背景颜色

  @override
  bool get barrierDismissible => true;

  AnimationController _animationController;

  @override
  AnimationController createAnimationController() {
    assert(_animationController == null);
    _animationController =
        BottomSheet.createAnimationController(navigator.overlay);
    return _animationController;
  }

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    return MediaQuery.removePadding(
      removeTop: true,
      context: context,
      child: GestureDetector(
        child: AnimatedBuilder(
          animation: animation,
          builder: (BuildContext context, Widget child) => GestureDetector(
            onTap: () {
              Navigator.pop(context);
            },
            child: _PictureWidget(picList, index),
          ),
        ),
      ),
    );
  }
}

class _PictureWidget extends StatefulWidget {
  final List picList;
  final int index;

  _PictureWidget(this.picList, this.index);

  @override
  State createState() {
    return _PictureWidgetState();
  }
}

class _PictureWidgetState extends State<_PictureWidget> {
  int startX = 0;
  int endX = 0;
  int index = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    index = widget.index;
  }

  @override
  Widget build(BuildContext context) {
    return new Material(
      color: Colors.transparent,
      child: new Container(
        width: double.infinity,
        padding: EdgeInsets.fromLTRB(0, 0, 0, 30),
        child: Stack(
          children: <Widget>[
            GestureDetector(
              child: Center(
                child: CachedNetworkImage(
                  imageUrl: widget.picList[index],
                  fit: BoxFit.fill,
                ),
              ),
              onHorizontalDragDown: (detail) {
                startX = detail.globalPosition.dx.toInt();
              },
              onHorizontalDragUpdate: (detail) {
                endX = detail.globalPosition.dx.toInt();
              },
              onHorizontalDragEnd: (detail) {
                _getIndex(endX - startX);
                setState(() {});
              },
              onHorizontalDragCancel: () {},
            ),

            Align(
                alignment: Alignment.bottomCenter,
                child:
                Container(
//                color: Colors.red,
                  width: widget.picList.length>=6 ? 200:widget.picList.length<3 ? 50:100,
                  height: 50,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: List.generate(
                      widget.picList.length,
                          (i) => GestureDetector(
                        child: CircleAvatar(
//                      foregroundColor: Theme.of(context).primaryColor,
                          foregroundColor: selColor,
                          radius: 5.0,
                          backgroundColor: index == i ? selColor : otherColor,
                        ),
                        onTap: () {
                          setState(() {
                            startX = endX = 0;
                            index = i;
                          });
                        },
                      ),
                    ).toList(),
                  ),
                )
            )

          ],
        ),
        alignment: Alignment.center,
      ),
    );
  }

  void _getIndex(int delta) {
    if (delta > 50) {
      setState(() {
        index--;
        index = index.clamp(0, widget.picList.length - 1);
      });
    } else if (delta < 50) {
      setState(() {
        index++;
        index = index.clamp(0, widget.picList.length - 1);
      });
    }
  }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flutter - 图片点击全屏浏览 的相关文章

随机推荐

  • Kafka 顺序消费方案

    Kafka 顺序消费方案 前言 1 问题引入 2 解决思路 3 实现方案 前言 本文针对解决Kafka不同Topic之间存在一定的数据关联时的顺序消费问题 如存在Topic insert和Topic update分别是对数据的插入和更新 当
  • applicationcontext in module file is included in 5 contexts的解决方式

    有时候IDEA会出现这样的情况 明明敲得挺好的代码却莫名其妙的出现这个错误 然后自己这个错误出现几次了 所以我要把它记录下来 让我们把他解决吧 1 file project Structure 2 Modules Spring 先把所有的
  • Java开发Telegram机器人

    基于springboot在 pom 中添加
  • Android webview显示电脑版网页

    第一步获取webview的setting 同时进行配置 settings webView getSettings settings setCacheMode WebSettings LOAD NO CACHE 支持js settings s
  • Python发送电子邮件的几种方式介绍

    发送电子邮件是Python中常见的任务之一 可以用于自动化发送通知 报表以及其他与邮件相关的任务 Python提供了几种方式来发送电子邮件 本文将介绍其中的三种常用方式 使用smtplib库 使用email库和使用第三方库 使用smtpli
  • 小程序云开发攻略,解决最棘手的问题

    背景 最近小程序非常的火 应公司业务发展要求 开发维护了几款小程序 公司开发的小程序都是由后端提供的接口 开发繁琐而复杂 直到小程序出现了云开发 仔细研读了文档之后 欣喜不已 于是我着手开发了本人的第一款小程序 小程序云开发教程地址 点我查
  • 【线性表】最常用的数据结构:线性表

    线性表 Linear List 是 最常用且 最简单的一种数据结构 有数据库知识的同学应该比较了解 线性表的定义 线性表是由n n 0 个 数据元素 结点 a 1 a 2 a n组成的有限序列 数据元素的个数n定义为表的长度 n 0时称为空
  • linux 检查程序所需库,查看命令运行所需要的库支持

    问题 我想知道当我调用一个特定的可执行文件在运行时载入了哪些共享库 是否有方法可以明确Linux上可执行程序或运行进程的共享库依赖关系 查看可执行程序的共享库依赖关系 要找出某个特定可执行依赖的库 可以使用ldd命令 这个命令调用动态链接器
  • ImportError: liblapack.so.3: cannot open shared object file: No such file or directory

    如果用的是conda的话 尝试一下 conda install c conda forge liblapack
  • 【操作教程】EasyNVR平台如何接入硬盘录像机?

    EasyNVR是基于RTSP Onvif协议接入的视频平台 可支持将前端设备的音视频进行采集 传输 处理并分发 实现视频监控直播 云端录像 云存储 检索回看 国标级联 告警等视频能力 平台兼容性高 可拓展性强 性能稳定 可应用在智慧工地 智
  • Failed to create the Java Virtual Machine问题解决

    问题现象 打开eclipse exe 弹出如下对话框 问题分析 这是eclipse启动初始化时报的错 一般出现这种情况跟安装了多个Java虚拟机有关 然后eclipse启动的时候 不知道要配置哪一个JDK 所以会报Failed to cre
  • D360周赛复盘:模拟(思维题目)⭐⭐+贪心解决可能的最小和(类似上次)

    文章目录 2833 距离原点最远的点 思路 完整版 2834 找出美丽数组的最小和 思路 完整版 2833 距离原点最远的点 给你一个长度为 n 的字符串 moves 该字符串仅由字符 L R 和 组成 字符串表示你在一条原点为 0 的数轴
  • python+selenium基于po模式的web自动化测试框架

    一 什么是Selenium Selenium是一个基于浏览器的自动化测试工具 它提供了一种跨平台 跨浏览器的端到端的web自动化解决方案 Selenium主要包括三部分 Selenium IDE Selenium WebDriver 和Se
  • 深度学习之目标检测与目标识别

    一 目标识别分类及应用场景 目前可以将现有的基于深度学习的目标检测与识别算法大致分为以下三大类 基于区域建议的目标检测与识别算法 如R CNN Fast R CNN Faster R CNN 基于回归的目标检测与识别算法 如YOLO SSD
  • FW1配置文件

    FW1 sh conf Building configuration Running configuration Version 5 5R2 ip vrouter trust vr exit vswitch vswitch1 exit zo
  • 【Linux】向Linux 5.11.8内核加入新的系统调用

    目录 特殊声明 A mathcal A A 获取root权限
  • 【分布式】分布式相关书籍

    1 概述 1 1 分布式文章汇总 书籍 悟空聊架构 分布式文章汇总 评分 8分 第一章 主要讲解 拜占庭故障 这个讲解的非常好值得一看 第二章 主要讲解 Paxos 共识算法 这个图很好 但是仍然很难懂 第三章 动图讲解分布式 Raft 但
  • 开始学下VC++了

    有点迟了 以前光学DELPHI了 结果还是半瓶子的样子 现在接触下VC 要不会让人BS的 希望开个好点的头吧 不要老是三心二意的哦耶 PS QQ的五笔比搜狗的五笔好用不 个人感觉还是QQ的有点好用哦 嘿嘿 Orz 转载于 https www
  • mac安装python3.7_Mac安装python3.7

    mac默认安装的pyhon版本为2 7 如果要更新为python3 7 那么可以直接安装python3 7 千万不要卸载2 7版本 相信我 把系统自带的东西胡乱卸载掉的话你绝对会后悔的 步骤一 下载安装python3 7 方法一 使用hom
  • Flutter - 图片点击全屏浏览

    demo 地址 https github com iotjin jh flutter demo flutter好用的轮子推荐四 可定制的图片预览查看器photo view flutter九宫格图片查看器 效果图 关于九宫格布局实现 JhPh