flutter 圖片預(yù)覽縮放、滑動(dòng) photo_view

使用photo_view插件,預(yù)覽圖片,可放大、縮小、滑動(dòng)

1、在pubspec.yaml添加依賴

  # 圖片預(yù)覽
  photo_view: ^0.13.0

獲取插件

flutter pub get

2、多張圖片預(yù)覽

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:photo_view/photo_view_gallery.dart';
 
typedef PageChanged = void Function(int index);
 
class PictureOverview extends StatefulWidget {
  final List imageItems; //圖片列表
  final int defaultIndex; //默認(rèn)第幾張
  final PageChanged pageChanged; //切換圖片回調(diào)
  final Axis direction; //圖片查看方向
  final Decoration decoration; //背景設(shè)計(jì)
 
  PictureOverview(
      {this.imageItems,
      this.defaultIndex = 1,
      this.pageChanged,
      this.direction = Axis.horizontal,
      this.decoration})
      : assert(imageItems != null);
  @override
  _PictureOverviewState createState() => _PictureOverviewState();
}
 
class _PictureOverviewState extends State<PictureOverview> {
  int currentIndex;
  @override
  void initState() {
    super.initState();
    // TODO: implement initState
    currentIndex = widget.defaultIndex;
  }
 
  @override
  Widget build(BuildContext context) {
    return Material(
      child: Stack(
        children: [
          Container(
              child: PhotoViewGallery.builder(
                  scrollPhysics: const BouncingScrollPhysics(),
                  builder: (BuildContext context, int index) {
                    return PhotoViewGalleryPageOptions(
                      imageProvider: NetworkImage(widget.imageItems[index]),
                    );
                  },
                  scrollDirection: widget.direction,
                  itemCount: widget.imageItems.length,
                  backgroundDecoration:
                      widget.decoration ?? BoxDecoration(color: Colors.black),
                  pageController:
                      PageController(initialPage: widget.defaultIndex),
                  onPageChanged: (index) => setState(() {
                        currentIndex = index;
                        if (widget.pageChanged != null) {
                          widget.pageChanged(index);
                        }
                      }))),
          Positioned(
            bottom: 20,
            child: Container(
                alignment: Alignment.center,
                width: MediaQuery.of(context).size.width,
                child: Text("${currentIndex + 1}/${widget.imageItems.length}",
                    style: TextStyle(
                      decoration: TextDecoration.none,
                      color: Colors.white,
                      fontSize: 16,
                      fontWeight: FontWeight.w400,
                      shadows: [
                        Shadow(color: Colors.black, offset: Offset(1, 1)),
                      ],
                    ))),
          ),
          Positioned(//右上角關(guān)閉
            top: 40,
            right: 40,
            child: Container(
              alignment: Alignment.centerLeft,
              width: 20,
              child: GestureDetector(
                onTap: () {
                  //隱藏預(yù)覽
                  Navigator.pop(context);
                },
                child: Icon(Icons.close_rounded, color: Colors.white),
              ),
            ),
          ),
          Positioned(//數(shù)量顯示
            right: 20,
            top: 20,
            child: Text(
              '${currentIndex + 1}/${widget.imageItems.length}',
              style: TextStyle(color: Colors.black),
            ),
          )
        ],
      ),
    );
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容