1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 22:09:06 +00:00

[federated_plugin] adds web implementation of federated_plugin (#507)

This commit is contained in:
Ayush Bherwani
2020-08-05 11:08:20 +05:30
committed by GitHub
parent 18758bf5af
commit c5c2da9a4b
20 changed files with 678 additions and 52 deletions

View File

@@ -0,0 +1,45 @@
// Copyright 2020 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:html' as html;
import 'package:federated_plugin_platform_interface/federated_plugin_platform_interface.dart';
import 'package:federated_plugin_platform_interface/location_model.dart';
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
/// Web Implementation of [FederatedPluginInterface] to provide location.
class FederatedPlugin extends FederatedPluginInterface {
final html.Geolocation _geolocation;
/// Constructor to override the geolocation object for testing purpose.
FederatedPlugin({html.Geolocation geolocation})
: _geolocation = geolocation ?? html.window.navigator.geolocation;
/// Method to register the plugin which sets [FederatedPlugin] to be the default
/// instance of [FederatedPluginInterface].
static void registerWith(Registrar registrar) {
FederatedPluginInterface.instance = FederatedPlugin();
}
/// Returns [Location] to provide latitude and longitude.
///
/// If any error, it's assume that user has denied the permission forever.
@override
Future<Location> getLocation() async {
try {
final geoPosition = await _geolocation.getCurrentPosition();
return Location(
longitude: geoPosition.coords.longitude.toDouble(),
latitude: geoPosition.coords.latitude.toDouble(),
);
} catch (error) {
throw PlatformException(
code: 'PERMISSION_DENIED',
message: 'Permission denied from User',
details: null,
);
}
}
}