Giter Site home page Giter Site logo

marcglasberg / back_button_interceptor Goto Github PK

View Code? Open in Web Editor NEW
105.0 105.0 21.0 156 KB

Flutter Package: May be used to intercept the Android back-button, as an alternative to `WillPopScope`.

License: BSD 2-Clause "Simplified" License

Dart 96.54% Java 1.13% Objective-C 2.33%

back_button_interceptor's People

Contributors

marcglasberg avatar noga-dev avatar rodrigobastosv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

back_button_interceptor's Issues

Doesn't work with iOS swipe

The package description makes no mention of handling the iOS back swipe. It would be appreciated to update the package description to explicitly state that the iOS swipe is not supported (I had to download and test the sample in the iOS simulator to confirm). It would also be helpful if the description described whether iOS support is planned for a future release.

Not working with Android 13

On android 13 the interceptor doesn't seem to work, backwards isn't really recognised.... this doesn't happen with android 12

If a keyboard is open back_button_inceptor doesn't recognize the event

First of all, thanks for this package.

Before I implemented the package, the back button closed the whole page when the keyboard was open.

Now I implemented the example code, now the keyboard gets closed when the back button is pressed. If it's pressed once more only then myInterceptor gets called (I am checking this with print statements)

Any idea why this might be the case or how I can change that? Thanks

A null exception occures during debug run in this package

Steps to Reproduce

  1. Install flutter 2.10.5
  2. Install Android SDK 29
  3. Clone medito-app
  4. Install packages with pub
  5. Start android emulator or connect phone
  6. Run project in debug mode
    Expected results: The app up and running in the emulator or phone

Actual results:
A null exception during build

Logs

