Flutter:无法刷新在SliverAppBarDelegate上拾取的图像

问题描述

我是扑朔迷离的新手,我正在使用下面的代码来拾取和裁剪图像,该应用程序已成功运行,我可以从图库或照相机中拾取和裁剪图像,但是在单击“热重载”按钮之前图像不会刷新在vscode中。

这是代码

import 'dart:io';
import 'package:Flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:image_cropper/image_cropper.dart';
import 'dart:math' as math;

class AddPhoto extends StatefulWidget {
  @override
  _AddPhotoState createState() => _AddPhotoState();
}

class _AddPhotoState extends State<AddPhoto> {
  File _image;
  final picker = ImagePicker();

  @override
  Widget build(BuildContext context) {
    return SliverPersistentHeader(
      pinned: true,floating: true,delegate: _SliverAppBarDelegate(
        _image,minHeight: 80.0,maxHeight: 200.0,onpressed: () => displayBottomSheet(context),),);
  }

  void displayBottomSheet(BuildContext context) {
    showModalBottomSheet(
        context: context,builder: (ctx) {
          return SafeArea(
            child: Container(
              child: Wrap(
                children: <Widget>[
                  ListTile(
                    leading: Icon(Icons.camera_alt_outlined),title: Text('Camera'),onTap: () {
                      _obtenerImagen(ImageSource.camera);
                    },Divider(
                    height: 1,thickness: 0.5,ListTile(
                    leading: Icon(Icons.photo_album_outlined),title: Text('gallery'),onTap: () {
                      _getimage(ImageSource.gallery);
                    },],);
        });
  }


  _getimage(ImageSource source) async {
    final pickedImage = await picker.getimage(source: source);

    if (pickedImage != null) {
      File croppedImage = await ImageCropper.cropImage(
          sourcePath: pickedImage.path,aspectRatio: CropAspectRatio(ratioX: 1,ratioY: 1),compressQuality: 100,maxWidth: 240,maxHeight: 240,compressFormat: ImageCompressFormat.jpg,androidUiSettings: AndroidUiSettings(
              toolbarColor: Colors.deepOrange,toolbarTitle: "Ajustar Imagen",statusBarColor: Colors.deepOrange.shade900,backgroundColor: Colors.white));
      setState(() {
        _image = croppedImage;
      });
    }
    Navigator.pop(context);
  }
}


class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  final File photo;
  final double minHeight;
  final double maxHeight;
  final VoidCallback onpressed;
  final bool hideButtonWhenExpanded;

  _SliverAppBarDelegate(this.photo,{@required this.minHeight,@required this.maxHeight,this.onpressed,this.hideButtonWhenExpanded = true});

  @override
  double get minextent => minHeight;
  @override
  double get maxExtent => math.max(maxHeight,minHeight);
  @override
  Widget build(
      BuildContext context,double shrinkOffset,bool overlapsContent) {
    final appBarSize = maxHeight - shrinkOffset;
    final proportion = 2 - (maxHeight / appBarSize);
    final percent = proportion < 0 || proportion > 1 ? 0.0 : proportion;
    return Flex(
      direction: Axis.vertical,children: <Widget>[
        Flexible(
          child: Container(
            width: MediaQuery.of(context).size.width,color: Colors.white,child: Column(
              mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
                Flexible(
                  flex: 8,child: CircleAvatar(
                    minRadius: 20.0,maxRadius:
                        75.0 * proportion > 20 ? 75.0 * proportion : 20.0,child: Clipoval(
                      child: photo == null
                          ? Image.asset(
                              'assets/images/pan.png',fit: BoxFit.cover,)
                          : Image.file(
                              photo,fit: BoxFit.contain,backgroundColor: Colors.grey[500],Flexible(
                  flex: 2,child: Opacity(
                    opacity: percent,child: FlatButton(
                      onpressed: onpressed,child: Text(
                        'Add Photo',style: TextStyle(
                            color: Colors.blue,fontSize: 14.0 * proportion),Divider(
          height: 1,);
  }

  @override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight ||
           minHeight != oldDelegate.minHeight;
  }
}

我知道有多种方法可以解决此问题,您建议我采用哪种方法,为什么? 谢谢您的帮助

解决方法

从sliverappbardelegate更改此部分:

@override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight ||
           minHeight != oldDelegate.minHeight || photo != oldDelegate.photo;
  }