Skip to content

Embed a Flutter application inside a web app in an iFrame and with element replacement. Also demonstrate how the Flutter apps can talk to handlers in the HTML/JavaScript. Embed a Flutter module inside an Android app

License

Notifications You must be signed in to change notification settings

freemansoft/flutter-embedded

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Embedded Flutter application communication with parent application

These examples show communication between the Flutter Application and the host / native application via message channels. The same Flutter code is used for all three embeddings with a minor switch statement for Web communication.

Message flow

This application uses message channels for mobile platform/Flutter communication. Both Flutter and Native can initiate an increment operation. The native side always receives a message from Flutter after an increment operation has occured.

graph TB;
    subgraph host
        HostControl[Control and UI Listener]
        HostChannel[Message Channel]

        HostChannel2[Message Channel]
        HostListener[Message Listener]
    end

    subgraph flutter
        FlutterControl[Control and UI Listener]
        FlutterChannel[Message Channel]

        FlutterChannel2[Message Channel]
        FlutterListener[Message Listener]
        Counter
    end

    FlutterControl--increment()-->Counter--{action:incremented}-->FlutterChannel2-.{action:incremented}.->HostChannel2-->HostListener

    HostControl--increment()-->HostChannel-."{action:incremnt}".->FlutterChannel

    FlutterChannel--"{action:increment}"-->FlutterListener--increment()-->Counter

Loading

Platform-specific implementations

Additional details are available for each platform implementation

Platform
Web application with message communication between Flutter and Web
Android application with message communication between Flutter and Android
iOS application with message communication between Flutter and Android

Mobile Native communication

Communication between the Mobile native code and Flutter happens over platform channels. Web commnication happens via window messaging

Flutter Channel types

There are three native platform channel types. V1 of this application uses the Message Channel

Channel type and Flutter class Description Flutter Native Native Flutter Return
Method Invocation Invoke method on the other side Yes Yes Via result
Message Sends a message to a remote listener Yes Yes Via reply
Event Streams and sinks. Events can flow in both directions Yes Yes N/a

FlutterMethodChannel, FlutterEventChannel, FlutterBasicMessageChannel

Dart/Flutter, Android and iOS have corresponding channel classes for each channel type. The three platform channel implementation classes in Flutter, iOS and, Android are:

The V1 demonstration code in this repository implements native communication via a message channel.

Flutter Class iOS Class Android Class
MethodChannel FlutterMethodChanel MethodChannel
EventChannel FlutterEventChannel EventChannel
BasicMessageChannel FlutterBasicMessageChannel BasicMessageChannel

Method Channel Codecs MethodCodec

Method channels a method on the opposite side. Method channels use MethodCode implementations.

Flutter Codec Class iOS Codec Class Android Codec Class
MethodCodec Interface ... ...
StandardMethodCodec FlutterStandardMethodCodec StandardMethodCodec
JSONMethodCodec FlutterJSONMethodCodec JSONMethodCodec

Message Channel Codecs

Message channels use MessageCodec Implementations. Messages are a single payload with an optional return value. Uses codecs shown below.

Flutter Codec Class iOS Codec Class Android Codec Class
MessageCodec Interface ... ...
StandardMessageCodec FlutterStandardMessageCodec StandardMessageCodec
BinaryCodec FlutterBinaryMessageCodec BinaryCodec
JsonMessageCodec FlutterJSONMessageCodec JSONMessageCodec
StringCodec FlutterStringCodec StringCodec

Event Channel

Event channels are continuous broadcast streams that use a MethodCodec.

Flutter Codec Class iOS Codec Class Android Codec Class
See MethodCodec See MethodCodec See MethodCodec

V1

This is the shortest path with as little code as possible.

  1. Flutter fills up the entire screen for the iOS and Android applications. There are no native controls to generate the increment messages
  2. This uses a classic single channel specifying the action in a field in the JSON. Native applications typically do one message per channel and have one channel for each message type

About

Embed a Flutter application inside a web app in an iFrame and with element replacement. Also demonstrate how the Flutter apps can talk to handlers in the HTML/JavaScript. Embed a Flutter module inside an Android app

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published