Sub topic 1
When Flutter Needs a Native View
Prerequisites
This chapter assumes that you know basic Flutter and Dart, including widgets, StatelessWidget, and StatefulWidget. You should also understand the basic idea of a MethodChannel and be able to read simple Kotlin or Swift. You do not need to know the platform-view APIs yet.
The boundary between Flutter and the platform
Flutter draws most of an app through its own rendering engine. A Flutter widget describes a user interface, and Flutter paints that interface into a single Flutter-rendered surface.
Some features already exist as Flutter widgets. Others are provided by Android or iOS libraries. A platform view is a native Android or iOS view placed inside the Flutter widget tree. Flutter controls where the view appears, while the operating system owns the native view and its rendering behavior.
For example, an Android WebView remains an Android WebView. An iOS map remains an iOS map view. Flutter can position these views beside Flutter widgets, but it does not turn their internal contents into ordinary Flutter widgets.
When a platform view is the right tool
Choose a platform view when the native SDK already provides the behavior you need and reproducing it in Flutter would be costly or unreliable.
Common examples include:
- A web page that requires a native
WebView. - A map SDK with native markers, camera controls, and gestures.
- A payment or identity SDK that requires its own secure native user interface.
- A camera preview supplied by a native camera library.
- A video player or document viewer with platform-specific rendering.
- A specialized control that must match an existing Android or iOS implementation.
The decision is about more than appearance. A native component may include gesture handling, accessibility behavior, hardware acceleration, security controls, or device integration that a visual imitation would not provide.
A platform view is especially useful when the native SDK is maintained independently and changes frequently. Wrapping that SDK lets your Flutter code use the vendor's supported implementation instead of duplicating it.
When a platform view is the wrong tool
Do not use a platform view merely because a native control looks familiar. A pure Flutter widget is usually simpler when the control only needs standard layout, painting, animation, text, or input.
Platform views add a boundary between two UI systems. That boundary can affect:
- Rendering performance, especially when many native views appear in a scrolling list.
- Gesture competition between Flutter parents and the native child.
- Clipping, transforms, opacity, and overlays.
- Accessibility semantics and focus behavior.
- Testing, because part of the UI is outside Flutter's normal widget tree.
- Platform consistency, because Android and iOS may behave differently.
A Flutter implementation is often the better choice for buttons, cards, forms, charts, and custom animations. A texture-based solution may be better for frequently updated pixels, such as a camera or video stream, when the plugin supports it.
> callout note > Start with the smallest native surface possible. If only a camera image or video frame is needed, prefer a texture or a Flutter-compatible plugin when available. Embed the complete native view only when its native behavior is required.
A practical decision process
Before choosing a platform view, ask these questions:
- Does a maintained Flutter package already expose the required behavior?
- Does the native SDK provide important behavior that Flutter cannot reproduce easily?
- Must the user interact with the native component directly?
- Will the component appear once, or many times in a scrolling list?
- Does it need to overlap Flutter widgets, animate, rotate, or receive complex gestures?
- Can both platforms provide an equivalent native implementation?
- Can the native view be disposed safely when its Flutter widget leaves the tree?
A map shown on one screen may be a reasonable platform view. Hundreds of map views in a scrolling feed are likely to cause memory and performance problems. A native payment form may be justified by security requirements, while a simple confirmation button usually is not.
The implementation connects three responsibilities: Dart requests a view by a shared view-type string, native code registers a factory for that string, and the factory creates each native instance. Later chapters build each part and show how data, messages, gestures, and disposal cross the boundary.
Exercise
You need a custom animated button that uses standard text, colors, and Flutter navigation. Should you begin with a platform view? Why?
Show answer
No. A pure Flutter widget is the better starting point because the behavior does not require a native SDK or platform-only capability. A platform view would add a rendering and gesture boundary without providing a needed benefit.