当前位置: 首页 > article >正文

Flutter:photo_view图片预览功能

导入SDK

photo_view: ^0.15.0

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

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'),
        ),
      ),
    );
  }
}

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

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),),
          )
        )
      ],
    );
  }
}

在这里插入图片描述


http://www.kler.cn/a/403994.html

相关文章:

  • C# Winform 2048小游戏源码
  • 鸿蒙 ArkTS 中文本居中对齐的坑:为何设置宽度至关重要?
  • Kibana 本地安装使用
  • LeetCode:1008. 前序遍历构造二叉搜索树
  • Consumer Group
  • Linux 编译Ubuntu24内核
  • uniapp+vue3+ts H5端使用Quill富文本插件以及解决上传图片反显的问题
  • 购物街项目TabBar的封装
  • 设计模式:4、命令模式(双重委托)
  • .NET 9 中 LINQ 新增功能实操
  • 详细教程-Linux上安装单机版的Hadoop
  • 支付宝租赁小程序的优势与应用前景分析
  • 创客匠人老蒋:个人IP如何获取有效流量?
  • 重读《人月神话》(14)-整体部分(The Whole and the Parts)
  • LVI-SAM视觉特征点深度恢复原理解析
  • 23种设计模式速记法
  • 《Beginning C++20 From Novice to Professional》第十三章 Operator Overloading
  • 泷羽sec----shell编程(7)
  • 什么是React Native?
  • 使用node-addon-api实现从c到nodejs模块全流程