Flutter:photo_view图片预览功能

导入SDK

js 复制代码
photo_view: ^0.15.0

单张图片预览,支持放大缩小

js 复制代码
import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';

...
...

class _MyHomePageState extends State<MyHomePage>{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('标题'),
      ),
      body: Center(
        child: PhotoView(
            imageProvider: const NetworkImage('https://cdn.uviewui.com/uview/swiper/1.jpg'),
        ),
      ),
    );
  }
}

多张图片预览,支持放大缩小,滑动显示

js 复制代码
class _MyHomePageState extends State<MyHomePage>{
  var bannerList = [
    'https://cdn.uviewui.com/uview/swiper/1.jpg',
    'https://cdn.uviewui.com/uview/swiper/2.jpg',
    'https://cdn.uviewui.com/uview/swiper/3.jpg',
  ];
  int _current = 0;
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Scaffold(
          appBar: AppBar(
            title: const Text('标题'),
          ),
          body: Center(
              child: PhotoViewGallery.builder(
                itemCount: bannerList.length,
                builder: (context,index){
                  return PhotoViewGalleryPageOptions(imageProvider: NetworkImage(bannerList[index]));
                },
                onPageChanged: ((index){
                  setState(() {
				    _current = index;
                    print('图片滑动触发:$_current');
                  });
                }),
                pageController: PageController(initialPage: _current), // 可以配置默认显示第几张图片
              )
          ),
        ),
        Positioned(
          left: 0,
          bottom: 0,
          right: 0,
          child: Container(
            alignment: Alignment.center,
            child: Text('${_current+1}/${bannerList.length}',style: TextStyle(fontSize: 20,color: Colors.white,decoration: TextDecoration.none),),
          )
        )
      ],
    );
  }
}
相关推荐
2501_9160074713 分钟前
iPhone APP 性能测试怎么做,除了Instruments还有什么工具?
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063219 分钟前
Windows 环境下有哪些可用的 iOS 上架工具, iOS 上架工具的使用方式
android·ios·小程序·https·uni-app·iphone·webview
智航GIS1 小时前
7.1 自定义函数
前端·javascript·python
l134062082351 小时前
Flutter Geocoding 在鸿蒙上的使用指南
flutter·华为·harmonyos
冬奇Lab1 小时前
稳定性性能系列之六——Java异常与JE分析实战
android·性能优化·debug
AiFlutter2 小时前
三、内容展示(02):图片
flutter·低代码·低代码平台·aiflutter·aiflutter低代码
Summer不秃2 小时前
使用 SnapDOM + jsPDF 生成高质量 PDF (含多页分页, 附源码)
前端·javascript·vue.js·pdf·node.js
爱装代码的小瓶子2 小时前
【c++进阶】c++11的魔法:从模板到可变模板.
android·开发语言·c++
l134062082352 小时前
344.在鸿蒙上使用 animations Flutter 包的指南
flutter·华为·harmonyos
灯把黑夜烧了一个洞2 小时前
2026年跨年倒计时网页版
javascript·css·html·2026跨年代码·新年代码