-
-
Notifications
You must be signed in to change notification settings - Fork 211
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
(android only) PdfView becomes blank on device rotation (portrait -> landscape) #9
Comments
Same Issue any solution? |
This happens with the example posted with the package.
|
Hello guys, this is a limitation from a native plugin. |
It works if on 'didChangeMetrics' we redefine a new PDFViewController (no more final) and force redrawing the PDFView. |
@egodefroy post some example code here. Thanks. |
Could you provide an example, removing final solved another issue for me that caused a blank screen but implementing the didChangeMetrics method hasn't solved the rotation issue. (I'm also experiencing the issue whenever the keyboard is displayed).
EDIT: Adding "resizeToAvoidBottomPadding : false" to the scaffold solved the blank screen when keypad appears issue. |
I get the same error. Here is the log:
Here is the repo: https://github.com/iampawan/FlutterPDFViewer |
i get the same error, |
My work around (not really a work around) is to disable screen rotation whilst viewing PDFs
However, according to some users on StackOverflow this doesn't work on iPads https://stackoverflow.com/a/50322184/469335 |
My work around (not very beautiful...) is based on reinstanciating the controller in didChangeMetrics.
and use a Timer in build to force redraw the pdf (code to adapt to your special case)
|
it will be very nice if @endigo fix this issue |
do you know any pdf viewer out there that is more maintained than this one. |
You shouldn't expect this automatically from this plugin, the underlying viewer clearly says it's the responsibility of the user to redraw with the current page stored earlier. So, adding a new channel method to the controller helps us replenish the view:
(Note that there is a fresh regression [https://github.com/flutter/flutter/issues/33866] that causes these calls to drop java.lang.IllegalStateException: Reply already submitted errors into logcat, it will be fixed in a coming Flutter release). And then:
Keep track of |
That's how I solved the problem. I check when the app rotation changes, and I replace the current pdf viewer by a new one. You can also save the current page and redirect the viewer to that page once the viewer has been rebuilt. This is useful when the viewer page is a separated widget. Hope it helped! class PdfViewerPage extends StatefulWidget {
_PdfViewerPageState createState() => new _PdfViewerPageState();
}
class _PdfViewerPageState extends State<PdfViewerPage> {
Orientation _lastScreenOrientation;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
_lastScreenOrientation = MediaQuery.of(context).orientation;
});
}
@override
Widget build(BuildContext context) {
// We need to erase and rebuild the viewer when the user changes the screen orientation.
if (_lastScreenOrientation != null && _lastScreenOrientation != MediaQuery.of(context).orientation) {
// Completely render the page.
Future.delayed(Duration(microseconds: 100), _repushViewer);
}
return Scaffold(
resizeToAvoidBottomPadding : false,
body: PDFView(
filePath: 'YOUR PDF FILE PATH.',
onError: (e) {},
)
);
}
/// Push and replace the viewer page so the page is completely new.
_repushViewer() {
Navigator.of(context).pushReplacementNamed(/*YOUR PDF VIEWER PAGE ROUTE */);
}
} |
Facing the same issue around here... |
Initially, I thought of not using it but worked around on @GENL's comment. Look like we got it working except for changing the default page to the last page before rotation. |
How can we redirect the viewer to the current page once the viewer has been rebuilt? Appreciate any idea around it. |
@zaheddec hello, you can use setPage function.
|
@endigo Thanks for the replay. I tried to play around with it but could not get any success with it.
would really appreciate any idea where to place it. Thanks again Note: I called the setpage() in onredender() and it all worked. |
Nice to have helped |
My workaround with the help of @GENL comment.
but i still don't know why controller.setPage(currPage) not working |
because, your setting page in a wrong way... |
well you can try like this |
I suggest a combination of my previous suggestion and the latest ones. No need to track the orientation, or to use an The only thing needed is:
and to use the The only remaining problem is the slight black flicker when the |
@deakjahn |
Yep, and also similar to my first idea from last July. That's why I said combination. :-) |
@akshayyadav76 thanks for your solution it work perfectly for me ,but for fit policy i used the default one ( didn't use this option at all ) and it looks great @deakjahn i tried your solution but it didn't work for me, as soon i change the orientation the pages doesn't show up |
@redbayoub I modified the suggestion, try that. It seems that the rest is also needed to make |
I am trying to implement the solution suggested by @deakjahn - this in null safety. I have a problem with this line:
I am getting the following error: The method 'addObserver' can't be unconditionally invoked because the receiver can be null. The didChangeMetrics method is never called when going from portrait to landscape. |
If you're positive that it'll never be null, you use a !:
If you want to leave it open to not to crash if it is, you use a ?:
But this isn't PdfView-specific, just the new null-safe Dart, so it might be best for you to read it up. :-)) |
Hello there, I am trying to implement your method since I modify my pdf viewer page from stateless to stateful. Or could you making a example for this great method for avoid pdf runs away once android is rotated. Thanks for help !! |
Just declare it inside your state. I modified the original post with that line. You don't have to do any more than what's in that snippet. On metrics change, a new uniqe key will be created, and this will force Flutter to create a new PDF viewer instead of reusing the previous one. Flutter automatically rebuilds any widget when the key changes (state is actually linked to the key inside Flutter, so a new key means a new state). |
I tried my best but i did not figure it out, |
@deakjahn, I am not able to understand where to write reload() function?
|
I no longer use endigo's plugin because I needed more and different features (eg. a way to show page pairs at will) but as far as I can remember, I had another suggestion later: #9 (comment) These said there's no need for that |
this forces flutter to rebuild the widget, so it works
|
This will help https://www.youtube.com/watch?v=gAUVz0U7eyA |
In my case, my PdfView becomes blank when the system puts the app in the background or returns the app to the foreground. To resolve that I add
|
(android only) PdfView becomes blank on device rotation (portrait -> landscape). Does not reappear when the device is rotated back to portrait.
This does not seem to happen when PdfView is in a SizedBox with limited height.
The text was updated successfully, but these errors were encountered: