• Stars
    star
    143
  • Rank 257,007 (Top 6 %)
  • Language
    C++
  • License
    MIT License
  • Created over 2 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[WIP] Embedding native windows into Flutter. Scrollable support & widgets placement on-top. 🪟

flutter_native_view

MITDonatepub package

Embedding native windows & components directly into Flutter window.

Example

Video showing a Flutter Windows app running with embedded webview & VLC using flutter_native_view.

Notice how AppBar is on-top of the the native surfaces & both native surfaces scroll perfectly.

flutter_native_view.mp4

[slight lag & delay can be observed due to screen recording. the actual experience is very seamless]

Try running the example application by cloning the repository.

Sponsor

You may sponsor this project's future development & research at:

It'll be a great motivation for me to continue.

💖 Current Sponsors

Used By

Description

This setup only uses Win32 APIs & no texture, intermediate buffers or copying of pixel buffers. Thus, it is very performant.

A Flutter plugin / Win32 setup to embed other native Windows (HWND on Windows) into Flutter window.

Current API design allows to embed any arbitrary HWND completely from Dart as a Widget. This can be a good choice when client code wants to embed any third-party window (which is already opened) into the Flutter window. However, this is not ideal in most cases, because there is almost no point of embedding a window without having a programmatic control to it (via some API).

On the other hand, a window created by the client code itself e.g. a webview instance window or a video-output window etc. (on which the client code has full programmatic control) will be an ideal window to embed, in that case:

  • If client code decides to create an HWND through platform channel interface, they can use the setup present in core to embed a window (or send back the HWND as int64_t to the plugin throught Dart).
  • Since dart:ffi is very capable now, one can pass the HWND directly as int of the window they created using dart:ffi or package:win32 through existing plugin API to embed it.

Future

In future, I will create natively rendered, performant & less-bundle-sized webview & video playback plugins, if I get enough community support. Currently I'm only targetting Windows to limit the scope of work, though I plan for Linux support at some point.

Setup

Add following lines to your windows/runner/main.cpp file:

  #include <flutter/dart_project.h>
  #include <flutter/flutter_view_controller.h>
  #include <windows.h>

+ #include "flutter_native_view/flutter_native_view_plugin.h"
  #include "flutter_window.h"
  #include "utils.h"
  window.SetQuitOnClose(true);

+ flutternativeview::NativeViewContainer::GetInstance()->Create();

  ::MSG msg;
  while (::GetMessage(&msg, nullptr, 0, 0)) {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
  }

Docs

Initialize the plugin

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  /// Initialize.
  await FlutterNativeView.ensureInitialized();
  runApp(const MyApp());
}

Create a controller & render a window

