preview_images.dart 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'package:flutter/material.dart';
  2. import 'package:photo_view/photo_view.dart';
  3. import 'package:photo_view/photo_view_gallery.dart';
  4. class PhotoViewGalleryScreen extends StatefulWidget {
  5. List images=[];
  6. int index=0;
  7. String heroTag;
  8. PageController controller;
  9. PhotoViewGalleryScreen({Key key,@required this.images,this.index,this.controller,this.heroTag}) : super(key: key){
  10. controller=PageController(initialPage: index);
  11. }
  12. @override
  13. _PhotoViewGalleryScreenState createState() => _PhotoViewGalleryScreenState();
  14. }
  15. class _PhotoViewGalleryScreenState extends State<PhotoViewGalleryScreen> {
  16. int currentIndex=0;
  17. @override
  18. void initState() {
  19. // TODO: implement initState
  20. super.initState();
  21. currentIndex=widget.index;
  22. }
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. body: Stack(
  27. children: <Widget>[
  28. Positioned(
  29. top: 0,
  30. left: 0,
  31. bottom: 0,
  32. right: 0,
  33. child: Container(
  34. child: PhotoViewGallery.builder(
  35. scrollPhysics: const BouncingScrollPhysics(),
  36. builder: (BuildContext context, int index) {
  37. return PhotoViewGalleryPageOptions(
  38. imageProvider: NetworkImage(widget.images[index]),
  39. // heroAttributes: widget.heroTag.isNotEmpty?PhotoViewHeroAttributes(tag: widget.heroTag):null,
  40. heroAttributes: null
  41. );
  42. },
  43. itemCount: widget.images.length,
  44. loadingChild: Container(),
  45. backgroundDecoration: null,
  46. pageController: widget.controller,
  47. enableRotation: true,
  48. onPageChanged: (index){
  49. setState(() {
  50. currentIndex=index;
  51. });
  52. },
  53. )
  54. ),
  55. ),
  56. Positioned(//图片index显示
  57. top: MediaQuery.of(context).padding.top+15,
  58. width: MediaQuery.of(context).size.width,
  59. child: Center(
  60. child: Text("${currentIndex+1}/${widget.images.length}",style: TextStyle(color: Colors.white,fontSize: 16)),
  61. ),
  62. ),
  63. Positioned(//右上角关闭按钮
  64. right: 10,
  65. top: MediaQuery.of(context).padding.top,
  66. child: IconButton(
  67. icon: Icon(Icons.close,size: 30,color: Colors.white,),
  68. onPressed: (){
  69. Navigator.of(context).pop();
  70. },
  71. ),
  72. ),
  73. ],
  74. ),
  75. );
  76. }
  77. }