How to listen to back press event in WebView in Flutter? Webview has been created in android platfromview

56 views Asked by At

I’m developing an application in Flutter where I’ve created a WebView as an Android platform. I’m currently facing an issue where I need to listen to the back press event and I’m not sure how to approach this. This is for enhancing my skill to use Platform view in general, The basic idea was to implement gestures or events to pass it to an Android/iOS platform view. I read the documentation PlatformViewSurface class, and here we have gestureRecognizers but I can't wrap my head around to use it. or is there any other way and I am getting it wrong?

in Android side code.

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        flutterEngine.platformViewsController.registry.registerViewFactory("<platform-view-type>",NativeViewFactory())
    }
}

in NativeViewFactory.kt

class NativeViewFactory : PlatformViewFactory(StandardMessageCodec.INSTANCE){
    override fun create(context: Context, viewId: Int, args: Any?): PlatformView {
        val creationParams = args as Map<String?, Any?>?
        return NativeView(context, viewId, creationParams)
    }
}

in NativeView.kt

class NativeView(context: Context, id: Int, creationParams: Map<String?, Any?>?): PlatformView{
    private var webview: WebView
    lateinit var params: HashMap<String?, Any?>
    private var view : WebViewActivity? = WebViewActivity(context,creationParams)
    override fun getView(): View? {
        return view
    }

    init {

        webview=WebView(context)
        if (creationParams != null) {
            params= creationParams as HashMap<String?, Any?>
        }else{
            params=HashMap<String?,Any?>()
        }
    }
}

in flutter

class NativeView extends StatelessWidget {
  const NativeView({super.key});

  @override
  Widget build(BuildContext context) {
    // This is used in the platform side to register the view.
    const String viewType = '<platform-view-type>';
    // Pass parameters to the platform side.
     Map<String, dynamic> creationParams = <String, dynamic>{};
    creationParams["url"]="https://upendra-bajpai.github.io/upnotes/";
   // creationParams["url"]="https://vdsai.com";
    return SafeArea(
      child: PlatformViewLink(
        onCreatePlatformView: (param) {
          return PlatformViewsService.initSurfaceAndroidView(
            id: param.id,
            viewType: viewType,
            creationParams: creationParams,
            layoutDirection: TextDirection.ltr,
            creationParamsCodec: const StandardMessageCodec(),
            onFocus: () {
              param.onFocusChanged(true);
            },
          )
            ..addOnPlatformViewCreatedListener(param.onPlatformViewCreated)//TODO: tony what are these methods and ".."?
            ..create();
        },
        viewType: viewType,
        surfaceFactory: (context, controller) {
          return AndroidViewSurface(
              controller: controller as AndroidViewController,
              gestureRecognizers: const <
                  Factory<
                      OneSequenceGestureRecognizer>>{}, // TODO:what is that tony
              hitTestBehavior: PlatformViewHitTestBehavior.opaque);
        },
      ),
    );
  }
}

Project linkPlateform view implementation

Please help me if you can.

I read documentation PlatformViewSurface class, here we have gestureRecognizers but i can't wrap my head around to use it. or there is any other way and i am getting it wrong?

I tried to read code from webview flutter but faailed to understand how they were using interfaces in webview.

0

There are 0 answers