fix(app): remove every app-level system Back override
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// A short, Android-only exit window. The second Back is left to the platform,
|
||||
/// including its predictive back-to-home animation, rather than forcing exit.
|
||||
class AndroidRootExit extends NavigatorObserver {
|
||||
AndroidRootExit(this.onChanged);
|
||||
|
||||
final VoidCallback onChanged;
|
||||
static const window = Duration(seconds: 2);
|
||||
Timer? _timer;
|
||||
ScaffoldFeatureController<SnackBar, SnackBarClosedReason>? _hint;
|
||||
bool _disposed = false;
|
||||
|
||||
bool get enabled =>
|
||||
!kIsWeb && defaultTargetPlatform == TargetPlatform.android;
|
||||
bool get armed => _timer?.isActive ?? false;
|
||||
|
||||
bool handleBack() {
|
||||
if (!enabled || armed) return false;
|
||||
_timer = Timer(window, () {
|
||||
_timer = null;
|
||||
onChanged();
|
||||
});
|
||||
final context = navigator?.context;
|
||||
if (context != null) {
|
||||
_hint = ScaffoldMessenger.maybeOf(context)?.showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Press back again to close'),
|
||||
duration: window,
|
||||
),
|
||||
);
|
||||
}
|
||||
onChanged();
|
||||
return true;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
_hint?.close();
|
||||
_hint = null;
|
||||
}
|
||||
|
||||
@override
|
||||
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
|
||||
if (previousRoute == null || !armed) return;
|
||||
// Dialogs and imperative detail routes do not change the URL. Invalidate
|
||||
// their exit window too, outside the Navigator's build/route flush.
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (_disposed) return;
|
||||
clear();
|
||||
onChanged();
|
||||
});
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
_timer?.cancel();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../shell/list_detail.dart';
|
||||
import 'android_root_exit.dart';
|
||||
import 'app_route.dart';
|
||||
|
||||
/// Parses the browser (or platform) location into the app's routes.
|
||||
@@ -46,8 +44,6 @@ String routeLocation(Uri uri) {
|
||||
class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
AppRouterDelegate({
|
||||
required this.builder,
|
||||
required this.onPopToRuns,
|
||||
required this.onPopToChat,
|
||||
required this.cupertino,
|
||||
this.searchBuilder,
|
||||
this.templatesBuilder,
|
||||
@@ -76,17 +72,9 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
/// state and selection, and the standard navigation bar supplies Back.
|
||||
final Widget Function(BuildContext context)? searchBuilder;
|
||||
|
||||
/// Called when a system Back belongs to the Runs detail, not the app stack.
|
||||
final VoidCallback onPopToRuns;
|
||||
|
||||
/// Legacy callback retained for callers; chat Back is owned by its Navigator
|
||||
/// route and never invokes this override.
|
||||
final VoidCallback onPopToChat;
|
||||
|
||||
final bool cupertino;
|
||||
|
||||
final _navigatorKey = GlobalKey<NavigatorState>();
|
||||
late final _rootExit = AndroidRootExit(notifyListeners);
|
||||
static const _pageKey = ValueKey<String>('pipelines-shell');
|
||||
static const _searchPageKey = ValueKey<String>('app-search');
|
||||
static const _templatesPageKey = ValueKey<String>('app-templates');
|
||||
@@ -96,7 +84,6 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
|
||||
/// The destination that was open before Search, restored on close.
|
||||
AppRoute _searchReturn;
|
||||
bool _searchTransient = false;
|
||||
|
||||
/// The Settings route the Templates page closes back to.
|
||||
AppRoute _templatesReturn;
|
||||
@@ -135,13 +122,8 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
/// page key is stable, so changing the route updates it in place instead of
|
||||
/// remounting it and losing the open run. Search is the only extra page, so
|
||||
/// its native Close action pops just that page.
|
||||
/// Whether the window shows an open chat beside its list rather than as a
|
||||
/// pushed page. Read in [build], so a resize rebuilds the Back policy.
|
||||
bool _chatBesideList = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
_chatBesideList = showsListBesideDetail(context);
|
||||
final searching = searchOpen && searchBuilder != null;
|
||||
final templates = templatesOpen && templatesBuilder != null;
|
||||
final schedules = schedulesOpen && schedulesBuilder != null;
|
||||
@@ -152,23 +134,8 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
: _route;
|
||||
return Navigator(
|
||||
key: _navigatorKey,
|
||||
observers: [_rootExit],
|
||||
pages: [
|
||||
// While an item is open inside its tab (a wide screen keeps it
|
||||
// there), Android's Back and back gesture step up a level instead of
|
||||
// leaving the app: the shell tells the system it handles Back.
|
||||
_page(
|
||||
_pageKey,
|
||||
PopScope(
|
||||
canPop: !_canStepUp && !(_isExitRoot && !_rootExit.armed),
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
if (!didPop && !_stepUp() && _isExitRoot) {
|
||||
_rootExit.handleBack();
|
||||
}
|
||||
},
|
||||
child: builder(context, shellRoute),
|
||||
),
|
||||
),
|
||||
_page(_pageKey, builder(context, shellRoute)),
|
||||
// Templates is a Settings sub-page: the shell stays mounted underneath,
|
||||
// so closing restores Settings exactly as it was left.
|
||||
if (templates) _page(_templatesPageKey, templatesBuilder!(context)),
|
||||
@@ -218,114 +185,40 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
if (searchOpen) return;
|
||||
_searchReturn = _route;
|
||||
goTo(AppRoute.search);
|
||||
_searchTransient = true;
|
||||
}
|
||||
|
||||
void closeSearch() {
|
||||
if (!searchOpen) return;
|
||||
_searchTransient = false;
|
||||
goTo(_searchReturn);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setInitialRoutePath(AppRoute configuration) async {
|
||||
_rootExit.clear();
|
||||
_route = configuration;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setNewRoutePath(AppRoute configuration) async {
|
||||
if (configuration == _route) return;
|
||||
_rootExit.clear();
|
||||
_searchTransient = false;
|
||||
_route = configuration;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Back (Android's button, the browser's): close whatever page sits on top
|
||||
/// first, then step up one level — a stage to its run or pipeline, a run,
|
||||
/// pipeline or chat to its list. Android list roots require two presses;
|
||||
/// all other platforms retain their normal Back behavior.
|
||||
/// System Back is the Navigator's own: it pops the page on top (a pushed
|
||||
/// detail, Search, Templates, Schedules or a dialog) and otherwise leaves
|
||||
/// Back to the platform. The app adds no Back behavior of its own.
|
||||
@override
|
||||
Future<bool> popRoute() async {
|
||||
final navigator = _navigatorKey.currentState;
|
||||
if (navigator != null && await navigator.maybePop()) return true;
|
||||
if (searchOpen && (searchBuilder != null || _searchTransient)) {
|
||||
closeSearch();
|
||||
return true;
|
||||
}
|
||||
if (schedulesOpen) {
|
||||
closeSchedules();
|
||||
return true;
|
||||
}
|
||||
if (templatesOpen) {
|
||||
closeTemplates();
|
||||
return true;
|
||||
}
|
||||
if (_stepUp()) return true;
|
||||
if (_isExitRoot) return _rootExit.handleBack();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool get _isExitRoot =>
|
||||
_rootExit.enabled &&
|
||||
// A phone's open chat is a pushed page with stock Back. Beside the list
|
||||
// it is no page at all, so the list root's double-back applies.
|
||||
(_route.chatSessionId == null || _chatBesideList) &&
|
||||
!_canStepUp &&
|
||||
switch (_route.kind) {
|
||||
AppRouteKind.pipelines ||
|
||||
AppRouteKind.runs ||
|
||||
AppRouteKind.chat ||
|
||||
AppRouteKind.settings => true,
|
||||
AppRouteKind.search => searchBuilder == null && !_searchTransient,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
/// Whether an inline run/pipeline detail needs the shell to step up. Chats
|
||||
/// always own a pushed route and use normal Navigator Back instead.
|
||||
bool get _canStepUp =>
|
||||
_route.runId != null ||
|
||||
(_route.kind == AppRouteKind.pipelines && _route.pipelineId != null);
|
||||
|
||||
/// Goes up one level: a stage to its run or pipeline, an item to its list.
|
||||
bool _stepUp() {
|
||||
final runId = _route.runId;
|
||||
if (runId != null && _route.stageKey != null) {
|
||||
goTo(AppRoute.run(runId));
|
||||
return true;
|
||||
}
|
||||
if (runId != null) {
|
||||
onPopToRuns();
|
||||
return true;
|
||||
}
|
||||
final pipelineId = _route.pipelineId;
|
||||
if (_route.kind == AppRouteKind.pipelines && pipelineId != null) {
|
||||
goTo(
|
||||
_route.stageKey != null
|
||||
? AppRoute.pipeline(pipelineId, version: _route.version)
|
||||
: AppRoute.pipelines,
|
||||
);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Future<bool> popRoute() async =>
|
||||
await _navigatorKey.currentState?.maybePop() ?? false;
|
||||
|
||||
/// Records a user move; the Router turns the new configuration into a
|
||||
/// history entry (or a replacement when it already matches the location).
|
||||
void goTo(AppRoute route) {
|
||||
if (route == _route) return;
|
||||
_rootExit.clear();
|
||||
_searchTransient = false;
|
||||
_route = route;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_rootExit.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// The location a launch starts at: the browser location on the web, the
|
||||
|
||||
@@ -177,8 +177,6 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
_delegate = AppRouterDelegate(
|
||||
initial: initial,
|
||||
cupertino: _cupertino,
|
||||
onPopToRuns: () => _delegate.goTo(AppRoute.runs),
|
||||
onPopToChat: () => _delegate.goTo(AppRoute.chat),
|
||||
templatesBuilder: _buildTemplates,
|
||||
schedulesBuilder: _buildSchedules,
|
||||
builder: _buildShell,
|
||||
|
||||
Reference in New Issue
Block a user