Coder Social home page Coder Social logo

gogovan / flutter-force-permission Goto Github PK

View Code? Open in Web Editor NEW
4.0 8.0 0.0 134 KB

Show permission disclosure page and allows required permissions and their associated services before the user can proceed.

License: MIT License

Dart 92.78% Shell 0.51% Kotlin 0.26% Ruby 5.67% Swift 0.71% Objective-C 0.07%
flutter permission-requests permissions

flutter-force-permission's Introduction

flutter-force-permission

Build codecov

Show permission disclosure page and allows required permissions and their associated services before the user can proceed.

This package shows a prominent in-app disclosure page for getting permissions as required by Google Play . Also support iOS to ensure a consistent experience.

In addition, permissions and their associated services (e.g. GPS) can be set as "required". If this is set, those required permissions will be required and if users denied it, this package will show a customizable dialog and redirect user to the appropriate settings page provided by the native OS.

Setup

  1. Add the following to pubspec.yaml
dependencies:
  flutter_force_permission: ^0.1.0
  # Currently this package depends on our `flutter-permission-handler` package to fix an iOS issue.
  # Directly depends on our packages to avoid any pubspec dependency resolving failure.
  # Track the PR at: https://github.com/Baseflow/flutter-permission-handler/pull/967
  # TODO replace once we upload our packages and our PR merged by Baseflow.
  permission_handler:
     git:
        url: https://github.com/gogovan/flutter-permission-handler.git
        ref: master
        path: permission_handler
  permission_handler_apple:
     git:
        url: https://github.com/gogovan/flutter-permission-handler.git
        ref: master
        path: permission_handler_apple

# TODO replace once we upload our packages and our PR merged by Baseflow.
dependency_overrides:
   permission_handler_apple:
      git:
         url: https://github.com/gogovan/flutter-permission-handler.git
         ref: master
         path: permission_handler_apple
  1. This package depends on permission_handler. Perform setup according to that package.
  2. On Android, if you use POST_NOTIFICATIONS permission, update the targetSdkVersion in build.gradle to at least 33 so that the permission request dialog is shown correctly. Refer to relevant Android Developer page for details.
android {
    // ...
    defaultConfig {
        compileSdkVersion 33
        targetSdkVersion 33
        // ...
    }
    // ...
}
  1. If any features is required, it is highly recommended to also set the <uses-feature> tag in AndroidManifest.xml. Refer to relevant Android Developers page for details.

Usage

  1. Create an instance of FlutterForcePermission, providing configuration. Refer to documentation of [FlutterForcePermissionConfig] for configuration details. Use a single instance of FlutterForcePermission throughout your app.
final perm = FlutterForcePermission(
  FlutterForcePermissionConfig(
    title: 'Title',
    permissionItemConfigs: [
      PermissionItemConfig(
        permissions: [Permission.locationWhenInUse],
        required: PermissionRequiredOption.required,
        itemText: PermissionItemText(
          header: 'Foreground Location',
          rationaleText: 'Rationale for Foreground location. Required.',
          forcedPermissionDialogConfig: ForcedPermissionDialogConfig(
            title: 'Please enable location permission',
            text: 'Please enable location permission for proper usage.',
            buttonText: 'Settings',
          ),
        ),
      ),
      PermissionItemConfig(
        permissions: [Permission.locationAlways],
        itemText: PermissionItemText(
          header: 'Background Location',
          rationaleText: 'Rationale for Background location. lorem ipsum dolor sit amet.',
        ),
      ),
    ],
  ),
);
  1. Show the disclosure page as needed. This method will handle showing the disclosure page and requesting permissions. This function takes a BuildContext. This is an async function. Wrap the function in an async block as needed. Returns a map of permission and their requested status ( granted/denied/etc), service status and whether they are requested by this plugin.
final result = await perm.show(context);

Styling

You can set the styles by providing a ThemeData in the configuration.

  • elevatedButtonTheme.style is used for the primary button.
  • primaryColor is used for as the color of the icons.
  • Title uses titleLarge text style.
  • Item header use titleMedium text style.
  • Item body use bodyMedium text style.

Advanced Usage

Customize the required permission denied prompt

If you wish to customize the dialog shown when the required permission is denied, provide a showDialogCallback which to show your dialog. Parameters are included for you to compose the appropriate dialog. In your callback, you SHOULD:

  1. Display a non-dismissable dialog. This can be typically achieved by setting barrierDismissible to false and provide an empty callback e.g. (() async => false) to willPopCallback for your dialog.
  2. Call the provided callback parameter in your callback when the user click the confirm button, and dismiss your dialog by Navigator.pop.
final config = FlutterForcePermissionConfig(
  title: 'Title',
  confirmText: 'Confirm',
  permissionItemConfigs: [
    PermissionItemConfig(
      permissions: [
        Permission.location,
      ],
      itemText: PermissionItemText(
        header: 'Foreground location',
        rationaleText: 'Rationale',
        forcedPermissionDialogConfig: ForcedPermissionDialogConfig(
          title: 'Location required',
          text: 'Location needed for proper operation',
          buttonText: 'Settings',
        ),
      ),
      required: PermissionRequiredOption.required,
    ),
  ],
  showDialogCallback: (context, option, permConfig, callback) {
    // Show your dialog.
    showDialog(context: context,
      barrierDismissible: false,
      builder: (context) =>
          WillPopScope(
            onWillPop: () async => false, // Prevent dismissing dialog by tapping outside the dialog or the back button.
            child: AlertDialog(
              title: Text(permConfig.forcedPermissionDialogConfig.title),
              content: Text(permConfig.forcedPermissionDialogConfig.text),
              actions: [
                TextButton(
                  onPressed: () {
                    // Remember to invoke `callback` after confirm action to show the OS settings page as appropriate. 
                    callback();
                    // !IMPORTANT!: You MUST use the BuildContext provided by the dialog when using the navigator, NOT the BuildContext provided by the callback.
                    // Failure of doing so will result in unintended behavior.
                    Navigator.pop(context);
                  },
                  child: Text(permConfig.forcedPermissionDialogConfig.buttonText),
                ),
              ],
            ),
          ),
    );
  },
);

Known Issues

  • Currently it depends on our fork of flutter-permission-handler instead of the original to fix an issue for iOS. You may track the issue and pull request here.

Issues

Contributing

flutter-force-permission's People

Contributors

gogox-mobile-team-bot avatar luiges90 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.