Launching lib/main.dart on Android SDK built for x86 64 in debug mode... The app could not be configured for release signing. In app purchases will not be testable. See example/README.md` for more info and instructions.
: Error: Property 'handlePopRoute' cannot be accessed on 'WidgetsBinding?' because it is potentially null.

  • 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../../Documents/flutter/packages/flutter/lib/src/widgets/binding.dart').

Try accessing using ?. instead.
WidgetsBinding.instance.handlePopRoute;
^^^^^^^^^^^^^^
: Error: Property 'handlePushRoute' cannot be accessed on 'WidgetsBinding?' because it is potentially null.

  • 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../../../Documents/flutter/packages/flutter/lib/src/widgets/binding.dart').
    Try accessing using ?. instead.
    WidgetsBinding.instance.handlePushRoute as Future Function(String?);
    ^^^^^^^^^^^^^^^

FAILURE: Build failed with an exception.

  • Where:
    Script '/Users/atharvajagtap/Documents/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 1102
  • What went wrong:
    Execution failed for task ':app:compileFlutterBuildDebug'.

Process 'command '/Users/atharvajagtap/Documents/flutter/bin/flutter'' finished with non-zero exit value 1

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
  • Get more help at https://help.gradle.org/

BUILD FAILED in 1m 42s
Exception: Gradle task assembleDebug failed with exit code 1
Exited (sigterm)`

   info • Unnecessary cast • lib/audioplayer/download_class.dart:83:38 • unnecessary_cast
   info • 'accentColor' is deprecated and shouldn't be used. Use colorScheme.secondary instead. For more information, consult
          the migration guide at
          https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties#migration-guide. This feature was
          deprecated after v2.3.0-0.1.pre. • lib/main.dart:111:11 • deprecated_member_use
   info • This class (or a class that this class inherits from) is marked as '@immutable', but one or more of its instance
          fields aren't final: ApiResponse.status, ApiResponse.body, ApiResponse.message • lib/network/api_response.dart:18:7 •
          must_be_immutable
   info • 'data' is deprecated and shouldn't be used. Use values below • lib/network/folder/folder_response.dart:21:23 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use values below • lib/network/folder/folder_response.dart:23:23 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use values below • lib/network/folder/folder_response.dart:25:29 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use values below • lib/network/folder/folder_response.dart:26:24 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use values below • lib/network/folder/folder_response.dart:27:36 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use values below • lib/network/folder/folder_response.dart:28:23 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use values below • lib/network/folder/folder_response.dart:31:20 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use values below • lib/network/folder/folder_response.dart:33:27 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use values below • lib/network/folder/folder_response.dart:38:5 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use values below • lib/network/folder/folder_response.dart:44:27 •
          deprecated_member_use_from_same_package
   info • 'idInt' is deprecated and shouldn't be used. Use id instead • lib/network/folder/folder_response.dart:110:20 •
          deprecated_member_use_from_same_package
   info • 'idInt' is deprecated and shouldn't be used. Use id instead • lib/network/folder/folder_response.dart:131:5 •
          deprecated_member_use_from_same_package
   info • 'idInt' is deprecated and shouldn't be used. Use id instead • lib/network/folder/folder_response.dart:140:18 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. User title and body instead •
          lib/network/home/daily_message_response.dart:5:23 • deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. User title and body instead •
          lib/network/home/daily_message_response.dart:7:22 • deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. User title and body instead •
          lib/network/home/daily_message_response.dart:12:5 • deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. User title and body instead •
          lib/network/home/daily_message_response.dart:17:14 • deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. User title and body instead •
          lib/network/home/daily_message_response.dart:18:27 • deprecated_member_use_from_same_package
   info • Unused import: 'package:connectivity/connectivity.dart' • lib/network/home/home_bloc.dart:22:8 • unused_import
   info • 'data' is deprecated and shouldn't be used. Use fields instead • lib/network/packs/announcement_response.dart:5:22 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use fields instead • lib/network/packs/announcement_response.dart:7:30 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use fields instead • lib/network/packs/announcement_response.dart:9:22 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use fields instead • lib/network/packs/announcement_response.dart:11:29 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use fields instead • lib/network/packs/announcement_response.dart:13:28 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use fields instead • lib/network/packs/announcement_response.dart:15:28 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use fields instead • lib/network/packs/announcement_response.dart:17:27 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use fields instead • lib/network/packs/announcement_response.dart:19:20 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use fields instead • lib/network/packs/announcement_response.dart:24:5 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. Use fields instead • lib/network/packs/announcement_response.dart:30:27 •
          deprecated_member_use_from_same_package
   info • 'coverOld' is deprecated and shouldn't be used. Use cover instead • lib/network/packs/packs_response.dart:47:23 •
          deprecated_member_use_from_same_package
   info • 'backgroundImage' is deprecated and shouldn't be used. Use backgroundImageUrl •
          lib/network/packs/packs_response.dart:51:36 • deprecated_member_use_from_same_package
   info • 'backgroundImage' is deprecated and shouldn't be used. Use backgroundImageUrl •
          lib/network/packs/packs_response.dart:82:5 • deprecated_member_use_from_same_package
   info • 'coverOld' is deprecated and shouldn't be used. Use cover instead • lib/network/packs/packs_response.dart:85:5 •
          deprecated_member_use_from_same_package
   info • 'backgroundImage' is deprecated and shouldn't be used. Use backgroundImageUrl •
          lib/network/packs/packs_response.dart:94:32 • deprecated_member_use_from_same_package
   info • 'coverOld' is deprecated and shouldn't be used. Use cover instead • lib/network/packs/packs_response.dart:97:21 •
          deprecated_member_use_from_same_package
   info • The value of the field '_screen' isn't used • lib/network/session_options/session_options_bloc.dart:37:10 •
          unused_field
   info • The value of the local variable 'options' isn't used • lib/network/session_options/session_options_bloc.dart:110:9 •
          unused_local_variable
   info • 'backgroundImage' is deprecated and shouldn't be used. Use backgroundImageUrl instead •
          lib/network/session_options/session_opts.dart:30:36 • deprecated_member_use_from_same_package
   info • 'author' is deprecated and shouldn't be used. use attribution instead •
          lib/network/session_options/session_opts.dart:33:29 • deprecated_member_use_from_same_package
   info • 'audio' is deprecated and shouldn't be used. use files instead • lib/network/session_options/session_opts.dart:34:32
          • deprecated_member_use_from_same_package
   info • 'backgroundImage' is deprecated and shouldn't be used. Use backgroundImageUrl instead •
          lib/network/session_options/session_opts.dart:64:5 • deprecated_member_use_from_same_package
   info • 'author' is deprecated and shouldn't be used. use attribution instead •
          lib/network/session_options/session_opts.dart:67:5 • deprecated_member_use_from_same_package
   info • 'audio' is deprecated and shouldn't be used. use files instead • lib/network/session_options/session_opts.dart:70:7 •
          deprecated_member_use_from_same_package
   info • 'audio' is deprecated and shouldn't be used. use files instead • lib/network/session_options/session_opts.dart:72:9 •
          deprecated_member_use_from_same_package
   info • 'backgroundImage' is deprecated and shouldn't be used. Use backgroundImageUrl instead •
          lib/network/session_options/session_opts.dart:85:32 • deprecated_member_use_from_same_package
   info • 'author' is deprecated and shouldn't be used. use attribution instead •
          lib/network/session_options/session_opts.dart:89:9 • deprecated_member_use_from_same_package
   info • 'author' is deprecated and shouldn't be used. use attribution instead •
          lib/network/session_options/session_opts.dart:90:24 • deprecated_member_use_from_same_package
   info • 'audio' is deprecated and shouldn't be used. use files instead • lib/network/session_options/session_opts.dart:92:9 •
          deprecated_member_use_from_same_package
   info • 'audio' is deprecated and shouldn't be used. use files instead • lib/network/session_options/session_opts.dart:93:23
          • deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. use fields instead • lib/network/text/text_response.dart:4:17 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. use fields instead • lib/network/text/text_response.dart:5:23 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. use fields instead • lib/network/text/text_response.dart:6:26 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. use fields instead • lib/network/text/text_response.dart:7:22 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. use fields instead • lib/network/text/text_response.dart:13:5 •
          deprecated_member_use_from_same_package
   info • 'data' is deprecated and shouldn't be used. use fields instead • lib/network/text/text_response.dart:19:27 •
          deprecated_member_use_from_same_package
   info • Unused import: 'package:package_info/package_info.dart' • lib/tracking/tracking.dart:20:8 • unused_import
   info • Unused import: 'package:Medito/tracking/tracking.dart' • lib/utils/stats_utils.dart:17:8 • unused_import
   info • 'await' applied to 'void', which is not a 'Future' • lib/utils/stats_utils.dart:304:7 • await_only_futures
   info • The import of 'package:flutter/cupertino.dart' is unnecessary because all of the used elements are also provided by
          the import of 'package:flutter/material.dart' • lib/utils/text_themes.dart:1:8 • unnecessary_import
   info • The import of 'package:flutter/widgets.dart' is unnecessary because all of the used elements are also provided by the
          import of 'package:flutter/material.dart' • lib/utils/text_themes.dart:3:8 • unnecessary_import
   info • 'canLaunch' is deprecated and shouldn't be used. Use canLaunchUrl instead • lib/utils/utils.dart:95:18 •
          deprecated_member_use
   info • 'launch' is deprecated and shouldn't be used. Use launchUrl instead • lib/utils/utils.dart:96:17 •
          deprecated_member_use
   info • 'canLaunch' is deprecated and shouldn't be used. Use canLaunchUrl instead • lib/utils/utils.dart:121:13 •
          deprecated_member_use
   info • 'launch' is deprecated and shouldn't be used. Use launchUrl instead • lib/utils/utils.dart:122:11 •
          deprecated_member_use
   info • The import of 'package:flutter/cupertino.dart' is unnecessary because all of the used elements are also provided by
          the import of 'package:flutter/material.dart' • lib/widgets/empty_widget.dart:3:8 • unnecessary_import
   info • The import of 'package:flutter/foundation.dart' is unnecessary because all of the used elements are also provided by
          the import of 'package:flutter/material.dart' • lib/widgets/folders/folder_nav_widget.dart:27:8 • unnecessary_import
   info • The import of 'package:flutter/cupertino.dart' is unnecessary because all of the used elements are also provided by
          the import of 'package:flutter/material.dart' • lib/widgets/home/daily_message_item_widget.dart:5:8 •
          unnecessary_import
   info • The import of 'package:flutter/cupertino.dart' is unnecessary because all of the used elements are also provided by
          the import of 'package:flutter/material.dart' • lib/widgets/home/small_shortcuts_row_widget.dart:7:8 •
          unnecessary_import
   info • 'accentColor' is deprecated and shouldn't be used. Use colorScheme.secondary instead. For more information, consult
          the migration guide at
          https://flutter.dev/docs/release/breaking-changes/theme-data-accent-properties#migration-guide. This feature was
          deprecated after v2.3.0-0.1.pre. • lib/widgets/home/stats_widget.dart:175:15 • deprecated_member_use
   info • The import of 'package:flutter/cupertino.dart' is unnecessary because all of the used elements are also provided by
          the import of 'package:flutter/material.dart' • lib/widgets/home/streak_tile_widget.dart:18:8 • unnecessary_import
   info • The import of 'package:flutter/widgets.dart' is unnecessary because all of the used elements are also provided by the
          import of 'package:flutter/material.dart' • lib/widgets/home/streak_tile_widget.dart:20:8 • unnecessary_import
   info • 'backwardsCompatibility' is deprecated and shouldn't be used. This property is obsolete and is false by default. This
          feature was deprecated after v2.4.0-0.0.pre. • lib/widgets/main/app_bar_widget.dart:45:9 • deprecated_member_use
   info • 'brightness' is deprecated and shouldn't be used. This property is no longer used, please use systemOverlayStyle
          instead. This feature was deprecated after v2.4.0-0.0.pre. • lib/widgets/main/app_bar_widget.dart:46:9 •
          deprecated_member_use
   info • The import of 'package:flutter/cupertino.dart' is unnecessary because all of the used elements are also provided by
          the import of 'package:flutter/material.dart' • lib/widgets/packs/announcement_banner_widget.dart:21:8 •
          unnecessary_import
   info • The import of 'package:flutter/cupertino.dart' is unnecessary because all of the used elements are also provided by
          the import of 'package:flutter/material.dart' • lib/widgets/packs/error_widget.dart:19:8 • unnecessary_import
   info • Unused import: '../../main.dart' • lib/widgets/player/background_sounds_sheet_widget.dart:32:8 • unused_import
   info • The import of 'package:flutter/cupertino.dart' is unnecessary because all of the used elements are also provided by
          the import of 'package:flutter/material.dart' • lib/widgets/player/player2/player_widget_2.dart:34:8 •
          unnecessary_import
   info • The import of 'package:flutter/rendering.dart' is unnecessary because all of the used elements are also provided by
          the import of 'package:flutter/cupertino.dart' • lib/widgets/player/player2/player_widget_2.dart:36:8 •
          unnecessary_import
   info • The declaration '_getLoadingScreenWidget' isn't referenced • lib/widgets/player/player2/player_widget_2.dart:286:10 •
          unused_element
   info • Unused import: 'package:Medito/widgets/home/daily_message_item_widget.dart' •
          test/widgets/home/courses_row_item_widget_test.dart:3:8 • unused_import

