1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-10 23:08:59 +00:00

[federated_plugin_sample] modify the sample to expose battery API instead of geolocation API (#526)

This commit is contained in:
Ayush Bherwani
2020-08-25 11:52:42 +05:30
committed by GitHub
parent 033ae11733
commit 60691d00c0
17 changed files with 116 additions and 211 deletions

View File

@@ -0,0 +1,17 @@
// 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:flutter/services.dart';
/// Implements [FederatedPluginInterface] using [MethodChannel] to fetch
/// battery level from platform.
class BatteryMethodChannel extends FederatedPluginInterface {
static const MethodChannel _methodChannel = MethodChannel('battery');
@override
Future<int> getBatteryLevel() async {
return await _methodChannel.invokeMethod<int>('getBatteryLevel');
}
}

View File

@@ -2,8 +2,7 @@
// 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:federated_plugin_platform_interface/battery_method_channel.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
/// Interface which allows all the platform plugins to implement the same
@@ -12,11 +11,11 @@ abstract class FederatedPluginInterface extends PlatformInterface {
FederatedPluginInterface() : super(token: _object);
static FederatedPluginInterface _federatedPluginInterface =
LocationMethodChannel();
BatteryMethodChannel();
static final Object _object = Object();
/// Provides instance of [LocationMethodChannel] to invoke platform calls.
/// Provides instance of [BatteryMethodChannel] to invoke platform calls.
static FederatedPluginInterface get instance => _federatedPluginInterface;
static set instance(FederatedPluginInterface instance) {
@@ -24,8 +23,8 @@ abstract class FederatedPluginInterface extends PlatformInterface {
_federatedPluginInterface = instance;
}
/// Returns [Location] to provide latitude and longitude.
Future<Location> getLocation() async {
throw UnimplementedError('getLocation() has not been implemented.');
/// Returns the current battery level of device.
Future<int> getBatteryLevel() async {
throw UnimplementedError('getBatteryLevel() has not been implemented.');
}
}

View File

@@ -1,24 +0,0 @@
// 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,
);
}
}

View File

@@ -1,11 +0,0 @@
// 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});
}