
Flutter Doesn't Wrap Native Widgets: It Draws Every Pixel Itself
- كاتب موثّق
- Flutter 3.x
محتويات المقال
Switch is supposed to become a UISwitch on iOS. It never was. On the iPhone, that toggle is exactly what it is on a Pixel: shapes Flutter painted itself, with no native switch anywhere in the app.
The common misconception
Switch, TextField and ListView, and at runtime Flutter maps each widget to the platform's own control, a UISwitch on iOS and an Android switch on Android. The OS draws your UI; Flutter only tells it what to show.
What actually happens
Three layers, one canvas
Framework (Dart): widgets, layout, painting, gestures, and the Material and Cupertino libraries. This is the code you write against. Engine (mostly C++): the Dart runtime, text layout and the renderer that rasterizes every frame. The framework talks to it through dart:ui.Embedder (one per platform): starts the app, sets up threads, hands the engine a rendering surface and connects it to the OS for input and accessibility.
FlutterView, which the engine paints into a SurfaceView or a SurfaceTexture. Your hundred widgets aren't a hundred Android views. They're pixels in one.
From a widget to pixels
paint() methods. The result is a scene of drawing commands, not a set of views. The engine then rasterizes that scene on the GPU with , which precompiles its shaders when the engine itself is built, so they don't compile while your app runs.
iOS: Impeller is the only supported renderer, with no way to switch back to Skia. Android: Impeller is the default on API 29 and later. On older versions, or devices without Vulkan, it falls back to the legacy OpenGL renderer. macOS, Windows and Linux: Impeller is enabled by default as of Flutter 3.47. Web: Flutter uses Skia today.
"Native" means native code, not native widgets
Why the toggle looked like Android
Switch is a Material widget, and it paints a Material switch on every platform. If you want each platform's convention, you have to ask for it:
import 'package:flutter/material.dart';class NotifyTile extends StatelessWidget { const NotifyTile({super.key, required this.on, required this.onChanged}); final bool on; final ValueChanged<bool> onChanged; @override Widget build(BuildContext context) { return ListTile( title: const Text('Notifications'), // A Material switch on every platform, iPhone included trailing: Switch(value: on, onChanged: onChanged), ); }}import 'package:flutter/material.dart';class NotifyTile extends StatelessWidget { const NotifyTile({super.key, required this.on, required this.onChanged}); final bool on; final ValueChanged<bool> onChanged; @override Widget build(BuildContext context) { return ListTile( title: const Text('Notifications'), // iOS and macOS: a Cupertino-style switch. Still painted by Flutter trailing: Switch.adaptive(value: on, onChanged: onChanged), ); }}Switch.adaptive checks the theme's platform: on iOS and macOS it looks and feels like the iOS control, elsewhere it's the Material one. Both are drawn by Flutter. The Cupertino library implements the iOS design language with Flutter's own widgets; it isn't a bridge to UIKit. Slider, Checkbox, Radio, CircularProgressIndicator, RefreshIndicator and AlertDialog have adaptive constructors too.
When you really need a native view
AndroidView and UiKitView embed a real native view inside the Flutter UI.
import 'package:flutter/foundation.dart';import 'package:flutter/material.dart';// 'native-map' must be registered by your Android and iOS host codeclass NativeMap extends StatelessWidget { const NativeMap({super.key}); @override Widget build(BuildContext context) { return switch (defaultTargetPlatform) { TargetPlatform.android => const AndroidView(viewType: 'native-map'), TargetPlatform.iOS => const UiKitView(viewType: 'native-map'), _ => const Center(child: Text('Map not available here')), }; }}Switch becomes a UISwitch on iOS | Switch.adaptive paints an iOS-style one on iOS and macOS |
How to apply it in your project
Choose a look per control, on purpose Decide between Material everywhere, Cupertino everywhere, or .adaptiveconstructors. For input controls like switches, sliders and dialogs, the docs recommend following each platform's conventions, which.adaptivegives you for free.Review both platforms from the first screen Don't assume a widget will "look native" on its own. Check every new screen on an iPhone and an Android phone, or at least with the ThemeData.platformtrick during development.Reach for plugins before platform views Camera, location and payments are platform APIs, not UI: a plugin reaches them through platform channels, no native view needed. Embed a native view only when the UI itself can't be rebuilt, then profile scrolling on a low-end Android phone. Isolate rendering bugs before reporting them If something renders wrong on Android, run flutter run --no-enable-impelleronce. If the glitch disappears, it's tied to the renderer, and that's exactly what to put in the bug report.
Key takeaways
Flutter doesn't map widgets to native controls: its framework paints the whole UI and the engine rasterizes it. Impeller renders on iOS and on Android API 29+ (older or non-Vulkan devices fall back to OpenGL); the web uses Skia. "Native" refers to compiled machine code and to platform channels, not to the widgets you see. A platform's look is your choice: Material, Cupertino or .adaptive, all drawn by Flutter.Use platform views only for native UI you can't reasonably rebuild, and measure what they cost.
التعليقات (0)