back_button_interceptor 2.0.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 36

back_button_interceptor #

When you need to intercept the Android back-button, you usually add WillPopScope to your widget tree. However, under some use cases, specially when developing stateful widgets that interact with the back button, it may be more convenient to use the BackButtonInterceptor.

You may add functions to be called when the back button is tapped. These functions may perform some useful work, and then, if any of them return true, the default button process (usually popping a Route) will not be fired.

In more detail: All added functions are called, in order. If any function returns true, the combinedResult is true, and the default button process will NOT be fired. Only if all functions return false (or null), the combinedResult is false, and the default button process will be fired. Each function gets a boolean that indicates the current combinedResult from all the previous functions.

Note: After you've finished you MUST remove each function by calling the remove() method.

Usage #

Import the package #

First, add back_button_interceptor as a dependency in your pubspec.yaml

Then, import it:

import 'package:back_button_interceptor/back_button_interceptor.dart';

Example usage #

@override
void initState() {
   super.initState();
   BackButtonInterceptor.add(myInterceptor);
}

@override
void dispose() {
   BackButtonInterceptor.remove(myInterceptor);
   super.dispose();
}

bool myInterceptor(bool stopDefaultButtonEvent) {
   print("BACK BUTTON!"); // Do some stuff.
   return true;
}

Don't forget to check the example tab.

See also:

[1.0.0] - 02/01/2019

  • Tested thoroughly. Single interceptor.

[2.0.0] - 02/01/2019

  • Multiple interceptors.

example/main.dart

import 'package:back_button_interceptor/back_button_interceptor.dart';
import 'package:flutter/material.dart';

void main() async => runApp(MaterialApp(home: Demo()));

class Demo extends StatefulWidget {
  @override
  DemoState createState() => new DemoState();
}

class DemoState extends State<Demo> {
  //
  @override
  void initState() {
    super.initState();
    BackButtonInterceptor.add(myInterceptor);
  }

  @override
  void dispose() {
    BackButtonInterceptor.remove(myInterceptor);
    super.dispose();
  }

  bool myInterceptor(bool stopDefaultButtonEvent) {
    print("BACK BUTTON!"); // Do some stuff.
    return true;
  }

  @override
  Widget build(BuildContext context) {
    //
    return Scaffold(
      appBar: AppBar(title: const Text('Back Button Interceptor Example')),
      body: Container(color: Colors.green),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  back_button_interceptor: ^2.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:back_button_interceptor/back_button_interceptor.dart';
  
Version Uploaded Documentation Archive
2.0.0 Jan 3, 2019 Go to the documentation of back_button_interceptor 2.0.0 Download back_button_interceptor 2.0.0 archive
1.0.0 Jan 2, 2019 Go to the documentation of back_button_interceptor 1.0.0 Download back_button_interceptor 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
71
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
36
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

Dependencies were not resolved.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <=5.0.0