83 issues found. (ran in 7.3s)

Flutter doctor -v

[✓] Flutter (Channel stable, 2.10.5, on macOS 12.3.1 21E258 darwin-x64, locale en-IN)
    • Flutter version 2.10.5 at /Users/atharvajagtap/fvm/versions/2.10.5
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5464c5bac7 (4 weeks ago), 2022-04-18 09:55:37 -0700
    • Engine revision 57d3bac3dd
    • Dart version 2.16.2
    • DevTools version 2.9.2

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/atharvajagtap/Library/Android
    • Platform android-31, build-tools 29.0.2
    • ANDROID_HOME = /Users/atharvajagtap/Library/Android
    • Java binary at: /Library/Java/JavaVirtualMachines/jdk1.8.0_333.jdk/Contents/Home/bin/java
    • Java version Java(TM) SE Runtime Environment (build 1.8.0_333-b02)
    • All Android licenses accepted.

[✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
      Download at: https://developer.apple.com/xcode/download/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods installed but not working.
        You appear to have CocoaPods installed but it is not working.
        This can happen if the version of Ruby that CocoaPods was installed with is different from the one being used to invoke
        it.
        This can usually be fixed by re-installing CocoaPods.
      To re-install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).

[✓] VS Code (version 1.67.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.40.0

[✓] Connected device (1 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 101.0.4951.64

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 2 categories.

Browser back button doesn't work

myInterceptor() is never called when I press the web browser's back button in my Flutter PWA. Is this intended? Does this only work for Android device back button? Is there any other way to every intercept browser back button? WillPopScope solution doesn't get called either.

Thanks!

ifRouteChanged() context parameter problem

I am trying to implement your lib but I am facing a problem when I am trying to check back button behavior when I push a modal (CustomDialog).

I have two BackButtonInterceptor implemented, one in my main.dart and one in another class :

BackButtonInterceptor: Custom, z-index: null (ifNotYetIntercepted: false).
BackButtonInterceptor: GlobalBackButtonInterceptor, z-index: null (ifNotYetIntercepted: false).

I/flutter (19197): The BackButtonInterceptor threw an ERROR: Assertion failed: "The ifRouteChanged() method can only be called if the context parameter was passed to the BackButtonInterceptor.add() method.".
E/flutter (19197): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Assertion failed: "The ifRouteChanged() method can only be called if the context parameter was passed to the BackButtonInterceptor.add() method."
E/flutter (19197): #0      BackButtonInterceptor.errorProcessing.<anonymous closure>.<anonymous closure> (package:back_button_interceptor/src/back_button_interceptor.dart:22:38)
E/flutter (19197): #1      new Future.delayed.<anonymous closure> (dart:async/future.dart:326:39)
E/flutter (19197): #2      _rootRun (dart:async/zone.dart:1182:47)
E/flutter (19197): #3      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (19197): #4      _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (19197): #5      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (19197): #6      _rootRun (dart:async/zone.dart:1190:13)
E/flutter (19197): #7      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (19197): #8      _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter (19197): #9      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter (19197): #10     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter (19197): #11     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter (19197): #12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

I tried your example main_complex_example and it's working.

That's my code :

class CustomPage extends StatefulWidget {
  @override
  _CustomPageState createState() => _CustomPageState();
}

class _CustomPageState extends State<CustomPage> {
  @override
  void initState() {
    super.initState();
    BackButtonInterceptor.add(myInterceptor, name: "Custom", context: context);
  }

  bool myInterceptor(bool stopDefaultButtonEvent, RouteInfo info) {
    print(BackButtonInterceptor.describe());
    print(stopDefaultButtonEvent);
    if (stopDefaultButtonEvent) return false;
    if (info.ifRouteChanged(context)) return false;
    return true;
  }

  @override
  void dispose() {
    BackButtonInterceptor.removeByName("Custom");
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text("Custom"),
        ),
        body: Container(
            child: IconButton(
          icon: Icon(
            Icons.share,
            color: Colors.blueGrey[500],
            size: 30,
          ),
          onPressed: () => Navigator.of(context).push(PageRouteBuilder(
              barrierDismissible: true, opaque: false, 
              pageBuilder: (context, _, __) => CustomDialog(...)),
        )));
  }
}

Flutter doctor -v :

[✓] Flutter (Channel stable, 1.22.0, on Mac OS X 10.15.6 19G2021, locale fr-FR)
    • Flutter version 1.22.0 at /Users/******/Workspace/flutter
    • Framework revision d408d302e2 (7 days ago), 2020-09-29 11:49:17 -0700
    • Engine revision 5babba6c4d
    • Dart version 2.10.0

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at /Users/*******/Library/Android/sdk
    • Platform android-30, build-tools 30.0.1
    • ANDROID_HOME = /Users/*****/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/*****/Library/Android/sdk
    • Java binary at: /Users/******/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/193.6626763/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 12.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.0.1, Build version 12A7300
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Users/*******/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/193.6626763/Android
      Studio.app/Contents
    • Flutter plugin version 50.0.1
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

 
[✓] Connected device (1 available)            
    • MI PAD 4 (mobile) • 5a95aa78 • android-arm64 • Android 9 (API 28)

• No issues found!

Null check operator used on a null value

E/flutter (28395): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value
E/flutter (28395): #0      BackButtonInterceptor._errorProcessing.<anonymous closure> (package:back_button_interceptor/src/back_button_interceptor.dart:26:44)
E/flutter (28395): #1      new Future.delayed.<anonymous closure> (dart:async/future.dart:427:39)
E/flutter (28395): #2      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter (28395): #3      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
E/flutter (28395): #4      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)
E/flutter (28395): #5      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:189:12)
E/flutter (28395): 

back_button_interceptor: ^6.0.2

doctor

[√] Flutter (Channel stable, 3.10.6, on Microsoft Windows [版本 10.0.22621.2134], locale zh-CN)
    • Flutter version 3.10.6 on channel stable at D:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision f468f3366c (4 weeks ago), 2023-07-12 15:19:05 -0700
    • Engine revision cdbeda788a
    • Dart version 3.0.6
    • DevTools version 2.23.1
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at C:\Users\LongPC\AppData\Local\Android\Sdk
    • Platform android-33, build-tools 33.0.1
    • Java binary at: D:\IntelliJ IDEA 2023.1.4\jbr\bin\java
    • Java version OpenJDK Runtime Environment JBR-17.0.7+7-1000.6-jcef (build 17.0.7+7-b1000.6)
    • All Android licenses accepted.

[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.5.3)
    • Visual Studio at D:\Microsoft Visual Studio
    • Visual Studio Community 2022 version 17.5.33516.290
    • Windows 10 SDK version 10.0.22000.0

[√] Android Studio (version 2022.1)
    • Android Studio at D:\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-9505619)

