Skip to content

Commit 2825131

Browse files
authored
Added sample code for multiple flutters on iOS (#670)
1 parent 241e2c1 commit 2825131

31 files changed

+1588
-1
lines changed

add_to_app/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ existing Android and iOS apps.
1717
existing Android app and demonstrates using
1818
[Pigeon](https://pub.dev/packages/pigeon) to communicate between Flutter and
1919
the host application.
20+
* ['multiple_flutters'](./multiple_flutters) — Shows the usage of the Flutter
21+
Engine Group APIs to embed multiple instances of Flutter into an existing app
22+
with low memory cost.
2023

2124
## Goals for these samples
2225

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# multiple_flutters
2+
3+
This is a sample that shows how to use the Flutter Engine Groups API to embed
4+
multiple instances of Flutter into an existing Android or iOS project.
5+
6+
## Getting Started
7+
8+
For iOS instructions, see:
9+
[multiple_flutters_ios](./multiple_flutters_ios/README.md).
10+
11+
For Android instructions, see:
12+
[multiple_flutters_android](./multiple_flutters_android/README.md).
13+
14+
## Requirements
15+
16+
* Flutter -- after version 1991216543eb74e07c2de3746ca07c92071b19ac
17+
* Android
18+
* Android Studio
19+
* iOS
20+
* Xcode
21+
* Cocoapods
22+
23+
## Flutter Engine Group
24+
25+
These examples use the Flutter Engine Group APIs on the host platform which
26+
allows engines to share memory and CPU intensive resources. This leads to easier
27+
embedding of Flutter into an existing app since multiple entrypoints can be
28+
maintained via a FlutterFragment on Android or a UIViewController on iOS.
29+
Before FlutterEngineGroup, users had to juggle the usage of a small number of
30+
engines judiciously.
31+
32+
More info on those API's can be found in the source
33+
code:
34+
35+
- iOS -
36+
[FlutterEngineGroup.h](https://github.com/flutter/engine/blob/master/shell/platform/darwin/ios/framework/Headers/FlutterEngineGroup.h)
37+
- Android -
38+
[FlutterEngineGroup.java](https://github.com/flutter/engine/blob/master/shell/platform/android/io/flutter/embedding/engine/FlutterEngineGroup.java)
39+
40+
## Important Files
41+
42+
### iOS
43+
44+
- [DataModel.swift](./multiple_flutters_ios/MultipleFluttersIos/HostViewController.swift)
45+
— Observable data model that is the source of truth for the host platform and Flutter.
46+
- [HostViewController.swift](./multiple_flutters_ios/MultipleFluttersIos/HostViewController.swift)
47+
— A UIViewController that synchronizes to the DataModel.
48+
- [main.dart](./multiple_flutters_module/lib/main.dart) — The Flutter source
49+
code.
50+
- [SingleFlutterViewController.swift](./multiple_flutters_ios/MultipleFluttersIos/SingleFlutterViewController.swift)
51+
— A subclass of FlutterViewController that synchronizes with the DataModel.
52+
- [DoubleFlutterViewController.swift](./multiple_flutters_ios/MultipleFluttersIos/DoubleFlutterViewController.swift)
53+
— A UIViewController that embeds multiple Flutter instances.
54+
55+
### Android
56+
57+
## Data Synchronization Description
58+
59+
This sample code performs data synchronization to share data between the host
60+
platform and multiple instances of Flutter by combining the
61+
[Observer design pattern](https://en.wikipedia.org/wiki/Observer_pattern) and
62+
[Flutter platform channels](https://flutter.dev/docs/development/platform-integration/platform-channels).
63+
Here is how it works:
64+
65+
- The definitive source of truth for the data lives in the host platform data
66+
model.
67+
- Every host view displaying Flutter content maintains: a Flutter engine, a
68+
bidirectional platform channel, and a subscription to the host data model.
69+
- Flutter instances maintain a copy of the data they are interested in reading,
70+
and this data is seeded by the host when the instance is first displayed.
71+
- Mutations from Flutter code are sent to the host platform via the channel. The
72+
host platform performs the mutations, and then notifies all host view
73+
controllers and Flutter engines of the new value.
74+
- Mutations from host code happen directly on the data model who notifies host
75+
view controllers and Flutter engines of the new value.
76+
77+
This is just one possible way to synchronize the data between the host platform
78+
and multiple Flutter instances. A more complete implementation is proposed in
79+
the work of
80+
[flutter/issues/72030](https://github.com/flutter/flutter/issues/72030).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MultipleFluttersIos.xcworkspace/xcuserdata
2+
MultipleFluttersIos.xcodeproj/project.xcworkspace/xcuserdata
3+
Pods/Pods.xcodeproj/xcuserdata
4+
MultipleFluttersIos.xcodeproj/xcuserdata
5+
Pods/
6+
MultipleFluttersIos.xcodeproj/xcshareddata/

0 commit comments

Comments
 (0)