123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- import 'dart:typed_data';
- import 'package:cached_network_image/cached_network_image.dart';
- import 'package:dio/dio.dart';
- import 'package:flutter/material.dart';
- import 'package:image_gallery_saver/image_gallery_saver.dart';
- import 'package:permission_handler/permission_handler.dart';
- import 'package:photo_view/photo_view.dart';
- import 'package:photo_view/photo_view_gallery.dart';
- import 'package:sport/application.dart';
- import 'package:sport/bean/image.dart' as photo;
- import 'package:sport/utils/toast.dart';
- import 'package:sport/widgets/dialog/modal_bottom_action.dart';
- import 'package:sport/widgets/dialog/request_dialog.dart';
- import 'package:sport/widgets/loading.dart';
- void open(BuildContext context, final int index, List<photo.Image> images) {
- Navigator.push(
- context,
- FadeRoute(
- page: GalleryPhotoViewWrapper(
- galleryItems: images,
- backgroundDecoration: const BoxDecoration(
- color: Colors.black,
- ),
- initialIndex: index,
- scrollDirection: Axis.horizontal,
- loadingBuilder: (_, __) => RequestLoadingWidget(),
- ),
- ),
- );
- }
- class FadeRoute extends PageRouteBuilder {
- final Widget page;
- FadeRoute({this.page})
- : super(
- pageBuilder: (
- BuildContext context,
- Animation<double> animation,
- Animation<double> secondaryAnimation,
- ) =>
- page,
- transitionsBuilder: (
- BuildContext context,
- Animation<double> animation,
- Animation<double> secondaryAnimation,
- Widget child,
- ) =>
- FadeTransition(
- opacity: animation,
- child: child,
- ),
- );
- }
- class GalleryPhotoViewWrapper extends StatefulWidget {
- GalleryPhotoViewWrapper({
- this.loadingBuilder,
- this.backgroundDecoration,
- this.minScale,
- this.maxScale,
- this.initialIndex,
- @required this.galleryItems,
- this.scrollDirection = Axis.horizontal,
- }) : pageController = PageController(initialPage: initialIndex);
- final LoadingBuilder loadingBuilder;
- final Decoration backgroundDecoration;
- final dynamic minScale;
- final dynamic maxScale;
- final int initialIndex;
- final PageController pageController;
- final List<photo.Image> galleryItems;
- final Axis scrollDirection;
- @override
- State<StatefulWidget> createState() {
- return _GalleryPhotoViewWrapperState();
- }
- }
- class _GalleryPhotoViewWrapperState extends State<GalleryPhotoViewWrapper> {
- int currentIndex;
- @override
- void initState() {
- currentIndex = widget.initialIndex;
- super.initState();
- }
- void onPageChanged(int index) {
- setState(() {
- currentIndex = index;
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: Stack(
- children: <Widget>[
- Container(
- decoration: widget.backgroundDecoration,
- constraints: BoxConstraints.expand(
- height: MediaQuery.of(context).size.height,
- ),
- child: GestureDetector(
- onLongPress: () async {
- await showActionDialog(context, {
- "保存至本地": () async {
- await request(context, () async {
- var file = await save(widget.galleryItems[currentIndex].src);
- if (file != null) {
- ToastUtil.show("已保存图片至:$file");
- }
- });
- }
- });
- },
- child: PhotoViewGallery.builder(
- scrollPhysics: const BouncingScrollPhysics(),
- builder: _buildItem,
- itemCount: widget.galleryItems.length,
- loadingBuilder: widget.loadingBuilder,
- backgroundDecoration: widget.backgroundDecoration,
- pageController: widget.pageController,
- onPageChanged: onPageChanged,
- scrollDirection: widget.scrollDirection,
- ),
- ),
- ),
- Positioned(
- child: SafeArea(
- child: IconButton(
- icon: Image.asset("lib/assets/img/topbar_return_white.png"),
- onPressed: () {
- Navigator.maybePop(context);
- },
- ),
- ))
- ],
- ),
- );
- }
- Future<String> save(String url) async {
- var permission = await Application.requestPermission(Permission.storage);
- if (!permission) {
- ToastUtil.show("没有相应权限!");
- return null;
- }
- var response = await Dio().get(url, options: Options(responseType: ResponseType.bytes));
- final result = await ImageGallerySaver.saveImage(Uint8List.fromList(response.data), quality: 100, name: "shoes_${DateTime.now().millisecondsSinceEpoch}");
- print(result);
- return result;
- }
- PhotoViewGalleryPageOptions _buildItem(BuildContext context, int index) {
- final photo.Image item = widget.galleryItems[index];
- return PhotoViewGalleryPageOptions(
- imageProvider: CachedNetworkImageProvider(item.src),
- initialScale: PhotoViewComputedScale.contained,
- minScale: PhotoViewComputedScale.contained * (0.5 + index / 10),
- heroAttributes: PhotoViewHeroAttributes(tag: item.id ?? item.src),
- onTapUp: (
- _,
- __,
- ___,
- ) {
- Navigator.of(context).pop();
- });
- }
- }
|