fix(app): restore standard Android back navigation
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Asks before Back leaves the app from the top of a tab. True to leave.
|
||||
Future<bool> confirmAppExit(
|
||||
BuildContext context, {
|
||||
required bool cupertino,
|
||||
}) async {
|
||||
const title = Text('Close Pipelines?');
|
||||
final leave = cupertino
|
||||
? await showCupertinoDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => CupertinoAlertDialog(
|
||||
title: title,
|
||||
actions: [
|
||||
CupertinoDialogAction(
|
||||
key: const Key('app-exit-stay'),
|
||||
isDefaultAction: true,
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: const Text('Stay'),
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
key: const Key('app-exit-close'),
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
child: const Text('Close'),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: title,
|
||||
actions: [
|
||||
TextButton(
|
||||
key: const Key('app-exit-stay'),
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: const Text('Stay'),
|
||||
),
|
||||
FilledButton(
|
||||
key: const Key('app-exit-close'),
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
child: const Text('Close'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
return leave ?? false;
|
||||
}
|
||||
@@ -1,9 +1,6 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'app_exit_dialog.dart';
|
||||
import 'app_route.dart';
|
||||
|
||||
/// Parses the browser (or platform) location into the app's routes.
|
||||
@@ -152,9 +149,9 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
_page(
|
||||
_pageKey,
|
||||
PopScope(
|
||||
canPop: !_canStepUp && !_asksBeforeExit,
|
||||
canPop: !_canStepUp,
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
if (!didPop && !_stepUp()) _confirmExit();
|
||||
if (!didPop) _stepUp();
|
||||
},
|
||||
child: builder(context, shellRoute),
|
||||
),
|
||||
@@ -247,26 +244,7 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
return true;
|
||||
}
|
||||
if (_stepUp()) return true;
|
||||
return _confirmExit();
|
||||
}
|
||||
|
||||
/// At the top of a tab Android's Back would leave the app; ask first.
|
||||
/// Elsewhere (a browser) Back keeps its usual meaning.
|
||||
bool get _asksBeforeExit =>
|
||||
!kIsWeb && defaultTargetPlatform == TargetPlatform.android;
|
||||
|
||||
bool _confirming = false;
|
||||
|
||||
bool _confirmExit() {
|
||||
final context = _navigatorKey.currentContext;
|
||||
if (!_asksBeforeExit || context == null) return false;
|
||||
if (_confirming) return true;
|
||||
_confirming = true;
|
||||
confirmAppExit(context, cupertino: cupertino).then((leave) {
|
||||
_confirming = false;
|
||||
if (leave) SystemNavigator.pop();
|
||||
});
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Whether the open route has a level above it in its own tab (a stage,
|
||||
|
||||
@@ -51,12 +51,13 @@ class DetailPageHost {
|
||||
// Pushing during a build is not allowed; the page follows this frame.
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!context.mounted || _route != route) return;
|
||||
Navigator.of(context).push(route).then((_) {
|
||||
Navigator.of(context).push(route);
|
||||
route.completed.then((_) {
|
||||
if (_route != route) return;
|
||||
_route = null;
|
||||
// Rebuild what lies underneath (route, tab bar, list) once the
|
||||
// page has finished sliding away, not during its animation.
|
||||
_afterExit(route, onClosed);
|
||||
// Keep ownership until the route is actually gone. Live updates
|
||||
// during its exit must not push a second copy of the detail page.
|
||||
onClosed();
|
||||
});
|
||||
});
|
||||
} else if (!selected && _route != null) {
|
||||
@@ -68,21 +69,6 @@ class DetailPageHost {
|
||||
}
|
||||
}
|
||||
|
||||
static void _afterExit(Route<void> route, VoidCallback onClosed) {
|
||||
final animation = route is ModalRoute<void> ? route.animation : null;
|
||||
if (animation == null || animation.status == AnimationStatus.dismissed) {
|
||||
onClosed();
|
||||
return;
|
||||
}
|
||||
late final AnimationStatusListener listener;
|
||||
listener = (status) {
|
||||
if (status != AnimationStatus.dismissed) return;
|
||||
animation.removeStatusListener(listener);
|
||||
onClosed();
|
||||
};
|
||||
animation.addStatusListener(listener);
|
||||
}
|
||||
|
||||
/// Removes the page with its owner.
|
||||
void dispose() {
|
||||
// The notifier is left to the page: a page still animating out keeps
|
||||
@@ -95,7 +81,7 @@ class DetailPageHost {
|
||||
|
||||
/// A full-screen page whose bar shows what the page's content publishes
|
||||
/// through [ShellBar] (title and actions), with the platform's Back.
|
||||
Route<void> detailPageRoute(
|
||||
PageRoute<void> detailPageRoute(
|
||||
BuildContext context, {
|
||||
required AppDestination destination,
|
||||
required WidgetBuilder builder,
|
||||
|
||||
@@ -30,12 +30,11 @@ ThemeData appLightTheme() => ThemeData(
|
||||
pageTransitionsTheme: appPageTransitions,
|
||||
);
|
||||
|
||||
/// Pages slide in and out quickly. Android's default fades the whole page,
|
||||
/// which is costly for long pages (a chat) and made Back feel slow; iOS keeps
|
||||
/// its native transition and swipe-back.
|
||||
/// Android and iOS keep their native transitions and interactive back gestures.
|
||||
/// Desktop hosts use the short horizontal slide.
|
||||
const appPageTransitions = PageTransitionsTheme(
|
||||
builders: {
|
||||
TargetPlatform.android: FastSlidePageTransitionsBuilder(),
|
||||
TargetPlatform.android: PredictiveBackPageTransitionsBuilder(),
|
||||
TargetPlatform.fuchsia: FastSlidePageTransitionsBuilder(),
|
||||
TargetPlatform.linux: FastSlidePageTransitionsBuilder(),
|
||||
TargetPlatform.windows: FastSlidePageTransitionsBuilder(),
|
||||
|
||||
Reference in New Issue
Block a user