added checkPermission method

This commit is contained in:
Prerak Mann 2019-05-10 18:24:38 +05:30
parent 59cb4ba91d
commit 6ee7aba035
5 changed files with 90 additions and 8 deletions

13
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,13 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Flutter",
"request": "launch",
"type": "dart"
}
]
}

View File

@ -1,5 +1,16 @@
package com.pkmnapps.nearby_connections;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
@ -10,19 +21,35 @@ import io.flutter.plugin.common.PluginRegistry.Registrar;
* NearbyConnectionsPlugin
*/
public class NearbyConnectionsPlugin implements MethodCallHandler {
private Activity activity;
private NearbyConnectionsPlugin(Activity activity) {
this.activity = activity;
}
/**
* Plugin registration.
*/
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "nearby_connections");
channel.setMethodCallHandler(new NearbyConnectionsPlugin());
channel.setMethodCallHandler(new NearbyConnectionsPlugin(registrar.activity()));
}
@Override
public void onMethodCall(MethodCall call, Result result) {
switch (call.method) {
case "checkPermissions":
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
result.success(false);
} else {
result.success(true);
}
break;
default:
result.notImplemented();
}

View File

@ -19,9 +19,44 @@ class _MyAppState extends State<MyApp> {
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Welcome'),
),
body: Body(),
),
);
}
}
class Body extends StatefulWidget {
@override
_MyBodyState createState() => _MyBodyState();
}
class _MyBodyState extends State<Body> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Center(
child: Column(
children: <Widget>[
RaisedButton(
child: Text("checkPermission"),
onPressed: () async {
if (await Nearby.instance.checkPermissions()) {
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text("yes")));
} else {
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text("No")));
}
},
),
RaisedButton(
child: Text("askPermission(permission handler)"),
onPressed: () async {
// await PermissionHandler()
// .requestPermissions([PermissionGroup.location]);
},
),
],
),
);
}

View File

@ -8,7 +8,7 @@ environment:
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2

View File

@ -2,9 +2,16 @@ import 'dart:async';
import 'package:flutter/services.dart';
class NearbyConnections {
class Nearby {
//for maintaining only 1 instance of this class
static final Nearby _instance = Nearby._();
static Nearby get instance => _instance;
Nearby._();
static const MethodChannel _channel =
const MethodChannel('nearby_connections');
Future<bool> checkPermissions() async => await _channel.invokeMethod(
'checkPermissions',
);
}