Skip to content

Commit 2eb5e48

Browse files
committed
fix: truely handle popping events
1 parent daf6b63 commit 2eb5e48

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

lib/features/home/view/home_page.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import 'package:tsdm_client/utils/logger.dart';
2626
import 'package:tsdm_client/utils/platform.dart';
2727
import 'package:tsdm_client/utils/show_dialog.dart';
2828
import 'package:tsdm_client/utils/show_toast.dart';
29+
import 'package:tsdm_client/widgets/safe_pop_scope.dart';
2930
import 'package:tsdm_client/widgets/shutdown.dart';
3031

3132
const _drawerWidth = 250.0;
@@ -278,7 +279,7 @@ class _HomePageState extends State<HomePage> with LoggerMixin {
278279
// Unreachable
279280
return;
280281
},
281-
child: child,
282+
child: SafePopScope(path: '<Home Page>', child: child),
282283
);
283284
},
284285
),

lib/features/root/bloc/root_location_cubit.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,4 @@ final class RootLocationCubit extends Cubit<RootLocationState> with LoggerMixin
7676
await _sub.cancel();
7777
return super.close();
7878
}
79-
80-
@override
81-
void onChange(Change<RootLocationState> change) {
82-
super.onChange(change);
83-
warning('>>> $change');
84-
}
8579
}

lib/features/root/view/root_page.dart

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
22
import 'package:tsdm_client/features/root/models/models.dart';
33
import 'package:tsdm_client/features/root/stream/root_location_stream.dart';
44
import 'package:tsdm_client/utils/logger.dart';
5-
import 'package:tsdm_client/widgets/shutdown.dart';
5+
import 'package:tsdm_client/widgets/safe_pop_scope.dart';
66

77
/// A top-level wrapper page for showing messages or provide functionalities to
88
/// all pages across the app.
@@ -29,18 +29,6 @@ class _RootPageState extends State<RootPage> with LoggerMixin {
2929

3030
@override
3131
Widget build(BuildContext context) {
32-
return BackButtonListener(
33-
onBackButtonPressed: () async {
34-
// App wide popping events interceptor, handles all popping events and notify the listener above.
35-
rootLocationStream.add(const RootLocationEventLeavingLast());
36-
if (!context.mounted) {
37-
// Well, leave it here.
38-
await exitApp();
39-
return true;
40-
}
41-
return true;
42-
},
43-
child: widget.child,
44-
);
32+
return SafePopScope(path: widget.path, child: widget.child);
4533
}
4634
}

lib/widgets/safe_pop_scope.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:tsdm_client/features/root/models/models.dart';
3+
import 'package:tsdm_client/features/root/stream/root_location_stream.dart';
4+
5+
/// Wrapper widget handle page popping events.
6+
class SafePopScope extends StatelessWidget {
7+
/// Constructor.
8+
const SafePopScope({required this.path, required this.child, super.key});
9+
10+
/// Route path of [child] page.
11+
final String path;
12+
13+
/// Wrapped child widget.
14+
final Widget child;
15+
16+
@override
17+
Widget build(BuildContext context) {
18+
return PopScope(
19+
canPop: false,
20+
onPopInvokedWithResult: (didPop, b) {
21+
if (!didPop) {
22+
// When popping event passed location check, `didPop` is true, do not recursively handle it.
23+
rootLocationStream.add(const RootLocationEventLeavingLast());
24+
}
25+
},
26+
child: child,
27+
);
28+
}
29+
}

0 commit comments

Comments
 (0)