title: error Looking up a deactivated widget's ancestor is unsafe
body: <p>I don't know what is wrong with this, but when I click on the close icon button I get this error.</p>
  <p>════════ Exception caught by widgets library ═══════════════════════════════════
  The following assertion was thrown while finalizing the widget tree:
  Looking up a deactivated widget's ancestor is unsafe.</p>
  <p>At this point the state of the widget's element tree is no longer stable.</p>
  <p>To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.</p>
  <p>this is my code</p>
  <pre><code>// ...
  class ImageViewer extends StatefulWidget {
    List&lt;String&gt; imageList;
    int currentIndex;
    BoxDecoration backgroundDecoration;
    ImageViewer(
        {Key key,
        this.imageList,
        this.backgroundDecoration,
        this.currentIndex = 0})
        : super(key: key);
  
    @override
    _ImageViewerState createState() =&gt; _ImageViewerState();
  }
  
  class _ImageViewerState extends State&lt;ImageViewer&gt; {
    PageController _pageController;
  
    @override
    void initState() {
      // TODO: implement initState
      _pageController = PageController(initialPage: widget.currentIndex);
      super.initState();
    }
  
    @override
    void dispose() {
      // TODO: implement dispose
      _pageController.dispose();
      super.dispose();
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
          body: ColorfulBackground(
        bottomColor: Colors.lightBlue[400],
        middleColor: Colors.purple,
        body: Stack(children: [
          PhotoViewGallery.builder(
            scrollPhysics: const BouncingScrollPhysics(),
            builder: (BuildContext context, int index) {
              return PhotoViewGalleryPageOptions(
                imageProvider: CachedNetworkImageProvider(
                  widget.imageList[index],
                  cacheManager: CustomCacheManager.instance,
                ),
                tightMode: true,
                maxScale: PhotoViewComputedScale.contained * 2.5,
                gestureDetectorBehavior: HitTestBehavior.translucent,
                minScale: PhotoViewComputedScale.contained * 1.0,
                initialScale: PhotoViewComputedScale.contained * 1.0,
                heroAttributes:
                    PhotoViewHeroAttributes(tag: widget.imageList[index]),
              );
            },
            itemCount: widget.imageList.length,
            backgroundDecoration: BoxDecoration(
              color: Colors.transparent,
            ),
            onPageChanged: (index) {
              setState(() {
                widget.currentIndex = index;
              });
            },
            loadingBuilder: (context, event) =&gt; Center(
              child: Container(
                width: 20.0,
                height: 20.0,
                child: CircularProgressIndicator(
                  value: event == null
                      ? 0
                      : event.cumulativeBytesLoaded / event.expectedTotalBytes,
                ),
              ),
            ),
            // backgroundDecoration: widget.backgroundDecoration,
            pageController: _pageController,
            // onPageChanged: onPageChanged,
          ),
  
          // close icon button //////////////////////////////////
  
          Positioned(
            top: 40,
            left: 10,
            child: IconButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                icon: Icon(
                  Icons.close_rounded,
                  color: kblackcolor,
                )),
          ),
          Positioned(
              right: 40,
              bottom: 100,
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
                decoration: BoxDecoration(
                    color: kPrimaryColor,
                    borderRadius: BorderRadius.circular(20)),
                child: Text(
                  &quot;${widget.currentIndex + 1}/ ${widget.imageList.length}&quot;,
                  style:
                      TextStyle(color: kwhitecolor, fontWeight: FontWeight.w500),
                ),
              )),
        ]),
      ));
    }
  }
  
  
  </code></pre>