Search code examples
androidflutterkotlinsdk

How to use Native Android onClickListeners in Flutter App


I want to use a custom Android SDK which has a listener "BenefitInAppButtonListener" and has its own functions

this is how to normally use it in Android

checkoutBtn.setListener(object : BenefitInAppButtonListener {
    override fun onButtonClicked() {
        val benefitInAppCheckout = BenefitInAppCheckout.newInstance(
            this@MainActivity,
            ....,
            ....,
            ....,
            (object : CheckoutListener{
                override fun onTransactionSuccess(p0: Transaction?) {
                   Toast.makeText(applicationContext, "Success", Toast.LENGTH_LONG).show()
                }

                override fun onTransactionFail(p0: Transaction?) {
                    Toast.makeText(applicationContext, "Fail", Toast.LENGTH_LONG).show()
                }

            })

        )

    }

    override fun onFail(p0: Int) {
        TODO("Not yet implemented")
    }


})

Here We initialize a class called BenefitInAppCheckout by calling and .newInstance() and also pass in a CheckoutListener which is automatically called when compiled

So the issue is that I want to use this custom SDK in Flutter, but unable to call the BenefitInAppButtonListener since we don't have any view or button to setListener to

I tried to call the method directly, here is my code which calls the onButtonClicked() but doesn’t run the CheckoutListener

it seems that BenefitInAppButtonListener itself needs to be called otherwise the inner functions/Checkoutlistener doesn’t work

 var myListener = object : BenefitInAppButtonListener {
            override fun onButtonClicked() {

                Log.i("hamad2", paymentAmount) // this log does work

                val benefitInAppCheckout = BenefitInAppCheckout.newInstance(
                    this@MainActivity,
                    ...,
                    ...,
                    ...,
                    (object : CheckoutListener {
                        override fun onTransactionSuccess(p0: Transaction?) {
                            Toast.makeText(applicationContext, "Succ", Toast.LENGTH_LONG).show()
                        }

                        override fun onTransactionFail(p0: Transaction?) {
                            Toast.makeText(applicationContext, "Fail", Toast.LENGTH_LONG).show()
                        }

                    })

                )
            }
            override fun onFail(p0: Int) {
                Toast.makeText(applicationContext, "Fail", Toast.LENGTH_LONG).show()
            }
        }
        
        myListener.onButtonClicked();

Really appreciate the help!! Thanks 😊

My whole flutter app's mainactivity.kt file:

package com.example.benefit_test

import android.annotation.SuppressLint
import android.content.ContentValues
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import mobi.foo.benefitinapp.data.Transaction
import mobi.foo.benefitinapp.listener.BenefitInAppButtonListener
import mobi.foo.benefitinapp.listener.CheckoutListener
import mobi.foo.benefitinapp.utils.BenefitInAppButton
import mobi.foo.benefitinapp.utils.BenefitInAppCheckout

class MainActivity: FlutterActivity() {

    private val channel = "ummatisoftwares.benefitpay"

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channel).setMethodCallHandler {
                call, result ->
            if (call.method == "BenefitPay"){
                val amount = call.argument<String>("amount")
                val status = payBenefitApp(amount.toString())
                if (status == "Success") result.success("Success")
                else result.error("Error", status, null)
            } else {
                result.notImplemented()
            }
        }

    }


    private fun payBenefitApp(paymentAmount: String): String {
        var status = "fail"

        var myListener = object : BenefitInAppButtonListener {
            override fun onButtonClicked() {
                Log.i("hamad2", paymentAmount)
                val benefitInAppCheckout = BenefitInAppCheckout.newInstance(
                    this@MainActivity,
                    ..., ..., ..., // some strings that are passed like 
                    (object : CheckoutListener {
                        override fun onTransactionSuccess(p0: Transaction?) {
                            Toast.makeText(applicationContext, "Suc", Toast.LENGTH_LONG).show()
                            status = "Success"
                        }

                        override fun onTransactionFail(p0: Transaction?) {
                            status = " "+p0.toString()
                            Toast.makeText(applicationContext, "Fail", Toast.LENGTH_LONG).show()
                        }

                    })

                )
            }
            override fun onFail(p0: Int) {
                Toast.makeText(applicationContext, "Fail", Toast.LENGTH_LONG).show()
            }
        }

        myListener.onButtonClicked();

        return status
    }
}

Solution

  • I found a solution, create a button and then simply addListener to it Finally call button.performClick() function

    here is the example for with respective to my problem

    val button = BenefitInAppButton(this@MainActivity)
    button.setListener(object: BenefitInAppButtonListener {
        override fun onButtonClicked() {
            val benefitInAppCheckout = BenefitInAppCheckout.newInstance(
                this@MainActivity,
                ...,
                ...,
            )
        }
    })
    
    button.performClick()