Require double back to exit Android tab roots
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
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,6 +1,7 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'android_root_exit.dart';
|
||||
import 'app_route.dart';
|
||||
|
||||
/// Parses the browser (or platform) location into the app's routes.
|
||||
@@ -83,6 +84,7 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
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');
|
||||
@@ -142,6 +144,7 @@ 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
|
||||
@@ -149,9 +152,11 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
_page(
|
||||
_pageKey,
|
||||
PopScope(
|
||||
canPop: !_canStepUp,
|
||||
canPop: !_canStepUp && !(_isExitRoot && !_rootExit.armed),
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
if (!didPop) _stepUp();
|
||||
if (!didPop && !_stepUp() && _isExitRoot) {
|
||||
_rootExit.handleBack();
|
||||
}
|
||||
},
|
||||
child: builder(context, shellRoute),
|
||||
),
|
||||
@@ -214,19 +219,22 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
|
||||
@override
|
||||
Future<void> setInitialRoutePath(AppRoute configuration) async {
|
||||
_rootExit.clear();
|
||||
_route = configuration;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setNewRoutePath(AppRoute configuration) async {
|
||||
if (configuration == _route) return;
|
||||
_rootExit.clear();
|
||||
_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 — and only leave the app from a list.
|
||||
/// pipeline or chat to its list. Android list roots require two presses;
|
||||
/// all other platforms retain their normal Back behavior.
|
||||
@override
|
||||
Future<bool> popRoute() async {
|
||||
final navigator = _navigatorKey.currentState;
|
||||
@@ -244,9 +252,21 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
return true;
|
||||
}
|
||||
if (_stepUp()) return true;
|
||||
if (_isExitRoot) return _rootExit.handleBack();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool get _isExitRoot =>
|
||||
_rootExit.enabled &&
|
||||
!_canStepUp &&
|
||||
switch (_route.kind) {
|
||||
AppRouteKind.pipelines ||
|
||||
AppRouteKind.runs ||
|
||||
AppRouteKind.chat ||
|
||||
AppRouteKind.settings => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
/// Whether the open route has a level above it in its own tab (a stage,
|
||||
/// a run, a pipeline, a chat).
|
||||
bool get _canStepUp =>
|
||||
@@ -285,9 +305,16 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
|
||||
/// history entry (or a replacement when it already matches the location).
|
||||
void goTo(AppRoute route) {
|
||||
if (route == _route) return;
|
||||
_rootExit.clear();
|
||||
_route = route;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_rootExit.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// The location a launch starts at: the browser location on the web, the
|
||||
|
||||
Reference in New Issue
Block a user