Flutter:组件9、图片预览

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

class ImagePreview extends StatelessWidget {
  final String? singleImage;
  final List<String>? imageList;
  final int initialIndex;

  const ImagePreview({
    super.key,
    this.singleImage,
    this.imageList,
    this.initialIndex = 0,
  });

  @override
  Widget build(BuildContext context) {
    // 如果提供了单张图片,转换为列表
    final List<String> images = singleImage != null 
        ? [singleImage!] 
        : imageList ?? [];

    if (images.isEmpty) return const SizedBox();

    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.black,
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.close, color: Colors.white,size: 24,),
          onPressed: () => Get.back(),
        ),
      ),
      body: PhotoViewGallery.builder(
        itemCount: images.length,
        builder: (context, index) {
          return PhotoViewGalleryPageOptions(
            imageProvider: NetworkImage(images[index]),
            initialScale: PhotoViewComputedScale.contained,
            minScale: PhotoViewComputedScale.contained,
            maxScale: PhotoViewComputedScale.covered * 2,
          );
        },
        scrollPhysics: const BouncingScrollPhysics(),
        backgroundDecoration: const BoxDecoration(color: Colors.black),
        pageController: PageController(initialPage: initialIndex),
      ),
    );
  }
} 

使用方式:单张预览

js 复制代码
onTap(() {
  Get.to(() => ImagePreview(singleImage: '图片url'));
})

使用方式:多张预览

js 复制代码
List<String> img = ['图片url','图片url','图片url'];
for(var i = 0; i < (img.length ?? 0); i++)
ImgWidget(
  path: item.img?[i] ?? '',
  width: 170.w,
  height: 170.w,
  radius: 20.w,
).onTap(() {
  Get.to(() => ImagePreview(
    imageList: item.img,
    initialIndex: i,
  ));
}),
相关推荐
火柴就是我20 小时前
学习一些常用的混合模式之BlendMode. dst_atop
android·flutter
火柴就是我21 小时前
学习一些常用的混合模式之BlendMode. dstIn
android·flutter
火柴就是我1 天前
学习一些常用的混合模式之BlendMode. dst
android·flutter
前端不太难1 天前
Sliver 为什么能天然缩小 rebuild 影响面
flutter·性能优化·状态模式
带带弟弟学爬虫__1 天前
Flutter 逆向想学却无从下手?
flutter
行者961 天前
Flutter跨平台开发:颜色选择器适配OpenHarmony
flutter·harmonyos·鸿蒙
不爱吃糖的程序媛1 天前
深度解析OpenHarmony跨平台框架生态:RN、Flutter、Cordova、KMP四大方向全梳理
flutter
kirk_wang1 天前
Flutter艺术探索-Flutter样式系统:TextStyle与主题配置
flutter·移动开发·flutter教程·移动开发教程
火柴就是我1 天前
Flutter 混合模式下:saveLayer 混合注意点
android·flutter
AiFlutter1 天前
四、动画图表(03):饼图
flutter·低代码·低代码平台·aiflutter·aiflutter低代码