gallery_photo_view_wrapper.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import 'dart:io';
  2. import 'package:cached_network_image/cached_network_image.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:image_picker/image_picker.dart';
  5. import 'package:photo_view/photo_view.dart';
  6. import 'package:photo_view/photo_view_gallery.dart';
  7. class GalleryPhotoViewWrapper extends StatefulWidget {
  8. final int initialIndex;
  9. final PageController pageController;
  10. /// 相册照片
  11. final List<XFile>? galleryItems;
  12. /// 网络图片
  13. final List<String>? netImgList;
  14. GalleryPhotoViewWrapper({
  15. super.key,
  16. this.initialIndex = 0,
  17. this.galleryItems, this.netImgList,
  18. }) : pageController = PageController(initialPage: initialIndex);
  19. @override
  20. State<GalleryPhotoViewWrapper> createState() => _GalleryPhotoViewWrapperState();
  21. }
  22. class _GalleryPhotoViewWrapperState extends State<GalleryPhotoViewWrapper> {
  23. late ValueNotifier<int> indexVal = ValueNotifier(widget.initialIndex);
  24. PhotoViewGalleryPageOptions _buildItem(BuildContext context, int index) {
  25. return (widget.galleryItems ?? [] ).isNotEmpty
  26. ? PhotoViewGalleryPageOptions(
  27. imageProvider: FileImage(File(widget.galleryItems![index].path)) ,
  28. initialScale: PhotoViewComputedScale.contained,
  29. minScale: PhotoViewComputedScale.contained * (0.5 + index / 10),
  30. maxScale: PhotoViewComputedScale.covered * 4.1,
  31. )
  32. : PhotoViewGalleryPageOptions(
  33. imageProvider: CachedNetworkImageProvider(widget.netImgList![index]) ,
  34. initialScale: PhotoViewComputedScale.contained,
  35. minScale: PhotoViewComputedScale.contained * (0.5 + index / 10),
  36. maxScale: PhotoViewComputedScale.covered * 4.1,
  37. );
  38. }
  39. @override
  40. Widget build(BuildContext context) {
  41. return Scaffold(
  42. body: Container(
  43. decoration: const BoxDecoration(
  44. color: Colors.black,
  45. ),
  46. constraints: BoxConstraints.expand(
  47. height: MediaQuery.of(context).size.height,
  48. ),
  49. child: Stack(
  50. alignment: Alignment.bottomCenter,
  51. children: [
  52. PhotoViewGallery.builder(
  53. scrollPhysics: const BouncingScrollPhysics(),
  54. builder: _buildItem,
  55. itemCount: widget.galleryItems?.length ?? widget.netImgList?.length,
  56. pageController: widget.pageController,
  57. onPageChanged: (index) {
  58. indexVal.value = index;
  59. },
  60. ),
  61. Padding(
  62. padding: const EdgeInsets.only(bottom: 12),
  63. child: ValueListenableBuilder(
  64. valueListenable: indexVal,
  65. builder: (context, index, child) {
  66. return Text(
  67. "${index + 1}/${widget.galleryItems?.length ?? widget.netImgList?.length}",
  68. style: const TextStyle(
  69. color: Colors.white,
  70. fontSize: 18.0,
  71. ),
  72. );
  73. }),
  74. ),
  75. Positioned(
  76. top: 28,
  77. left: 8,
  78. child: IconButton(
  79. onPressed: () {
  80. Navigator.pop(context);
  81. },
  82. icon: const Icon(
  83. Icons.arrow_circle_left,
  84. color: Colors.white,
  85. size: 38,
  86. )),
  87. ),
  88. ],
  89. ),
  90. ),
  91. );
  92. }
  93. }