Creating a platform view widget

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 9Developing 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.

Check, the hands_on_platform_views file on GitHub for the complete plugin code.

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):
    • viewTypeA 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.

In Chapter 13Improving User Experience, we will check how to add native code to the application. You may also find some helpful information there to helps you understand how the platform view works.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.14.219