mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-04-20 03:06:59 +08:00
@@ -2,10 +2,12 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/material.dart' hide PopScope;
|
||||
import 'package:get/get_core/src/get_main.dart';
|
||||
import 'package:get/get_navigation/src/extension_navigation.dart';
|
||||
|
||||
abstract class PopScopeState<T extends StatefulWidget> extends State<T>
|
||||
implements PopEntry<T> {
|
||||
implements PopEntry<Object> {
|
||||
ModalRoute<dynamic>? _route;
|
||||
|
||||
@override
|
||||
@@ -14,31 +16,60 @@ abstract class PopScopeState<T extends StatefulWidget> extends State<T>
|
||||
@override
|
||||
late final ValueNotifier<bool> canPopNotifier;
|
||||
|
||||
void initCanPopNotifier() {
|
||||
canPopNotifier = ValueNotifier<bool>(false);
|
||||
}
|
||||
bool get initCanPop => true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
initCanPopNotifier();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
final ModalRoute<dynamic>? nextRoute = ModalRoute.of(context);
|
||||
if (nextRoute != _route) {
|
||||
_route?.unregisterPopEntry(this);
|
||||
_route = nextRoute;
|
||||
_route?.registerPopEntry(this);
|
||||
}
|
||||
canPopNotifier = ValueNotifier<bool>(initCanPop);
|
||||
_route = (Get.routing.route as ModalRoute)..registerPopEntry(this);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_route?.unregisterPopEntry(this);
|
||||
_route = null;
|
||||
canPopNotifier.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
// ignore: camel_case_types
|
||||
typedef popScope = PopScope;
|
||||
|
||||
class PopScope extends StatefulWidget {
|
||||
const PopScope({
|
||||
super.key,
|
||||
required this.child,
|
||||
this.canPop = true,
|
||||
required this.onPopInvokedWithResult,
|
||||
});
|
||||
|
||||
final Widget child;
|
||||
|
||||
final PopInvokedWithResultCallback<Object> onPopInvokedWithResult;
|
||||
|
||||
final bool canPop;
|
||||
|
||||
@override
|
||||
State<PopScope> createState() => _PopScopeState();
|
||||
}
|
||||
|
||||
class _PopScopeState<T extends PopScope> extends PopScopeState<T> {
|
||||
@override
|
||||
bool get initCanPop => widget.canPop;
|
||||
|
||||
@override
|
||||
void onPopInvokedWithResult(bool didPop, Object? result) {
|
||||
widget.onPopInvokedWithResult(didPop, result);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(T oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
canPopNotifier.value = widget.canPop;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => widget.child;
|
||||
}
|
||||
|
||||
@@ -3,11 +3,11 @@ import 'package:flutter/material.dart';
|
||||
class KeepAliveWrapper extends StatefulWidget {
|
||||
const KeepAliveWrapper({
|
||||
super.key,
|
||||
required this.builder,
|
||||
required this.child,
|
||||
this.wantKeepAlive = true,
|
||||
});
|
||||
|
||||
final WidgetBuilder builder;
|
||||
final Widget child;
|
||||
final bool wantKeepAlive;
|
||||
|
||||
@override
|
||||
@@ -19,7 +19,7 @@ class _KeepAliveWrapperState extends State<KeepAliveWrapper>
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
super.build(context);
|
||||
return widget.builder(context);
|
||||
return widget.child;
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
21
lib/common/widgets/route_aware_mixin.dart
Normal file
21
lib/common/widgets/route_aware_mixin.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:get/get_core/src/get_main.dart';
|
||||
import 'package:get/get_navigation/src/extension_navigation.dart';
|
||||
import 'package:get/get_navigation/src/routes/default_route.dart'
|
||||
show GetPageRoute;
|
||||
|
||||
final routeObserver = RouteObserver<GetPageRoute>();
|
||||
|
||||
mixin RouteAwareMixin<T extends StatefulWidget> on State<T>, RouteAware {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
routeObserver.subscribe(this, Get.routing.route as GetPageRoute);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
routeObserver.unsubscribe(this);
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user