[√] IntelliJ IDEA Ultimate Edition (version 2022.2)
    • IntelliJ at D:\IntelliJ IDEA 2022.2.2
    • Flutter plugin version 72.1.3
    • Dart plugin version 222.4560

[√] IntelliJ IDEA Ultimate Edition (version 2023.2)
    • IntelliJ at D:\IntelliJ IDEA 2023.1.4
    • Flutter plugin version 74.0.5
    • Dart plugin version 232.8660.129

[√] Connected device (3 available)
    • Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 10 (API 29) (emulator)
    • Windows (desktop)                  • windows       • windows-x64    • Microsoft Windows [版本 10.0.22621.2134]
    • Edge (web)                         • edge          • web-javascript • Microsoft Edge 115.0.1901.200

ifRouteChanged() not working with unnamed routes (Navigator 2.0)

I was trying to use if (info.ifRouteChanged(context)) return false; to avoid running the interceptor while an alert dialog is being displayed and it's not working (Always returning false & opening a new AlertDialog). I'm using unnamed routes / Navigator 2.0.

v6.0.1 back button interception issues

Hi,

following #21 I migrated the application to null-safety and upgraded to the last version of flutter and the package version but the issue still continues. It looks like the interceptor function is being executed, but the navigation pop is not being intercepted.

