A new flutter plugin for checking and requesting permissions on iOs and Android.
Make sure you add the needed permissions to your Android Manifest Permission and Info.plist.
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
enum Permission {
// Microphone
RecordAudio,
// Camera
Camera,
// External Storage
WriteExternalStorage,
// Access Coarse Location (Android) / When In Use iOs
AccessCoarseLocation,
// Access Fine Location (Android) / When In Use iOS
AccessFineLocation,
// Access Fine Location (Android) / When In Use iOS
WhenInUseLocation,
// Access Fine Location (Android) / Always Location iOS
AlwaysLocation
}
/// Check a [permission] and return a [Future] with the result
static Future<bool> checkPermission(Permission permission);
/// Request a [permission] and return a [Future] with the result
static Future<bool> requestPermission(Permission permission);
/// Open app settings on Android and iOs
static Future<bool> openSettings();
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:simple_permissions/simple_permissions.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
Permission permission;
@override
initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion = await SimplePermissions.platformVersion;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Plugin example app'),
),
body: new Center(
child: new Column(children: <Widget>[
new Text('Running on: $_platformVersion\n'),
new DropdownButton(items: _getDropDownItems(), value: permission, onChanged: onDropDownChanged),
new RaisedButton(onPressed: checkPermission, child: new Text("Check permission")),
new RaisedButton(onPressed: requestPermission, child: new Text("Request permission")),
new RaisedButton(onPressed: SimplePermissions.openSettings, child: new Text("Open settings"))
]),
),
),
);
}
onDropDownChanged(Permission permission) {
setState(() => this.permission = permission);
print(permission);
}
requestPermission() async {
bool res = await SimplePermissions.requestPermission(permission);
print("permission request result is " + res.toString());
}
checkPermission() async {
bool res = await SimplePermissions.checkPermission(permission);
print("permission is " + res.toString());
}
List<DropdownMenuItem<Permission>>_getDropDownItems() {
List<DropdownMenuItem<Permission>> items = new List();
Permission.values.forEach((permission) {
var item = new DropdownMenuItem(child: new Text(getPermissionString(permission)), value: permission);
items.add(item);
});
return items;
}
}
Add this to your package's pubspec.yaml file:
dependencies:
simple_permissions: ^0.1.1
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.
Now in your Dart code, you can use:
import 'package:simple_permissions/simple_permissions.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.1.9 | Oct 24, 2018 |
|
|
0.1.8 | Sep 24, 2018 |
|
|
0.1.7 | Sep 19, 2018 |
|
|
0.1.6 | Aug 9, 2018 |
|
|
0.1.5 | May 23, 2018 |
|
|
0.1.4 | May 11, 2018 |
|
|
0.1.3 | May 1, 2018 |
|
|
0.1.2 | Apr 13, 2018 |
|
|
0.1.1 | Mar 26, 2018 |
|
|
0.1.0 | Mar 14, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
98
|
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]
|
49
|
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.
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.
Make sure dartdoc
successfully runs on your package's source files. (-10 points)
Running dartdoc
failed with the following output:
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=1.8.0 <2.0.0 |