Skip to content

PageView seems to ignore PageController's page after state changes #108961

@jfahrenkrug

Description

@jfahrenkrug

Background

I was trying to add "swipe to dismiss" to my easy_image_viewer project. I wrapped a PageView inside of a Dismissible widget in order to change the dismissDirection from down to none when the user has zoomed in to an image (to allow the user to pan around without triggering the dismissal).
However, when changing the dismissDirection, the PageView always jumps back to the first page. I had already filed this here #106708, but I was asked by @maheshmnj to provide a minimal example without any dependencies. Here it is :) Could you take another look @maheshmnj? Thanks!

Steps to Reproduce

  1. Open https://dartpad.dev/?id=3862ceb9c5b74271b99363933e67bdee
  2. Run the sample
  3. Tap on the "Next" button
  4. Tap on the "Change Dismiss Direction" button

Expected results: The app stays on the second page

Actual results: The app jumps back to the first page

Code sample
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'EasyImageViewer Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyPageView(),
    );
  }
}

class MyPageView extends StatefulWidget {
  const MyPageView({Key? key}) : super(key: key);

  @override
  State<MyPageView> createState() => _MyPageViewState();
}

class _MyPageViewState extends State<MyPageView> {
  final PageController _pageController = PageController();
  DismissDirection _dismissDirection = DismissDirection.down;

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: Scaffold(
      body: Dismissible(
        direction: _dismissDirection,
        resizeDuration: null,
        onDismissed: (_) {
          Navigator.of(context).pop();
        },
        key: const Key('dismissable_easy_image_viewer_dialog'),
        child: PageView(
          controller: _pageController,
          children: <Widget>[
            Container(
              color: Colors.red,
              child: Center(
                child: ElevatedButton(
                  onPressed: () {
                    if (_pageController.hasClients) {
                      _pageController.animateToPage(
                        1,
                        duration: const Duration(milliseconds: 400),
                        curve: Curves.easeInOut,
                      );
                    }
                  },
                  child: const Text('Next'),
                ),
              ),
            ),
            Container(
              color: Colors.blue,
              child: Center(
                child: ElevatedButton(
                  onPressed: () {
                    setState(() {
                      _dismissDirection = DismissDirection.none;
                    });
                  },
                  child: const Text('Change Dismiss Direction'),
                ),
              ),
            ),
          ],
        ),
      ),
    ));
  }
}
Details
[✓] Flutter (Channel stable, 3.0.2, on macOS 12.4 21F79 darwin-arm, locale en-US)
    • Flutter version 3.0.2 at /Users/johannes/Code/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision cd41fdd495 (8 weeks ago), 2022-06-08 09:52:13 -0700
    • Engine revision f15f824b57
    • Dart version 2.17.3
    • DevTools version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/johannes/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)

[✓] VS Code (version 1.69.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.46.0

[✓] Connected device (5 available)
    • sdk gphone64 arm64 (mobile)          • emulator-5554                        • android-arm64  • Android 12 (API 32) (emulator)
    • iPod touch (7th generation) (mobile) • 187677E9-0221-44D3-B8AA-A6382CD4FCB5 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-5 (simulator)
    • iPad (9th generation) (mobile)       • 6EEE8A28-98D4-45D9-BB99-2E7AE15EADC2 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-5 (simulator)
    • macOS (desktop)                      • macos                                • darwin-arm64   • macOS 12.4 21F79 darwin-arm
    • Chrome (web)                         • chrome                               • web-javascript • Google Chrome 103.0.5060.134
    ! Error: Johannes' iPad Air is not connected. Xcode will continue when Johannes' iPad Air is connected. (code -13)

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listfound in release: 3.0Found to occur in 3.0found in release: 3.1Found to occur in 3.1frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer version

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions