当前位置:   article > 正文

flutter 图片预览缩放、滑动 photo_view_flutter图片放大预览

flutter图片放大预览

使用photo_view插件,预览图片,可放大、缩小、滑动

1、在pubspec.yaml添加依赖

  1. # 图片预览
  2. photo_view: ^0.13.0

获取插件

flutter pub get

2、多张图片预览

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:photo_view/photo_view_gallery.dart';
  4. typedef PageChanged = void Function(int index);
  5. class PictureOverview extends StatefulWidget {
  6. final List imageItems; //图片列表
  7. final int defaultIndex; //默认第几张
  8. final PageChanged pageChanged; //切换图片回调
  9. final Axis direction; //图片查看方向
  10. final Decoration decoration; //背景设计
  11. PictureOverview(
  12. {this.imageItems,
  13. this.defaultIndex = 1,
  14. this.pageChanged,
  15. this.direction = Axis.horizontal,
  16. this.decoration})
  17. : assert(imageItems != null);
  18. @override
  19. _PictureOverviewState createState() => _PictureOverviewState();
  20. }
  21. class _PictureOverviewState extends State<PictureOverview> {
  22. int currentIndex;
  23. @override
  24. void initState() {
  25. super.initState();
  26. // TODO: implement initState
  27. currentIndex = widget.defaultIndex;
  28. }
  29. @override
  30. Widget build(BuildContext context) {
  31. return Material(
  32. child: Stack(
  33. children: [
  34. Container(
  35. child: PhotoViewGallery.builder(
  36. scrollPhysics: const BouncingScrollPhysics(),
  37. builder: (BuildContext context, int index) {
  38. return PhotoViewGalleryPageOptions(
  39. imageProvider: NetworkImage(widget.imageItems[index]),
  40. );
  41. },
  42. scrollDirection: widget.direction,
  43. itemCount: widget.imageItems.length,
  44. backgroundDecoration:
  45. widget.decoration ?? BoxDecoration(color: Colors.black),
  46. pageController:
  47. PageController(initialPage: widget.defaultIndex),
  48. onPageChanged: (index) => setState(() {
  49. currentIndex = index;
  50. if (widget.pageChanged != null) {
  51. widget.pageChanged(index);
  52. }
  53. }))),
  54. Positioned(
  55. bottom: 20,
  56. child: Container(
  57. alignment: Alignment.center,
  58. width: MediaQuery.of(context).size.width,
  59. child: Text("${currentIndex + 1}/${widget.imageItems.length}",
  60. style: TextStyle(
  61. decoration: TextDecoration.none,
  62. color: Colors.white,
  63. fontSize: 16,
  64. fontWeight: FontWeight.w400,
  65. shadows: [
  66. Shadow(color: Colors.black, offset: Offset(1, 1)),
  67. ],
  68. ))),
  69. ),
  70. Positioned(//右上角关闭
  71. top: 40,
  72. right: 40,
  73. child: Container(
  74. alignment: Alignment.centerLeft,
  75. width: 20,
  76. child: GestureDetector(
  77. onTap: () {
  78. //隐藏预览
  79. Navigator.pop(context);
  80. },
  81. child: Icon(Icons.close_rounded, color: Colors.white),
  82. ),
  83. ),
  84. ),
  85. Positioned(//数量显示
  86. right: 20,
  87. top: 20,
  88. child: Text(
  89. '${currentIndex + 1}/${widget.imageItems.length}',
  90. style: TextStyle(color: Colors.black),
  91. ),
  92. )
  93. ],
  94. ),
  95. );
  96. }
  97. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/312954?site
推荐阅读
相关标签
  

闽ICP备14008679号