class _MyAppState extends State<MyApp> {
  /// Create a [NativeViewController].
  final controller = NativeViewController(
    /// Using [FindWindow] from `package:win32` to retrieve `HWND` as [int].
    handle: FindWindow(nullptr, 'VLC Media Player'.toNativeUtf16()),
    /// Make the [NativeView] interactable.
    hitTestBehavior: HitTestBehavior.translucent,
  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
            child: Padding(
              padding: const EdgeInsets.all(24.0),
              child: Stack(
                children: [
                  LayoutBuilder(
                    /// Create a [NativeView].
                    builder: (context, constraints) => NativeView(
                      /// Pass [NativeViewController] to render the window.
                      controller: controller,
                      width: constraints.maxWidth,
                      height: constraints.maxHeight,
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: FloatingActionButton(
                      onPressed: () {},
                      child: const Icon(Icons.edit),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

Dispose the native view & destory the window

controller.dispose();

Features

Currently Implemented

  • Placement of other Flutter Widgets on top of the NativeView.
  • Multiple instances of NativeView.
  • Window movement handling & NativeViews positioning.
  • Window resize handling & NativeViews sizing.
  • Windows 10 & higher support.
  • Proper disposing of HWND and instances.
  • Semi transparent Widgets on top of NativeView.
  • Placement of NativeViews inside scrollables like ListViews.
  • [UNSTABLE] Customizable hit-test i.e. optional interactability with the NativeViews.

Under Progress

  • Stable support for interactability with the NativeViews [maybe switching to programmatic approach].
  • Support for older Windows versions.
  • Defining z-order for each NativeViewController.
  • Finalized API.
  • General stability.

Motivation

flutter/flutter#31713

Absence of official Platform View APIs in Flutter for Windows, Linux & macOS.

Platforms

The plugin currently works on following platforms:

Platform State
Windows Working

I plan to add Linux support soon. For now, limiting the scope of work to just Windows.

License

MIT License

Copyright (C) 2022, Hitesh Kumar Saini <[email protected]>

More Repositories

1

youtube-search-python

🔎 Search for YouTube videos, channels & playlists. Get 🎞 video & 📑 playlist info using link. Get search suggestions. WITHOUT YouTube Data API v3.
Python
735
star
2

flutter_acrylic

Flutter library for window acrylic, mica & transparency effects.
Dart
591
star
3

dart_vlc

Flutter bindings to libVLC.
C++
503
star
4

media_kit

A complete 🎬 video & 🎵 audio library for Flutter & Dart.
Dart
391
star
5

windows_taskbar

Flutter library serving utilities related to Windows taskbar.
C++
151
star
6

audire

Identify music playing around you.
Kotlin
141
star
7

flutter_media_metadata

A Flutter plugin to read 🔖 metadata of 🎵 media files. Supports Windows, Linux, macOS, Android, iOS & Web.
C++
68
star
8

flutter_audio_desktop

[WIP] An 🎵 audio playback library for Flutter Desktop. Supports Windows & Linux. Based on miniaudio.
C++
52
star
9

moving-letters-android

Text animation library for Jetpack Compose.
Kotlin
40
star
10

desktoasts

[DISCONTINUED] A Dart FFI package to send 💬 toasts on Windows. Written in C++, based on WinToast.
36
star
11

flutter-windows-ANGLE-OpenGL-ES

OpenGL ES inside Flutter Windows.
C++
33
star
12

window_plus

As it should be. Extend view into title-bar.
Dart
32
star
13

dart_discord_rpc

Discord Rich Presence for Flutter & Dart.
C++
29
star
14

image_palette_generator

🌈 Generate a color palette from an image or find dominant color.
C++
14
star
15

mpris_service

🎹 D-Bus MPRIS for GNU/Linux.
Dart
14
star
16

safe_local_storage

🗃️ A safe caching library to read/write values on local storage.
Dart
12
star
17

material_wave_slider

[WIP] Material Design 3 / Material You style wave slider.
C++
12
star
18

uri_parser

🔗 A minimal & safe utility to parse URIs.
Dart
8
star
19

libmpv.dart

[WIP] Dart bindings to mpv media player's C API.
Dart
6
star
20

artwork-extractor

A simple CLI to extract & save artwork of a 🎵 music/audio file.
C++
5
star
21

mdl

🎶 A simple webapp to download music.
Svelte
5
star
22

harmonoid-website

Harmonoid Project Website.
JavaScript
4
star
23

flutter-win32-acrylic

[WIP] Not ready for usage.
CMake
2
star
24

alexmercerind.github.io

✨ A website where I publish my documentations, blogs & keep my CV.
CSS
1
star
25

dart-ffi-linux-issue

1
star
26

dart_api_test

Nothing to see here. Move along.
C++
1
star
27

native-android

[NOT FOR WORK] 🦄 Just a personal repository to keep Native Android apps made in Java & Kotlin while learning.
Kotlin
1
star
28

musiclibgen

[WIP] A fast music indexer & music library generator for C.
C++
1
star
29

hello-world

1
star
30

github-stats

Python
1
star
31

slides

1
star