flutter 实现 大图预览

1- 导入包 具体版本请前往pub.dev查看

  # 图片预览
  photo_view: ^0.13.0

2- 路由动画组件

import 'package:flutter/cupertino.dart';
​
/// 路由动画
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,
        ),
  );
}

3- 真正实现大图查看

import 'dart:io';
​
import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
​
class PhotoViewGalleryScreen extends StatefulWidget {
  List images = [];
  int index = 0;
  String heroTag;
  PageController controller;
  bool isFile = false;
​
  PhotoViewGalleryScreen(
      {Key key,
      @required this.images,
      this.index,
      this.controller,
      this.heroTag,
      this.isFile
      })
      : super(key: key) {
    controller = PageController(initialPage: index);
  }
​
  @override
  _PhotoViewGalleryScreenState createState() => _PhotoViewGalleryScreenState();
}
​
class _PhotoViewGalleryScreenState extends State<PhotoViewGalleryScreen> {
  int currentIndex = 0;
​
  @override
  void initState() {
    super.initState();
    currentIndex = widget.index;
  }
​
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          color: Colors.black,
        ),
        constraints: BoxConstraints.expand(
          height: MediaQuery.of(context).size.height,
        ),
        child: Stack(
          children: <Widget>[
            Container(
                child: PhotoViewGallery.builder(
              scrollPhysics: const BouncingScrollPhysics(),
              builder: (BuildContext context, int index) {
                return PhotoViewGalleryPageOptions(
                  imageProvider: widget.isFile?FileImage(File(widget.images[index].path)):NetworkImage(widget.images[index]),
                  heroAttributes: widget.heroTag.isNotEmpty
                      ? PhotoViewHeroAttributes(tag: widget.heroTag)
                      : null,
                );
              },
              itemCount: widget.images.length,
              backgroundDecoration: null,
              pageController: widget.controller,
              enableRotation: true,
              onPageChanged: (index) {
                setState(() {
                  currentIndex = index;
                });
              },
            )),
            Positioned(
              top: MediaQuery.of(context).padding.top + 15,
              width: MediaQuery.of(context).size.width,
              child: Center(
                child: Text("${currentIndex + 1}/${widget.images.length}",
                    style: TextStyle(color: Colors.white, fontSize: 16)),
              ),
            ),
            Positioned(
              right: 10,
              top: MediaQuery.of(context).padding.top,
              child: IconButton(
                icon: Icon(
                  Icons.close,
                  size: 30,
                  color: Colors.white,
                ),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
​

4- 代码中使用

在图片上添加GestureDetector ,在ontap中写入下列内容

Navigator.of(context).push(new FadeRoute(
                    page: PhotoViewGalleryScreen(
                        images: imgList, //传入图片list
                        index: index, //传入当前点击的图片的index
                        heroTag: '',
                        isFile: false)));

以上便实现大图预览功能

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现图片的缓入缓出效果,你可以使用 Flutter 中的动画来实现。下面是一个简单的示例代码,演示了图片的缓入缓出效果: 首先,确保你已经在 `pubspec.yaml` 文件中添加了图片资源的引用,例如: ```yaml flutter: assets: - images/image.jpg ``` 然后,在你的 Flutter 页面中,使用 `AnimatedOpacity` 和 `Image` 组件来实现图片的缓入缓出效果: ```dart import 'package:flutter/material.dart'; class ImageFadeInOut extends StatefulWidget { @override _ImageFadeInOutState createState() => _ImageFadeInOutState(); } class _ImageFadeInOutState extends State<ImageFadeInOut> with SingleTickerProviderStateMixin { AnimationController _controller; Animation<double> _animation; bool _showImage = false; @override void initState() { super.initState(); _controller = AnimationController( duration: Duration(milliseconds: 500), vsync: this, ); _animation = Tween<double>(begin: 0.0, end: 1.0).animate(_controller); } @override void dispose() { _controller.dispose(); super.dispose(); } Future<void> _toggleImage() async { if (_showImage) { await _controller.reverse(); } else { await _controller.forward(); } setState(() { _showImage = !_showImage; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Image Fade In/Out'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ AnimatedOpacity( opacity: _showImage ? 1.0 : 0.0, duration: Duration(milliseconds: 500), child: Image.asset('images/image.jpg'), ), SizedBox(height: 16.0), ElevatedButton( onPressed: _toggleImage, child: Text(_showImage ? 'Fade Out' : 'Fade In'), ), ], ), ), ); } } ``` 在这个示例中,我们使用了 `AnimatedOpacity` 组件来控制图片的透明度,从而实现缓入缓出效果。`_toggleImage` 方法用于切换图片的显示状态,并在切换完成后更新界面。 在 `build` 方法中,我们将 `AnimatedOpacity` 组件包裹在一个居中的 `Column` 中,并添加了一个按钮用于触发图片的缓入缓出效果。 记得将图片资源的路径替换为你自己的图片路径。 通过运行这个示例,你就可以看到图片缓入缓出的效果了。点击按钮可以切换图片的显示状态,实现动态的缓入缓出效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值