Skip to content

ShaneDiNozzo/flutter-desktop-embedding

 
 

Repository files navigation

Desktop Embedding for Flutter

This purpose of this project is to support building applications that use Flutter on Windows, macOS, and Linux.

It consists of libraries that implement Flutter's embedding API, handling drawing and mouse/keyboard input, as well as optional plugins to access other native platform functionality.

How to Use This Code

Setting Up

The tooling and build infrastructure for this project requires that you have a Flutter tree in the same parent directory as the clone of this project:

<parent dir>
  ├─ flutter (from http://github.com/flutter/flutter)
  └─ flutter-desktop-embedding (from https://github.com/google/flutter-desktop-embedding)

Alternately, you can place a .flutter_location_config file in the directory containing flutter-desktop-embedding, containing a path to the Flutter tree to use, if you prefer not to have the Flutter tree next to flutter-desktop-emebbing.

Repository Structure

Each supported platform has a top-level directory named for the platform. Within that directory is a library directory containing the core embedding library, and an example directory containing an example application using it.

See the README file in the directory corresponding to your platform for more details.

In addition, there is:

  • example_flutter: The Flutter application loaded by the example application provided for each platform.
  • plugins: Plugins which provide access to additional platform functionality. These follow a similar structure to Flutter plugins. See the README for details.
  • third_party: Dependencies used by this repository, beyond Flutter itself.
  • tools: Tools used in the development process. Currently these are used by the build systems, but in the future developer utilities providing some functionality similar to the flutter tool may be added.

Flutter Application Requirements

Because desktop platforms are not supported Flutter targets, existing Flutter applications are likely to require slight modifications to run.

Target Platform Override

Most applications will need to override the target platform for the application to one of the supported values in order to avoid 'Unknown platform' exceptions. This should be done as early as possible.

In the simplest case, where the code will only run on desktop and the behavior should be consistent on all platforms, you can hard-code a single target:

import 'package:flutter/foundation.dart'
    show debugDefaultTargetPlatformOverride;
[...]

void main() {
  debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
  [...]
}

If the code needs to run on both mobile and desktop, or you want different behavior on different desktop platforms, you can conditionalize on Platform. For example, the line in main() above could be replaced with a call to:

/// 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;
  }
}

Note that the target platform you use will affect not only the behavior and appearance of the widgets, but also the expectations Flutter will have for what is available on the platform, such as fonts.

Fonts

Flutter applications may default to fonts that are standard for the target platform, but unavailable on desktop. For instance, if the target platform is TargetPlatform.iOS the Material library will default to San Francisco, which is available on macOS but not Linux or Windows.

Most applications will need to set the font (e.g., via ThemeData) based on the host platform, or set a specific font that is bundled with the application. The example application demonstrates using and bundling Roboto on all platforms.

Symptoms of missing fonts can include text failing to display, console logging about failure to load fonts, or in some cases crashes.

Feedback and Discussion

For bug reports and specific feature requests, you can file GitHub issues. For general discussion and questions there's a project mailing list.

When submitting issues related to build errors or other bugs, please make sure to include the git hash of the Flutter checkout you are using. This will help speed up the debugging process.

Caveats

  • This is not an officially supported Google product.
  • This is an exploratory effort, and is not part of the Flutter project. See the Flutter FAQ for Flutter's official stance on desktop development.
  • Currently the development workflow assumes you are starting from an existing native application shell, and provides the pieces to add Flutter support. This is very different from the Flutter model, where the native application projects are created automatically. This may change in the future, but for now there is no equivalent to flutter create.
  • Many features that would be useful for desktop development do not exist yet. Check the plugins directory for support for native features beyond drawing and event processing. If the feature you need isn't there, file a feature request, or write a plugin!

About

Desktop implementations of the Flutter embedding API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 44.0%
  • Objective-C 24.5%
  • Dart 17.3%
  • Objective-C++ 4.7%
  • Makefile 4.4%
  • Batchfile 2.8%
  • Other 2.3%