When we create a platform view widget, we basically create a Flutter wrapper of a native iOS/Android view. The process of creating a platform view is similar to plugins and requires adding native code to an application.
To keep things simple, we create a plugin project; see Chapter 9, Developing Your Own Flutter Plugin, to remember how to create a plugin project. In this project, we define a new view, HandsOnTextView, which is a native text displaying view; TextView on Android and UITextView on iOS.
To begin with, after the plugin project is created, we define the Dart API. This is the code that makes the bridge from Dart to native code. We create a HandsOnTextView widget.
As you can see, its build method has the following important parts:
- Depending on the platform type, Theme.of(context).platform, we instantiate a AndroidView or UiKitView widget.
- Their properties are similar, and we define the viewType widget we want to create, its parameters (creationParams), and the parameters codec (creationParamsCodec):
- viewType: A view type is used by the Flutter platform view system to indicate which native view we are intending to use, similar to a plugin system.
- creationParams: These are the arguments that we want to pass down to the native view creation—the text to be shown, in our case.
- creationParamsCodec: This defines which method of parameter data transfer will occur while sending creationParams to the native code.
This is all for the Dart side of the platform view. Now we need to define the view in corresponding platforms.