Last active
May 25, 2023 19:25
-
-
Save aqua30/52a350ae8bf7792aca16965639a695fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* [DashboardFragment] is a custom flutter fragment which is used to pass custom | |
* data from native app to dashboard part in flutter. This communication is set up on | |
* both native and in flutter module. | |
* | |
* @see To refer to the flutter part, please refer the flutter module. | |
* */ | |
const val KeyData = "key_data" | |
const val CommunicationChannel = "com.app/dataShare" | |
class DashboardFragment: FlutterFragment() { | |
private val dashboardRoute = "/loan_dashboard" | |
private val callbackMethod = "shareData" | |
var data: String? = null | |
companion object { | |
fun instance(data: String): DashboardFragment { | |
val fragment = DashboardFragment() | |
val bundle = Bundle().apply { | |
putString(KeyData,data) | |
} | |
fragment.arguments = bundle | |
return fragment | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
arguments?.let {args -> | |
if (args.containsKey(KeyData)) { | |
data = args.getString(KeyData) | |
} | |
} | |
} | |
override fun provideFlutterEngine(context: Context): FlutterEngine? { | |
val engine = FlutterEngine(requireActivity()) | |
engine.navigationChannel.setInitialRoute(dashboardRoute) | |
return engine | |
} | |
override fun configureFlutterEngine(flutterEngine: FlutterEngine) { | |
super.configureFlutterEngine(flutterEngine) | |
GeneratedPluginRegistrant.registerWith(flutterEngine); | |
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CommunicationChannel).setMethodCallHandler { | |
call, result -> | |
if (call.method == callbackMethod) { | |
data?.let { | |
result.success(it) | |
} ?: result.error("error") | |
} else { | |
result.notImplemented() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment