// Copyright 2014 The Flutter Authors. All rights reserved. // 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' hide PopScope; import 'package:get/get_core/src/get_main.dart'; import 'package:get/get_navigation/src/extension_navigation.dart'; abstract class PopScopeState extends State implements PopEntry { ModalRoute? _route; @override void onPopInvoked(bool didPop) {} @override late final ValueNotifier canPopNotifier; bool get initCanPop => true; @override void initState() { super.initState(); canPopNotifier = ValueNotifier(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 onPopInvokedWithResult; final bool canPop; @override State createState() => _PopScopeState(); } class _PopScopeState extends PopScopeState { @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; }