feat(platform_interface): create my_plugin_platform_interface (#1)

This commit is contained in:
Felix Angelov
2022-02-14 15:55:48 -06:00
committed by GitHub
parent 2bd229694b
commit 6206c931bd
11 changed files with 193 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
import 'package:my_plugin_platform_interface/src/method_channel_my_plugin.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
/// The interface that implementations of my_plugin must implement.
///
/// Platform implementations should extend this class
/// rather than implement it as `MyPlugin`.
/// Extending this class (using `extends`) ensures that the subclass will get
/// the default implementation, while platform implementations that `implements`
/// this interface will be broken by newly added [MyPluginPlatform] methods.
abstract class MyPluginPlatform extends PlatformInterface {
/// Constructs a MyPluginPlatform.
MyPluginPlatform() : super(token: _token);
static final Object _token = Object();
static MyPluginPlatform _instance = MethodChannelMyPlugin();
/// The default instance of [MyPluginPlatform] to use.
///
/// Defaults to [MethodChannelMyPlugin].
static MyPluginPlatform get instance => _instance;
/// Platform-specific plugins should set this with their own platform-specific
/// class that extends [MyPluginPlatform] when they register themselves.
static set instance(MyPluginPlatform instance) {
PlatformInterface.verify(instance, _token);
_instance = instance;
}
/// Return the current platform name.
Future<String?> getPlatformName();
}

View File

@@ -0,0 +1,15 @@
import 'package:flutter/foundation.dart' show visibleForTesting;
import 'package:flutter/services.dart';
import 'package:my_plugin_platform_interface/my_plugin_platform_interface.dart';
/// An implementation of [MyPluginPlatform] that uses method channels.
class MethodChannelMyPlugin extends MyPluginPlatform {
/// The method channel used to interact with the native platform.
@visibleForTesting
final methodChannel = const MethodChannel('my_plugin');
@override
Future<String?> getPlatformName() {
return methodChannel.invokeMethod<String>('getPlatformName');
}
}