mirror of
https://github.com/flutter/samples.git
synced 2025-11-10 23:08:59 +00:00
[federated_plugin] adds platform interface implementation and plugin implementation for Android (#503)
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
// 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 'package:federated_plugin_platform_interface/location_method_channel.dart';
|
||||
import 'package:federated_plugin_platform_interface/location_model.dart';
|
||||
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
||||
|
||||
/// Interface which allows all the platform plugins to implement the same
|
||||
/// functionality.
|
||||
abstract class FederatedPluginInterface extends PlatformInterface {
|
||||
FederatedPluginInterface() : super(token: _object);
|
||||
|
||||
static FederatedPluginInterface _federatedPluginInterface =
|
||||
LocationMethodChannel();
|
||||
|
||||
static final Object _object = Object();
|
||||
|
||||
/// Provides instance of [LocationMethodChannel] to invoke platform calls.
|
||||
static FederatedPluginInterface get instance => _federatedPluginInterface;
|
||||
|
||||
static set instance(FederatedPluginInterface instance) {
|
||||
PlatformInterface.verifyToken(instance, _object);
|
||||
_federatedPluginInterface = instance;
|
||||
}
|
||||
|
||||
/// Returns [Location] to provide latitude and longitude.
|
||||
Future<Location> getLocation() async {
|
||||
throw UnimplementedError('getLocation() has not been implemented.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// 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 'package:federated_plugin_platform_interface/federated_plugin_platform_interface.dart';
|
||||
import 'package:federated_plugin_platform_interface/location_model.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
/// Implements [FederatedPluginInterface] using [MethodChannel] to fetch
|
||||
/// location from platform.
|
||||
class LocationMethodChannel extends FederatedPluginInterface {
|
||||
static const MethodChannel _methodChannel = MethodChannel('location');
|
||||
|
||||
@override
|
||||
Future<Location> getLocation() async {
|
||||
final result =
|
||||
await _methodChannel.invokeMethod<List<dynamic>>('getLocation');
|
||||
|
||||
return Location(
|
||||
longitude: result.first as double,
|
||||
latitude: result.last as double,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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.
|
||||
|
||||
/// Model to hold the incoming latitude and longitude values from platform.
|
||||
class Location {
|
||||
final double latitude;
|
||||
final double longitude;
|
||||
|
||||
Location({this.latitude, this.longitude});
|
||||
}
|
||||
Reference in New Issue
Block a user