Flutter for Desktop

Flutter for Desktop was originally a project created to develop Flutter embeddings for Windows, macOS, and Linux, and this work later became part of Flutter. The project is currently only building a sample and test environment for Flutter applications using these libraries in their current state. It also includes some experimental early desktop plug-ins.

Flutter for Desktop is still in its early stages. The code for this project is unstable and not suitable for production use.

Desktop shell

Flutter Team is working on extending Flutter support to the desktop as a goal, allowing developers to create macOS, Windows and Linux applications using Flutter.

In the long run, a fully integrated solution will be created for desktop Flutter creation, Flutter running, and Flutter construction, but the project is still ongoing.

Current status

The Flutter desktop API is still in the early stages of development and is subject to change without notice. Nor does it provide backward compatibility with the API or ABI. After any Flutter update, any code using these libraries needs to be updated and recompiled.

macOS

This is the most mature desktop platform (for a variety of reasons, including how close it is to the already supported iOS).

Classes that start with Flutter share with iOS and should be basically stable. The class beginning with FLE is still in its early stages.

Windows

The current Windows shell is a GLFW placeholder for early experimentation. It will be replaced by Win32 or UWP shell in the future, allowing the inclusion of Flutter views in applications. The API of the final shell will be completely different from the current implementation.

Linux

The current Linux shell is a GLFW placeholder for early experimentation. The Flutter Team plans to create a library that allows you to embed Flutter, whether using GTK +, Qt, wxWidgets, Motif or any other toolkit for other parts of your application, but has not yet decided on a good solution. So, the current plan is to support GTK +, where adding support for other toolkits will be very simple. The API of the final shell will be completely different from the current implementation.

Plugins

Writing plug-ins is supported on all platforms, however, few plug-ins currently actually have desktop support (such as these in the Flutter – desktop-Embedding project).

Tooling

Supporting the desktop in the Flutter tool is a work in progress. To use any support (such as hosts listed on the FLUTTER device), you must currently require:

  • Can’t be in stableFlutter channelOn. This is to show that desktop support is not yet considered stable.
    • This library and should always be updatedFlutterBecause major desktop changes happen all the time.
  • Must beENABLE_FLUTTER_DESKTOPEnvironment variable is set totrue. This is to avoid interfering with existing mobile development workflows while developing long-term solutions.
    • macOS/Linux: export ENABLE_FLUTTER_DESKTOP=true
    • Windows: $env:ENABLE_FLUTTER_DESKTOP="true"(PowerShell) orset NABLE_FLUTTER_DESKTOP=true (CMD).

A pre-built Shell library

By default, desktop libraries are not downloaded, but can be downloaded into the Engine artifact cache of Flutter by running a Flutter precache with the — Linux, — MacOS, or — Windows flags, depending on the development platform. Currently only the Debug library is available.

C++ Wrapper

Windows and Linux libraries provide C apis. To make them easier to use, C ++ Wrappers can be used, which can be built into the application to provide a higher level of API. The precache command downloads the source code for this Wrapper into the cpp_client_wrapper folder next to the library.

Use the Shells

Because Flutter Create does not yet support desktops, a Runner application is required. The Flutter -desktop-embedding project provides a simple runtime for each desktop platform. These desktop support can be done using the Flutter tools.

Or, if you’re familiar with native development on the platform, you can write your own applications using a shell. For information about using them, see the headers that come with the platform-appropriate libraries. There will be more files in the future; Now, look at the flutter-desktop-embedding example to see how it uses them. In addition to linking to Flutter libraries, applications also need to bundle your Flutter assets. On Windows and Linux, ICU data from the Flutter engine is also required (look for icudtl.dat in the Flutter folder bin/cache/ artifacts/engine).

Plugins
macOS

Before setting up FLEViewController, before calling launchEngine… Call -registerwithRegistrar on each plug-in you want to use:

[XYZMyAwesomePlugin registerWithRegistrar:
      [myFlutterViewController registrarForPlugin:"XYZMyAwesomePlugin"]].Copy the code
Windows/Linux

After creating the Flutter window controller, invoke the plugin’s registration function. Such as:

 MyAwesomePluginRegisterWithRegistrar(
      flutter_controller.GetRegistrarForPlugin("MyAwesomePlugin"));
Copy the code

Application requirements of Flutter

Since the Flutter framework does not yet fully support the desktop platform, existing Flutter applications may need to be slightly modified to run.

The target platform

Most applications need to override the target platform of the application to one of the supported platforms to avoid “unknown platform” exceptions. A single target can be encoded:

import 'package:flutter/foundation.dart'show debugDefaultTargetPlatformOverride; [...].voidmain() { debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia; [...]. }Copy the code

If the code needs to run on mobile devices and desktops, or if you want to run different code on different desktop platforms, you can use Platform to make the distinction. For example, the lines in main() above could be replaced with:

/// If the current platform is desktop, override the default platform to
/// a supported platform (iOS for macOS, Android for Linux and Windows).
/// Otherwise, do nothing.
void _setTargetPlatformForDesktop() {
  TargetPlatform targetPlatform;
  if (Platform.isMacOS) {
    targetPlatform = TargetPlatform.iOS;
  } else if (Platform.isLinux || Platform.isWindows) {
    targetPlatform = TargetPlatform.android;
  }
  if(targetPlatform ! =null) { debugDefaultTargetPlatformOverride = targetPlatform; }}Copy the code

Note that the target platform used affects not only the behavior and appearance of the widgets, but also the content available on the platform, such as fonts;

Fonts

The Flutter application may default to the target platform standard font, but it is not available on the desktop. For example, if the TargetPlatform is targetplatform.ios, the Material library will default to San Francisco, and it can be used on macOS but not Linux or Windows.

Most applications need to set up fonts based on the host platform (via ThemeData, for example) or specific fonts bundled with the application. Without additional font specifications (for example, text for the DEBUG banner), other widgets that don’t use ThemeData may not display.

Tools

Run the Flutter Doctor and ensure that no problems are reported with the platform-related parts.

Doctor support for Windows and Linux is coming soon; Requirements are:

  • Linux:Make, and the latest versionclang.
  • Windows:Visual Studio 2017or2019, includingDesktop development with C++.

Project to run git repository

Example

Once everything is set up just run the desktop Flutter application in the sample directory;

Note: Only debug mode is currently available. Using –release runs successfully, but the result is still using the debug Flutter configuration: implies firing, etc.

IDEs

To run the Flutter project on the desktop using the IDE, you need to set ENABLE_FLUTTER_DESKTOP for the IDE:

  • VS CodeIn:VS Code settings.jsonadddart.env:
    "dart.env": {
      "ENABLE_FLUTTER_DESKTOP": true,}Copy the code
  • IntelliJ / Android Studio: You need to use the normal procedures of the operating system to set environment variables for your application. This variable is set correctly if the device menu lists the development machine as a device.