Skip to main content

pop_back_handler 1.0.1 copy "pop_back_handler: ^1.0.1" to clipboard
pop_back_handler: ^1.0.1 copied to clipboard

A Flutter widget that provides unified handling for back navigation events, integrating PopScope with iOS swipe-back gesture detection.

pop_back_handler #

pub package License: MIT

A Flutter widget that provides unified handling for back navigation events, integrating PopScope with iOS swipe-back gesture detection.

一个 Flutter 小部件,提供统一的返回导航事件处理,并将 PopScope 与 iOS 的滑动返回手势检测功能集成。

Features #

  • ✅ Unified API - Single callback for all back navigation events
  • ✅ Android Support - Intercepts system back button via PopScope
  • ✅ iOS Support - Detects edge swipe gestures when interception is enabled
  • ✅ RTL Support - Fully supports both LTR and RTL layout directions
  • ✅ Configurable - Customizable edge width and swipe threshold
  • ✅ Toggle-able - Easy to enable/disable interception at runtime

Problem Solved #

When using Flutter's PopScope with canPop: false, iOS edge swipe gestures don't trigger the onPopInvokedWithResult callback. This package solves this by adding a GestureDetector layer that detects iOS edge swipes and triggers the same callback.

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  pop_back_handler: ^1.0.0

Then run:

flutter pub get

Usage #

Basic Usage #

Wrap your page content with PopBackHandler and provide an onPopRequested callback:

import 'package:pop_back_handler/pop_back_handler.dart';

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return PopBackHandler(
      canPop: false, // Intercept back events
      onPopRequested: (didPop, result) async {
        // didPop = true: Page was already popped
        // didPop = false: Pop was intercepted, handle it here
        if (!didPop) {
          final shouldPop = await showConfirmDialog(context);
          if (shouldPop && context.mounted) {
            Navigator.pop(context);
          }
        }
      },
      child: Scaffold(
        appBar: AppBar(title: Text('My Page')),
        body: YourContent(),
      ),
    );
  }
}

With AppBar Back Button #

Don't forget to also handle the AppBar's back button:

Future<void> _handleBack() async {
  final shouldPop = await showConfirmDialog(context);
  if (shouldPop && mounted) {
    Navigator.pop(context);
  }
}

PopBackHandler(
  canPop: false,
  onPopRequested: (didPop, result) {
    if (!didPop) {
      _handleBack();
    }
  },
  child: Scaffold(
    appBar: AppBar(
      title: Text('My Page'),
      leading: IconButton(
        icon: Icon(Icons.arrow_back),
        onPressed: _handleBack,  // Use the same handler
      ),
    ),
    body: YourContent(),
  ),
);

Allow Normal Back Navigation #

You can allow normal back navigation by setting canPop: true:

PopBackHandler(
  canPop: true,  // System back gesture works normally (default)
  onPopRequested: (didPop, result) {
    // didPop will be true when page is popped normally
    debugPrint('Page popped: $didPop, result: $result');
  },
  child: YourContent(),
);

Custom Sensitivity #

Adjust the edge detection sensitivity:

PopBackHandler(
  canPop: false,
  edgeWidth: 30.0,        // Edge trigger zone width (default: 20.0)
  swipeThreshold: 80.0,   // Swipe distance to trigger (default: 100.0)
  onPopRequested: (didPop, result) {
    if (!didPop) {
      _handleBack();
    }
  },
  child: YourContent(),
);

API Reference #

PopBackHandler<T> #

Property Type Default Description
child Widget required The widget below this widget in the tree
onPopRequested void Function(bool didPop, T? result) required Callback when back navigation is attempted
canPop bool true Whether the route can be popped. Set to false to intercept back events
edgeWidth double 20.0 iOS edge trigger zone width (in logical pixels)
swipeThreshold double 100.0 iOS swipe distance threshold to trigger back (in logical pixels)

Callback Parameters #

  • didPop: true if the page was already popped, false if the pop was intercepted
  • result: The result value passed when popping (if any)

Example #

See the example directory for a complete sample app.

cd example
flutter run

License #

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing #

Contributions are welcome! Please feel free to submit a Pull Request.

2
likes
150
points
14
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Flutter widget that provides unified handling for back navigation events, integrating PopScope with iOS swipe-back gesture detection.

Homepage
Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter

More

Packages that depend on pop_back_handler