All Products
Search
Document Center

Mobile Platform as a Service:Flutter project access guide

Last Updated:May 29, 2024

For more information about how to configure and initialize an mPaaS environment for a Flutter project, see Access a Flutter project.

Use MCDP

Register MCDP View in native Android

  1. Inheritance io.flutter.plugin.platform.PlatformView to provide a reference to the android.view.View.

    package com.example.mpaas_demo
    
    import android.content.Context
    import android.view.View
    import com.mpaas.cdp.CdpAdvertisementView
    import io.flutter.plugin.platform.PlatformView
    
    /**
    * MCDP view
    */
    internal class NativeMcdpView(context: Context, id: Int, creationParams: Map<String?, Any?>?) : PlatformView {
        private val cdpView: CdpAdvertisementView
    
        override fun getView(): View {
            return cdpView
        }
        override fun dispose() {}
        init {
    
            cdpView = CdpAdvertisementView(context)
            if (creationParams != null) {
                cdpView.updateSpaceCode(creationParams["cdpCode"].toString())
            }
        }
    }
  2. Create a factory class that is used to create instances of the NativeView.

    package com.example.mpaas_demo
    
    import android.content.Context
    import io.flutter.plugin.common.StandardMessageCodec
    import io.flutter.plugin.platform.PlatformView
    import io.flutter.plugin.platform.PlatformViewFactory
    
    class NativeMcdpViewFactory (activity: MainActivity) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
       private val mContext = activity
        override fun create(context: Context, viewId: Int, args: Any?): PlatformView {
            val creationParams = args as Map<String?, Any?>?
            return NativeMcdpView(mContext, viewId, creationParams)
        }
    }

Register a platform view in an application

 override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        flutterEngine
            .platformViewsController
            .registry
            .registerViewFactory("<mcdp-platform-view-type>", NativeMcdpViewFactory(this))
    }

Use MCDP in Flutter

  1. Create a MCDP Widget.

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/foundation.dart';
    import 'package:flutter/services.dart';
    
    class McdpView extends StatelessWidget {
      final String cdpCode;
    
      McdpView({Key? key, required this.cdpCode}) : super(key: key);
      String viewType = '<mcdp-platform-view-type>';
    
      @override
      Widget build(BuildContext context) {
        const StandardMessageCodec _decoder = StandardMessageCodec();
        if (defaultTargetPlatform == TargetPlatform.android) {
          return  Expanded(
            child: AndroidView(
              viewType: viewType,
              creationParams: {"cdpCode": cdpCode},
              creationParamsCodec: _decoder,
            ),
          );
        } else {
          return Container();
        }
      }
    }
    
  2. Use MCDP Widget.

      McdpView( cdpCode: 'slideshow1'),