fix: back do android saia da sala em vez de fechar o viewer de imagem
O PopScope sozinho nao resolvia: GoRouterDelegate.popRoute() itera _findCurrentNavigators(), que devolve [root, shell] e retorna states.reversed, ou seja, o shell navigator e consultado primeiro. O viewer e um showDialog no root navigator, entao o shell nunca ve o PopEntry dele, popa a sala e retorna true antes de chegar no dialog. BackButtonListener registra no BackButtonDispatcher raiz, que e consultado antes do RouterDelegate.popRoute(), entao o viewer fecha primeiro. Aplicado tambem no MxcImageViewer (avatar/sticker), que tinha o mesmo defeito.
This commit is contained in:
@@ -199,15 +199,25 @@ class ImageViewerController extends State<ImageViewer> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) => PopScope<Object?>(
|
Widget build(BuildContext context) => BackButtonListener(
|
||||||
// Claim the system back button (and predictive back gesture) for the
|
// A [PopScope] alone is not enough here: go_router's `popRoute()` asks
|
||||||
// viewer. Without this, the request can reach go_router instead, which
|
// `GoRouterDelegate._findCurrentNavigators()`, which returns the shell
|
||||||
// pops the chat room out of the shell and leaves the image on screen.
|
// navigator BEFORE the root one. The viewer is a dialog on the root
|
||||||
|
// navigator, so the shell navigator never sees its [PopEntry], pops the
|
||||||
|
// chat room instead and returns `true`, leaving the image on screen.
|
||||||
|
// A [BackButtonListener] is asked before the router gets the request at
|
||||||
|
// all, so the viewer gets to close itself first.
|
||||||
|
onBackButtonPressed: () async {
|
||||||
|
closeAction();
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
child: PopScope<Object?>(
|
||||||
canPop: false,
|
canPop: false,
|
||||||
onPopInvokedWithResult: (didPop, _) {
|
onPopInvokedWithResult: (didPop, _) {
|
||||||
if (didPop) return;
|
if (didPop) return;
|
||||||
closeAction();
|
closeAction();
|
||||||
},
|
},
|
||||||
child: ImageViewerView(this),
|
child: ImageViewerView(this),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,9 +24,16 @@ class MxcImageViewer extends StatelessWidget {
|
|||||||
backgroundColor: Colors.black.withAlpha(200),
|
backgroundColor: Colors.black.withAlpha(200),
|
||||||
foregroundColor: Colors.white,
|
foregroundColor: Colors.white,
|
||||||
);
|
);
|
||||||
return PopScope<Object?>(
|
return BackButtonListener(
|
||||||
// Claim the system back button for the viewer so that it is dismissed
|
// Same reason as the room image viewer: this dialog lives on the root
|
||||||
// instead of letting the router navigate away underneath it.
|
// navigator, but go_router asks the shell navigator first, so only a
|
||||||
|
// [BackButtonListener] (which runs before the router) can keep the back
|
||||||
|
// button from popping the page underneath.
|
||||||
|
onBackButtonPressed: () async {
|
||||||
|
_close(context);
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
child: PopScope<Object?>(
|
||||||
canPop: false,
|
canPop: false,
|
||||||
onPopInvokedWithResult: (didPop, _) {
|
onPopInvokedWithResult: (didPop, _) {
|
||||||
if (didPop) return;
|
if (didPop) return;
|
||||||
@@ -73,6 +80,7 @@ class MxcImageViewer extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user