Here are some details:

YAML config:

environment:
  sdk: '>=2.17.0 <=3.0.4'

dependencies:
  # some additional packages
  back_button_interceptor: ^6.0.1

Flutter Doctor:

[√] Flutter (Channel stable, 3.0.4, on Microsoft Windows [Versi¢n 10.0.19044.1806], locale es-ES)
    • Flutter version 3.0.4 at C:\DEVELOPMENT\SRC\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 85684f9300 (11 days ago), 2022-06-30 13:22:47 -0700
    • Engine revision 6ba2af10bb
    • Dart version 2.17.5
    • DevTools version 2.12.2

My interceptor implementation:

  // Some code
  BackButtonInterceptor.add(myInterceptor, context: this.context);
  // Some more code

  bool myInterceptor(bool stopDefaultButtonEvent, RouteInfo info) {
    if (info.ifRouteChanged(context)) {
      Navigator.of(context).pop();
      return true;
    }
    if (state.updatedSchedule) {
      add(ScheduleEditGoBackNeedConfirmation());
    } else {
      add(ScheduleEditGoBack());
    }
    return true;
  }

  @override
  Future<void> close() async {
    BackButtonInterceptor.remove(myInterceptor);
    super.close();
  }

The navigation works correctly when executed through the in-app nav buttons. The myInterceptor function is being executed as expected, but the navigation pop is not intercepted (it's executed anyways)

Does this work with the material AppBar back button?

Hi there,

These library works wonderfully with the native Android back button, however, I'd also like the same behaviour when pressing the AppBar back button (from the flutter/material.dart package) and by default it doesn't appear to work. Is there a way to enable this behaviour?

Cheers,
George

Flutter 3 WidgetsBinding Issue

#16

For now add this to pubspec;

dependency_overrides:
  back_button_interceptor:
    git:
      url: https://github.com/noga-dev/back_button_interceptor.git
      ref: a1f3031251ea86978f21a4d86db92da0700568dd

Async interceptor

Currently it is impossible to have an async logic for the interceptor

Not working as described

If any function returns true, the combined result is true

I have the following case which is not working as described:

I have two widgets: MyScreen and CustomAppBar.
In both widgets I initialize an interceptor.
MyScreen has a Scaffold with a custom app bar widget I've made: CustomAppBar.

CustomAppBar:

  bool _interceptor(bool stopDefaultButtonEvent, RouteInfo info) {
    print('Custom AppBar');
    return true;
  }

MyScreen:

  bool _interceptor(bool stopDefaultButtonEvent, RouteInfo info) {
    print('My Screen');
    return false;
  }

In this case, the default back button behavior IS fired, and the route is being popped.

And the console looks like this:

Custom AppBar
My Screen

If I return true in MyScreen too, then it is not fired.
I tried using z-index, same result.

Version 4.4.0

_interceptor is being called before closing dialogs

I have a screen where I set an interceptor in initState like so:
BackButtonInterceptor.add(_interceptor);

If I open a dialog [showDialog(...)], and press the "back" button,
I want the dialog to close and not fire the _interceptor method.

Currently the _interceptor is being called first,
and only after the second "back", the dialog will be closed.

Of course I can set a flag that indicates if a dialog is opened, and make sure to set it for all dialogs on open&close, but I feel like a default behavior should be closing dialogs first.

Snackbar is closed instead of going back

Hi @marcglasberg ,

Thanks for the great package.

I am facing one issue though. I am implementing double tap back button functionality in which first I am showing snackbar and on second tap I need to close the APP( or route to the previous screen if any). But, on the second tap when I am return false, it is just closing the snackbar.

Can you please help with this?

Thanks.

Conflict with go_router deep links

I think is the same issue as in #23 , except in this case the navigation in prevented when invoking a deep link when the app is open and using the go_router package as described here

If you don't intend on fixing this, I could look into it. But at least a warning should be put on the README. I spent two days debugging this issue 🙃

Debug mode / Release APK

Hi,

In debug mode, using VC Studio, the plugin is working as expected: the function is running when the back button is pressed.
But when I build the app, the function is not running anymore. (Back Button is disabled, but it doesn't launch the function onPressed)

I tried to flutter clean and create a new build but I got the same issue. Some special permission is needed in the Manifest?

Suspected conflict with flow_builder

We are using the flow_builder package which also uses the SystemChannels.navigation.setMethodCallHandler();. In addition we use the loader_overlay package, which uses this package.

As soon as a BackButtonInterceptor is added, the flow_builder package stops to register android back button navigations.

This is how flow_builder works with the SystemChannels.navigation:

image

The flow builder calls that class in initState
image

Based on documentation of SystemChannels.navigation.setMethodCallHandler, the one replaces the other:
image

version 5.1.0 compile fail

console message

../../development/flutter/.pub-cache/hosted/pub.dartlang.org/back_button_interceptor-5.1.0/lib/src/back_button_interceptor.dart:30:31: Error: Property 'handlePopRoute' cannot be accessed on 'WidgetsBinding?' because it is potentially null.
     - 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('../../development/flutter/packages/flutter/lib/src/widgets/binding.dart').
    Try accessing using ?. instead.
          WidgetsBinding.instance.handlePopRoute;
                                  ^^^^^^^^^^^^^^

WidgetsBinding.instance is null safety

Trouble running multiples tests that use the BackButtonInterceptor

While running multiple tests that use the interceptor i got this error:

The BackButtonInterceptor threw an ERROR: 'package:provider/src/provider.dart': Failed assertion: line 189 pos 12: 'context != null': is not true..
Pending timers:
Timer (duration: 0:00:00.000000, periodic: false), created:
#0      new _FakeTimer._ (package:quiver/testing/src/async/fake_async.dart:277:42)
#6      BackButtonInterceptor.errorProcessing.<anonymous closure> (package:back_button_interceptor/src/back_button_interceptor.dart:22:12)
#7      BackButtonInterceptor.popRoute (package:back_button_interceptor/src/back_button_interceptor.dart:138:24)
#8      main.<anonymous closure>.<anonymous closure> (file:///home/rodrigo/Documentos/projects/polis/test/page/user_following_politics_page_test.dart:60:35)
#21     AutomatedTestWidgetsFlutterBinding.runTest.<anonymous closure> (package:flutter_test/src/binding.dart:1099:17)
#22     AutomatedTestWidgetsFlutterBinding.runTest.<anonymous closure> (package:flutter_test/src/binding.dart:1087:35)
(elided 32 frames from class _FakeAsync, dart:async, and package:stack_trace)

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
A Timer is still pending even after the widget tree was disposed.
'package:flutter_test/src/binding.dart':
Failed assertion: line 1119 pos 12: '() {
      if (   _currentFakeAsync.periodicTimerCount == 0
          && _currentFakeAsync.nonPeriodicTimerCount == 0) {
        return true;
      }

      debugPrint('Pending timers:');
      for (final String timerInfo in _currentFakeAsync.pendingTimersDebugInfo) {
        final int firstLineEnd = timerInfo.indexOf('\n');
        assert(firstLineEnd != -1);

        // No need to include the newline.
        final String firstLine = timerInfo.substring(0, firstLineEnd);
        final String stackTrace = timerInfo.substring(firstLineEnd + 1);

        debugPrint(firstLine);
        debugPrintStack(stackTrace: StackTrace.fromString(stackTrace));
        debugPrint('');
      }
      return false;
    }()'

When the exception was thrown, this was the stack:
#2      AutomatedTestWidgetsFlutterBinding._verifyInvariants (package:flutter_test/src/binding.dart:1119:12)
#3      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:716:7)
<asynchronous suspension>
#6      TestWidgetsFlutterBinding._runTest (package:flutter_test/src/binding.dart:683:14)
#7      AutomatedTestWidgetsFlutterBinding.runTest.<anonymous closure> (package:flutter_test/src/binding.dart:1083:24)
#13     AutomatedTestWidgetsFlutterBinding.runTest (package:flutter_test/src/binding.dart:1080:15)
#14     testWidgets.<anonymous closure> (package:flutter_test/src/widget_tester.dart:133:24)
#15     Declarer.test.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart:172:27)
<asynchronous suspension>
#16     Declarer.test.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart)
#17     Invoker.waitForOutstandingCallbacks.<anonymous closure> (package:test_api/src/backend/invoker.dart:246:15)
#22     Invoker.waitForOutstandingCallbacks (package:test_api/src/backend/invoker.dart:243:5)
#23     Declarer.test.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart:170:33)
#28     Declarer.test.<anonymous closure> (package:test_api/src/backend/declarer.dart:169:13)
#29     Invoker._onRun.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/invoker.dart:400:30)
(elided 33 frames from class _AssertionError, class _FakeAsync, class _RawReceivePortImpl, class _Timer, dart:async, dart:async-patch, and package:stack_trace)

Typo in the year of latest (7.0.2) version date

Affect: CHANGELOG.md

Issue: Typo in the year of the date, is 2023 and should be 2024.

Description: Latest available version at the moment is 7.0.2, and has date of 2023/02/07, I assume its a typo and should be 2024/02/07, specially since the former version has date [7.0.1] - 2023/08/10.

Note: Or it's a typo, or you guys discover time travel, so pls, share your knowledge with us 😄

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.