Sub topic 12
Alternatives to Platform Views and Shipping as a Plugin
This chapter assumes you understand Flutter widgets, basic Dart, and platform channels. You should also be able to read simple Kotlin and Swift and run a Flutter application on Android or iOS.
Platform views are useful when Flutter must display a real native view, such as a map, camera preview, web browser, or payment control. They are not the best solution for every native feature. A platform view adds a boundary between Flutter’s rendering system and the native UI toolkit. That boundary can affect performance, gestures, accessibility, and maintenance.
Choose the right rendering strategy
Use a pure Flutter widget when the feature can be drawn and controlled in Dart. Flutter widgets usually provide the most consistent layout, theming, animation, testing, and scrolling behavior.
Use a platform view when the native SDK requires an actual View on Android or UIView on iOS. For example, a native SDK may depend on platform window management, text input, or its own rendering lifecycle.
A Texture is another option for native content that only needs to appear as pixels. A texture lets Flutter display a stream of frames without embedding the complete native view hierarchy. Camera previews and video output often fit this model. A texture does not automatically provide the native view’s buttons, accessibility tree, or input behavior.
| Requirement | Usually suitable |
|---|---|
| Flutter controls layout and interaction | Pure Flutter widget |
| Native SDK requires a real UI object | Platform view |
| Native code produces video or image frames | Texture |
| Native code provides computation only | FFI or a platform channel |
Understand the trade-offs
A platform view can cost more than a Flutter widget because Flutter and the native compositor must coordinate two rendering systems. The cost depends on the Android composition mode, device, view complexity, and how many native views appear at once.
Common symptoms include dropped frames while scrolling, delayed touch handling, clipped overlays, and unexpected behavior when Flutter widgets overlap the native view. A texture can reduce some composition problems, but it gives up native controls and may require a separate input or accessibility design.
Before choosing, test the real screen on physical devices. Measure scrolling and animation with the Flutter performance tools. Test keyboard input, screen readers, rotation, backgrounding, and multiple instances. A solution that works for one static preview may fail inside a scrolling list.
Package the integration as a plugin
Keep reusable platform-view code in a Flutter plugin instead of placing it directly in one application. A plugin gives the integration a stable Dart API and keeps Android and iOS implementation details behind that API.
Create a plugin project with the Flutter CLI:
flutter create --template=plugin --platforms=android,ios -a kotlin -i swift native_labelA typical plugin contains these parts:
- A Dart library that exposes a widget or service.
- An Android implementation that registers a
PlatformViewFactory. - An iOS implementation that registers a
FlutterPlatformViewFactory. - A shared view type identifier, such as
com.example.native_label. - Tests for Dart behavior and platform-specific behavior where practical.
- A
pubspec.yamlfile that declares the supported platforms and dependencies.
In a plugin, Android registration normally occurs when the plugin is attached to a FlutterEngine. The plugin registers its factory with the engine’s platform-view registry. On iOS, the plugin’s registration method receives a registrar and registers the factory with the same view type identifier. The Dart widget then selects AndroidView or UiKitView and passes creation parameters through the plugin’s public API.
Keep the view type identifier private to the plugin when possible. Treat creation parameters as an API: document their types, validate them on both platforms, and avoid passing large objects. For later commands, use a per-view MethodChannel or another documented communication interface.
Publish and maintain the plugin
Before publishing, test the plugin in a separate example application. Check that a fresh consumer can add the dependency without copying native files or editing generated Flutter files.
Document:
- Supported Flutter, Android, iOS, and SDK versions.
- Required permissions and native configuration.
- Which composition and gesture limitations remain.
- Whether multiple instances are supported.
- How the plugin disposes native resources.
- Which values are safe in creation parameters.
If the native SDK changes, preserve the Dart API where possible and isolate version-specific code inside the platform implementations. A small, documented plugin is easier to replace than application code that directly depends on private native classes.
Exercise
A native SDK displays a camera preview but does not require native buttons or text input. Which approach should you evaluate first, and why?
Show answer
Evaluate a Texture first. It can display native-produced frames without embedding a complete native view hierarchy. Use a platform view instead if the SDK requires a real native view or native interaction.