Sub topic 1
When Flutter needs a native view
Most Flutter screens never need a native Android or iOS view. Flutter draws every button, list and text field itself. A few features still exist only as native UI components, and the only way to show one inside a Flutter screen is a platform view. This chapter explains what a platform view is, which problems call for one, and which problems only look like they do. Later chapters build the Android, iOS and Dart pieces.
Prerequisites
This chapter assumes you already have the following:
- Basic Flutter and Dart: you can write a
StatelessWidgetand aStatefulWidgetand understand the widget tree. - A basic grasp of platform channels. You have seen a
MethodChannelcall into Kotlin or Swift code. - Enough Kotlin (or Java) and Swift to read short classes.
- A working setup that runs a Flutter app on an Android device or emulator and on an iOS device or simulator. Later chapters need both.
This chapter has no code you must run. The one snippet at the end is optional.
How Flutter draws, and why that matters
A native Android app builds its screen from View objects. A native iOS app builds it from UIView objects. The operating system owns these objects. It lays them out, draws them, and delivers touch events to them.
Flutter works differently. It asks the platform for a surface to draw on, and then it renders every pixel itself with its own engine. A Flutter ElevatedButton is not an Android Button or a UIButton. It is a Dart object that paints a rounded rectangle and some text onto Flutter's canvas.
This design gives you consistent visuals, fast iteration and one codebase. It has a consequence: Flutter knows nothing about native views. If a library ships its UI as a native view, Flutter has no widget for it, and you cannot copy its behavior by calling a function.
What a platform view is
A platform view is a mechanism that places a real native view inside the Flutter widget tree. You write a widget in Dart that takes up a rectangle in the layout. The engine puts a native View or UIView in that rectangle, so users see and touch the native component.
Three parts make this work:
- A view type, which is a string such as
com.example.app/native_label. It names the kind of native view you want. - A native factory, written in Kotlin or Swift. It creates the native view when Flutter asks for a view of that type.
- A Dart widget,
AndroidVieworUiKitView. It asks the engine to create the native view and reserves space for it.
The next chapter covers how these connect. Here it is enough to know that the native view lives in the platform's own view hierarchy, not in Flutter's rendering tree. Flutter has to coordinate the two systems, and that is where the costs come from.
Situations that call for a platform view
You need a platform view when a native UI component has no practical Flutter replacement. The usual cases fall into a few groups.
| Situation | Example | Why a pure Flutter widget does not work |
|---|---|---|
| Large SDKs that ship UI | Google Maps, Apple MapKit, Mapbox | The map renderer, tile caching and gestures are inside the SDK's own view. |
| Embedded web content | In-app browsers, OAuth or payment pages, rich HTML | The browser engine (Android WebView, iOS WKWebView) draws its own content. |
| Ad and monetization views | Native ad units from ad SDKs | The SDK renders the ad and tracks impressions in its own view. |
| Platform-specific components | PDFKit's `PDFView`, an Android `MapView`, a camera or AR view | The component depends on system frameworks that Dart cannot call directly. |
| Existing native code you cannot rewrite | A company's internal charting view used in a native app | Rewriting it in Flutter costs more than hosting it. |
The common thread is that the thing you want to show is a view that something else draws. Libraries such as google_maps_flutter and webview_flutter are built on platform views for this reason.
Situations that do not call for a platform view
Many problems look like they need a native view but do not. Check these alternatives first.
| What you want | Better option | Why |
|---|---|---|
| A custom look, such as a native-style switch or picker | Flutter widgets (`Switch.adaptive`, `CupertinoPicker`, `CustomPaint`) | Flutter can draw it, and you avoid the platform view costs. |
| Camera frames, video frames, or other pixel streams | The `Texture` widget | Native code renders into a texture and Flutter composites it as an ordinary image. |
| Calling a native API that has no UI of its own (battery level, file picker, biometrics) | A `MethodChannel` plugin | You send a request and read a result. No view is involved. |
| Fast native computation | `dart:ffi` | You call C code directly without a view. |
| A system dialog or sheet | A plugin that presents it from native code | The system shows the dialog on top of Flutter. It is not embedded in your layout. |
A Texture fits a case where you only want pixels, such as a video frame. It does not forward touch events or accessibility information to native code. That means it cannot replace a map or a web page, which users scroll, tap and select.
A good check is this question: does the user need to interact with the native component as a native component? If the answer is no, you probably do not need a platform view.
A decision flow
The diagram below turns the checks above into a sequence. Walk through it before you write any native code.
Only the last branch leads to the rest of this tutorial. Reaching it means you have ruled out simpler options.
Costs to know before you commit
A platform view works, but it is not free. You should understand the costs before choosing one, so you can plan for them instead of finding them late.
- Performance. Flutter and the native view system have to share one screen. Depending on the Android composition mode, Flutter may copy pixels or synchronize two rendering threads. A single platform view is usually fine. A list with many platform views often drops frames.
- Touch handling. Touches go to either Flutter or the native view. Scrolling a native map inside a scrollable Flutter page is a classic conflict. You must decide which side wins.
- Layering. The native view and Flutter widgets have to be stacked correctly. Overlays, clips and transforms around a platform view can behave differently from what they do around a normal widget.
- Two codebases. Every platform view needs Kotlin or Java code for Android, Swift or Objective-C code for iOS, and Dart code that ties them together. You test and maintain all three.
- Platform reach. This tutorial covers Android and iOS. The widget you write in Dart should show a fallback on other targets, such as the web or desktop, unless a separate implementation exists.
A first look in code
The snippet below does not use a native view yet. It shows the habit the rest of the tutorial builds on: put the decision in one widget and give unsupported platforms a fallback. The NativeMap name is a placeholder, and the real native view arrives in later chapters.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: NativeMap()))));
class NativeMap extends StatelessWidget {
const NativeMap({super.key});
@override
Widget build(BuildContext context) {
switch (defaultTargetPlatform) {
case TargetPlatform.android:
case TargetPlatform.iOS:
// Later chapters replace this placeholder with AndroidView or UiKitView.
return const Text('A native view would appear here');
default:
return const Text('This feature is not available on this platform');
}
}
}Run it on an emulator and then on another target, such as Chrome. The text changes with the platform. Keeping this switch inside one widget means the rest of your app never needs to know which platform it runs on.
Common mistakes
- Choosing a platform view because a package exists for it. A package such as a map plugin hides the platform view from you. Use it as-is when it meets your needs. Build your own only when no suitable package exists.
- Using
Texturefor interactive content. A texture shows pixels, but it carries no native touch handling or accessibility tree. - Expecting a Flutter widget to cover a native view for free. A widget placed over a platform view must be composited correctly. Test overlays early.
- Forgetting other platforms. Code that returns an
AndroidViewunconditionally throws or shows nothing on iOS and the web. - Putting many native views in a scrolling list. Each view costs memory and rendering work. Prototype a long list before you commit to the design.
Summary of the decision
Use a pure Flutter widget when Flutter can draw the thing. Use a plugin with a MethodChannel or dart:ffi when you need a capability but no visible native component. Use Texture when you only need pixels from native code. Use a platform view when the user must interact with a real native component that something else draws, such as a map, a web page or an ad.
Exercise
Your team wants to show a live camera preview in a Flutter app. The preview is only for viewing, with no touch interaction with the preview itself. A second request asks for an embedded Google Map that users can pan and zoom. For each request, say whether you would choose a Texture or a platform view, and give one reason.
Show answer
Use a Texture for the camera preview. Native code can render camera frames into a texture, and Flutter composites it like an image. This avoids the platform view costs, and no native touch handling is needed. Use a platform view for the map. The map SDK draws its own view and handles panning, zooming and gestures itself, and Flutter cannot reproduce that. A texture would show pixels but would not pass touches or accessibility information to the map.
Exercise
A colleague proposes a platform view to show a native-looking iOS toggle switch on both platforms. What would you suggest instead?
Show answer
Use a Flutter widget such as Switch.adaptive or CupertinoSwitch. Flutter can draw this control itself, so a platform view would add native code for Android and iOS, touch and layout limits, and a performance cost, and the result would have no functional benefit.