avatarAseem Wangoo

Summary

The web content provides a comprehensive guide on creating and using a Flutter Desktop Plugin, including pre-requisites, step-by-step instructions, and enabling support for Windows.

Abstract

The article titled "Flutter Desktop Plugin" on the undefined website is a detailed resource for developers looking to extend Flutter's capabilities to desktop environments. It begins by acknowledging the growing interest in Flutter for desktop application development and directs readers to a comprehensive guide available at https://flatteredwithflutter.com/flutter-desktop-plugin/. The guide includes an embedded YouTube video for visual assistance, pre-requisite steps for setting up Flutter Desktop, and a structured approach to creating a desktop plugin. Key steps include running a specific command to initialize the plugin, structuring the plugin's code, and implementing the plugin for macOS using Swift. The article also covers how to integrate the newly created plugin into a Flutter desktop application using a FutureBuilder and discusses the current limitations and instability of plugin APIs for Windows. Despite these warnings, the article encourages developers to explore by providing links to necessary files and instructions for configuring the plugin for Windows support. The article concludes with related links to other Flutter Desktop resources and provides access to the source code for both the plugin and desktop application.

Opinions

  • The author expresses enthusiasm for Flutter Desktop development, encouraging readers to explore and create their own plugins.
  • There is an acknowledgment of the experimental nature of Flutter Desktop plugins, particularly for Windows, with a caution against publishing such plugins to pub.dev due to their instability.
  • The article promotes a specific Flutter resource, flatteredwithflutter.com, as an all-in-one solution for Flutter Desktop development, suggesting a preference or endorsement of this resource.
  • The inclusion of an embedded YouTube video and multiple online resources indicates a supportive community and the availability of learning materials for developers interested in Flutter Desktop.
  • The author's provision of step-by-step instructions, code snippets, and links to source code reflects a commitment to guiding developers through the plugin creation process in a hands-on manner.

Flutter Desktop Plugin

Create a Flutter Desktop Plugin!

How to write my own Flutter Desktop Plugin ? Hmmm….

All in one Flutter resource: https://flatteredwithflutter.com/flutter-desktop-plugin/

Pre-Requisite…

In case you are still new to Flutter Desktop and want to set up first, check out this article here…

Begin…

At the end of this article, you should be able to create your plugins for Desktop….

We will structure this article into :

  1. Creating Desktop Plugin
  2. Using Desktop Plugin in Flutter application
  3. Enable support for Windows
Flutter Desktop Plugin

Creating Desktop Plugin

We need to run a command,

flutter create --org com.flatteredwithflutter --template=plugin init_dsktp_plugin

Breakdown :

  • To create a plugin, we need to use --template=plugin flag
  • The name of our plugin will be init_dsktp_plugin
  • Use the --org option to specify your organization, mostly in the reverse order…i.e, com.flatteredwithflutter

After running this command, you should now have a project structure like this,

Flutter Desktop Plugin— Step 1

Here, the folder name is same as you specified during the plugin creation command…Note : Windows folder is added later.

Inside the pubspec.yaml, you can specify the platforms, you want to support…

Inside the lib folder, you can see init_dsktp_plugin.dart. This is the entry point, for outside Flutter application, into our plugin…

We expose a method called platformVersion .

Flutter Desktop Plugin — Impl

The name of our method channel is init_dsktp_plugin and method name is getPlatformVersion . Note: this should be same in the implementation files.

Implementation…

Inside the macOS folder, we see InitDsktpPlugin.swift .. This file is the brain of the plugin…

Steps :

  1. Register the plugin with the method channel as specified in the dart file..(see above)
Register Plugin…

Note: See the method channel is same as dart file one.

2. Implement the method..

Method Implementation…

This function is invoked whenever Flutter application calls a method of our plugin…We can have n number of cases here…

For our plugin, we have just getPlatformVersion . Inside this, we invoke

ProcessInfo.processInfo.operatingSystemVersionString

Now, this ProcessInfo is specific to macOS. Read the doc. here. As per the docs :

operatingSystemVersionString

A string containing the version of the operating system on which the process is executing.

The result is collected and passed via FlutterResult..As per the docs,

FlutterResult : A method call result callback.

Used for submitting a method call result back to a Flutter caller. Also used in the dual capacity for handling a method call result received from Flutter.

Using Desktop Plugin in Flutter application

Lets use the plugin created above in our desktop application…

Steps :

  1. Open the pubspec.yaml of our Flutter Desktop Application, and include the plugin dependency as
...
dependencies:
  init_dsktp_plugin:
    git:
      url: https://github.com/AseemWangoo/plugins.git
      path: ./init_dsktp_plugin
...

By default, it takes the source code from the master branch.

2. Use the plugin, as any other dependency now…

FutureBuilder<String>(
   future: InitDsktpPlugin.platformVersion,
   builder: (context, snapshot) {
     // YOUR LOGIC
   },
)

Enable support for Windows

Warning : The plugin APIs, plugin tooling, and plugin structure for Windows are not at all stable. This means you should not publish Windows plugin implementations to pub.dev as anything published now will almost certainly not work with the final Flutter Windows support.

But we are devz, we like to explore !!!!

Steps :

  1. Download the windows folder from here….or individual files from here
  2. Change the ProjectName in plugin.vcxproj to your plugin's name.

In our case, it’s init_dsktp_plugin

3. Change the ProjectGuid in plugin.vcxproj to a new UUID. Generate online here.

4. Change the FlutterPluginName in PluginInfo.props to your plugin's name.

In our case, it’s init_dsktp_plugin

5. Search for comments containing *** in the .h and .cpp file, and update the code as described in the comment..

In our case, SAMPLE has been replaced by INIT_DSKTP_PLUGIN and “sample_plugin” with “init_dsktp_plugin

Articles related to Flutter Desktop:

Source Code for Plugin…

Source Code for Desktop application…

Flutter
Dart
Desktop App
Software Development
Programming
Recommended from ReadMedium