Photo_view: [BUG] `PhotoViewCore` throws when changing pages while zoomed in

Created on 25 Nov 2019  Â·  5Comments  Â·  Source: fireslime/photo_view

If you create a PhotoViewGallery and pass a PhotoViewScaleStateController in the PhotoViewGalleryPageOptions defined at the builder, the error The method 'toDouble' was called on null is thrown by PhotoViewCore when changing pages while zoomed in.

You can reproduce this issue by running the code below, zooming in and changing to the second page without zooming out:

import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(body: Foo()),
    );
  }
}

class Foo extends StatefulWidget {
  @override
  _FooState createState() => _FooState();
}

class _FooState extends State<Foo> {
  static const images = <String>[
    'https://homepages.cae.wisc.edu/~ece533/images/peppers.png',
    'https://homepages.cae.wisc.edu/~ece533/images/baboon.png',
    'https://homepages.cae.wisc.edu/~ece533/images/girl.png',
  ];

  PhotoViewScaleStateController scaleStateController;

  @override
  void initState() {
    super.initState();
    scaleStateController = PhotoViewScaleStateController();
  }

  @override
  void dispose() {
    scaleStateController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return PhotoViewGallery.builder(
      itemCount: images.length,
      builder: (context, index) => PhotoViewGalleryPageOptions(
        imageProvider: NetworkImage(images[index]),
        scaleStateController: scaleStateController,
      ),
    );
  }
}

Not passing scaleStateController makes the issue disappear, so it's definitely related.

Flutter details:

• Flutter version 1.9.1+hotfix.5 at C:\flutter
• Framework revision 1aedbb1835 (6 weeks ago), 2019-10-17 08:37:27 -0700
• Engine revision b863200c37
• Dart version 2.5.0

I believe this is related to the release 0.8.0.

Gallery Priority bug

All 5 comments

This only happens when a scaleStateController with a value different than initial is passed to a brand new PhotoView (every time user changes a page in the gallery, a new PhotoView is created). Fix should arrive on 0.9.1.

Got it. I believe that PhotoViewScaleState should be reset to initial when changing the page. Previously, it was only possible to change the page when the state was initial? This would explain why no concerns were given to this matter.

I believe that PhotoViewScaleState should be reset to initial when changing the page.

Two things that make that impossible:

  • We can have at some times two (or more) pages (instances of PhotoView) ate the screen. By the time the page has fully changed, the widget had been instantiated already.
  • We are passing controllers to page options, in theory, the gallery doesn't know the existence of the controller.

Anyway, throwing is never an option, this must be fixed.

A drastic solution is to make the gallery receive the controllers, not pages. But with this approach, package user will not be able to eventually pass different controllers to different pages.

A drastic solution is to make the gallery receive the controllers, not pages. But with this approach, package user will not be able to eventually pass different controllers to different pages.

Generally, a single controller for each feature is held by the state. From my point of view, to pass a different controller to different pages, the user must have lots of scale-state-controllers in the same state, which is unusual.

Can you think on a practical usage of such feature?

I can't picture a use case right now but having and application where there are a known amount of pages and the user has different (but exposed) states for each page doesnt sound like something impossible.

Was this page helpful?
0 / 5 - 0 ratings