如何在轮播更改时将图像 url 给另一个字符串?

how to give image url to another String when carousel changes?

这可能是个愚蠢的问题,但我想将 image url 从轮播保存到另一个 String pathImage 以将其保存在画廊中。当我滑动到轮播中的下一张图片时,我想将 imagePath 更新为轮播中滑动的 image url。我正在使用 carousel_progallery_saver 包。这是代码:

import 'package:carousel_pro/carousel_pro.dart';
import 'package:flutter/material.dart';
import 'package:pursattm/models/models.dart';
import 'package:pursattm/utils/utils.dart';
import 'package:gallery_saver/gallery_saver.dart';

class DetailsFullImage extends StatefulWidget {
  DetailsFullImage({Key key, this.imagesForCarousel}) : super(key: key);

  final List<Images> imagesForCarousel;
  @override
  _DetailsFullImageState createState() => _DetailsFullImageState();
}

class _DetailsFullImageState extends State<DetailsFullImage> {
  // image download declarations
  String pathImage = '';
  String albumName = 'Media';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: Container(
          height: SizeConfig.sh / 3,
          child: Carousel(
            // onImageChange: () {},
            images: widget.imagesForCarousel.map((image) {
              pathImage = image.url; // declaring image.url to pathImage
              return CachedNetworkImage(
                imageUrl: image.url,
                fit: BoxFit.fill,
                alignment: Alignment.bottomCenter,
                placeholder: (context, url) => Image.asset(
                  'assets/placeholder.png',
                  fit: BoxFit.fill,
                ),
                errorWidget: (context, url, error) {
                  return Center(
                    child: Icon(
                      Icons.refresh,
                      color: Colors.black12,
                    ),
                  );
                },
              );
            }).toList(),
            animationDuration: const Duration(seconds: 1),
            dotSize: 8.0,
            dotSpacing: 20.0,
            autoplay: false,
            autoplayDuration: const Duration(seconds: 4),
            dotColor: Colors.white,
            dotIncreasedColor: AppColors.GREEN_JUST,
            dotIncreaseSize: 2.2,
            dotPosition: DotPosition.bottomLeft,
            indicatorBgPadding: 15,
            dotBgColor: Colors.transparent,
            borderRadius: false,
            // moveIndicatorFromBottom: 250.0,
            noRadiusForIndicator: true,
          ),
        ),
      ),
      floatingActionButton: Builder(
        builder: (BuildContext context) {
          return FloatingActionButton(
            backgroundColor: Colors.white,
            onPressed: () async {
              GallerySaver.saveImage(pathImage, albumName: albumName)
                  .then((bool success) {
                setState(() {
                  Scaffold.of(context).showSnackBar(
                    SnackBar(
                      content: Text('Sucsess!'),
                      backgroundColor: Colors.green,
                      behavior: SnackBarBehavior.floating,
                    ),
                  );
                });
              }).catchError((e) {
                setState(() {
                  Scaffold.of(context).showSnackBar(
                    SnackBar(
                      content: Text('Error!'),
                      backgroundColor: Colors.red,
                      behavior: SnackBarBehavior.floating,
                    ),
                  );
                });
              });
            },
            child: Icon(
              Icons.file_download,
              color: AppColors.GREEN,
              size: 30,
            ),
          );
        },
      ),
    );
  }
}

在这里,当我按下 floatingActionButton 时,它应该将来自轮播的图像路径保存在轮播图像列表中显示的图像上。使用此代码 imagePath 仅从轮播中的图像列表中获取最后 image Url

您可以尝试使用 onImageChange 属性,如下所示:

    onImageChange: (last, current) {
      setState (() {
       pathImage = widget.imagesForCarousel[current].url;
     });
    }

在图像更改事件中,将先前图像索引和当前图像索引作为参数传递。你可以利用它作为优势