Create the Plugin

Create by command

  • use--orgOption to specify your organization in reverse domain name notation
flutter create --org com.example --template=plugin 'plugin_name'
Copy the code
  • use-aOptions to specify the Android language, or to use-iOption specifies the language of iOS. By default, the iOS code in the plug-in project is written using Swift and the Android code is written using Kotlin
flutter create --org com.example --template=plugin --platforms=android,ios -i objc -a java plugin 'plugin_name'
Copy the code

For common Flutter commands and shortcuts, see: Flutter- the most common Flutter shortcut

The common problems of upload are listed in package. This article does not list the problems in detail. Please refer to: Development, release and use of Flutter- package

Created through Android Studio

Once created, you can see that it contains native code, unlike the Package

The development of the Plugin

  • I’m just adding a little bit of code here, in this example, to get device version information, and I’m adding to get battery power. In the iOS folderPluginDemoPluginAdd the code
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  } else if ([@"getBatteryLevel" isEqualToString:call.method]) {
      int batteryLevel = [self getBatteryLevel];
      result (@(batteryLevel));
  } else {
    result(FlutterMethodNotImplemented);
  }
}

- (int)getBatteryLevel{
    UIDevice *device = [UIDevice currentDevice];
    if (device.batteryState == UIDeviceBatteryStateUnknown) {
        return -1;
    }
    return (int)device.batteryLevel*100;
}
Copy the code
  • In the engineering oflib->plugin_demo.dartAdd Method to get power. Here,MethodChannelIs created by default
static Future<String? > get batteryLevel async { final String? version = await _channel.invokeMethod('getBatteryLevel'); return version; }Copy the code
  • inexample->lib->main.dartAdd display power inside

Get the battery here by calling plugindemo.batterylevel

Future<void> initBatteryLevel() async { String batteryLevel; try { batteryLevel = await PluginDemo.batteryLevel ?? 'Unknown platform batteryLevel'; } on PlatformException { batteryLevel = 'Failed to get platform batteryLevel.'; } if (! mounted) return; setState(() { _batteryLevel = batteryLevel; }); }Copy the code
@override
Widget build(BuildContext context) {
  return MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Running on: $_platformVersion\n'),
            SizedBox(
              height: 20,
            ),
            Text('Running on: $_batteryLevel\n')
          ],
        ),
      ),
    ),
  );
}
Copy the code

I’m running code because I’m running an emulator and I’m not getting power

Release the Plugin

The publishing process is essentially the same as publishing the Package, as described in this article: Developing, publishing, and using the Package plug-in

  • Check the code first, the license here also needs to be configured
flutter packages pub publish --dry-run
Copy the code

Problems encountered

This error is reported when I did not configure homepage in pubspec.yaml

Once an accessible address is configured, there is no problem

  • Start publishing without problems, the same as publishing the package
flutter packages pub publish --server=https://pub.dartlang.org
Copy the code

Since the name of my project is plugin_demo, those with the same name need to be renamed

I re-created the Flutter_plugin_candy project and uploaded it, and it worked

Common problems associated with upload are listed in package for reference: development, release and use of Flutter- package

Welcome to follow, like and forward.