-
Notifications
You must be signed in to change notification settings - Fork 27.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PageView seems to ignore PageController's page after state changes #108961
Comments
I figured out my mistake by looking at the Dismissible widget's source code:
none , the parent changes because the child is returned directly instead of being wrapped in a GestureDetector . So adding a GlobalKey to the PageView solves the problem. Re-watching this video helped a lot: https://www.youtube.com/watch?v=kn0EOS-ZiIc
|
Thanks for reporting this. |
@xu-baolin I agree, it's definitely unexpected. It could probably be solved by always returning the same hierarchy: The child being wrapped in a |
Reproducible using the code sample provided above. videoSimulator.Screen.Recording.-.iPhone.13.Pro.-.2022-08-04.at.12.48.21.mp4code sampleimport '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(() {
if(_dismissDirection == DismissDirection.none) {
_dismissDirection = DismissDirection.down;
return;
}
_dismissDirection = DismissDirection.none;
});
},
child: const Text('Change Dismiss Direction'),
),
),
),
],
),
),
),
);
}
}
flutter doctor -v
|
A ticket asked for swipe-to-dismiss (#14) It's easy to implement and now supported with the `swipeDismissible` argument. This allows the user to drag the dialog down to dismiss it. Initially there was a problem when the user zoomed in and tried to pan around on the zoomed-in image: The `Dismissible` widget's GestureDetector would take over and enter the dismissal animation. Watching the image's scale and then changing the Dismissible's `dismissDirection` from `down` to `none` solves the issue. However, additionally a `GlobalObjectKey` was needed for the PageView because Dismissible changes the location of the widget in the widget tree when its `dismissDirection` is changed to `none`. See https://github.com/flutter/flutter/blob/2aa348b9407e96ffe4eca8e8f213c7984afad3f7/packages/flutter/lib/src/widgets/dismissible.dart#L692 See flutter/flutter#108961 See https://www.youtube.com/watch?v=kn0EOS-ZiIc
@xu-baolin Thanks so much for fixing this! |
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
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 thedismissDirection
fromdown
tonone
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
Expected results: The app stays on the second page
Actual results: The app jumps back to the first page
Code sample
The text was updated successfully, but these errors were encountered: