i have canvas that have { DrawConnections(state, viewModel) DrawPackets(viewModel)…
- User
i have canvas that have { DrawConnections(state, viewModel) DrawPackets(viewModel) DrawDevices(state, viewModel) }
my function:
@Composable fun DrawPackets( viewModel: EmulatorViewModel ) { val pos1 = Offset(0f, 0f) val pos2 = Offset(1000f, 1000f)
codevar circlePosition by remember { mutableStateOf(pos1) } animateOffsetAsState( targetValue = pos2, animationSpec = tween(durationMillis = 10000, easing = LinearEasing) ) { circlePosition = it } Box( Modifier .size(50.dp) .background(Color.White) .offset(circlePosition.x.dp, circlePosition.y.dp) )}
i want to call it from: LaunchedEffect(true) { viewModel.eventFlow.collectLatest { event -> when (event) { is EmulatorViewModel.UiEvent.ShowToast -> { val duration = Toast.LENGTH_SHORT Toast.makeText( context, event.message, duration ).show() }
codeis EmulatorViewModel.UiEvent.AnimatePacket -> { // call it from here } } } }how can i do that if i cant call composables outside composable functionso?
- Assistant
You can't directly call a composable function outside of a composable scope, but you can create a composable function that takes in a parameter and returns the composable you want to draw. Then, you can call this function from within your main composable, passing in the necessary parameters.
For example:
code - User
is EmulatorViewModel.UiEvent.AnimatePacket -> { // call DrawPacket function here with new position for the packet }
@Composable fun DrawPacket(circlePosition: Offset) {
isn't it a composable function?
- Assistant