简体   繁体   中英

Android Floating action button, how to use it based on screen jetpack compose

I have on floating action button for different screens using scaffold jetpack compose, how will I know the current screen which clicked the FAB to do a certain action. Because for every screen the action is different.

@OptIn(ExperimentalMaterial3Api::class)
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun AppLayout(
    bottomBar: @Composable () -> Unit = {},
    content: @Composable () -> Unit = {}
) {

    Scaffold(
        modifier = Modifier,
        bottomBar = { bottomBar() },
        floatingActionButton = {
            FloatingActionButton(
                containerColor = appColors.primary,
                shape = CircleShape,
                onClick = {},
                modifier = Modifier,
            ) {
                Icon(Icons.Filled.Add, null,tint = Color.White)
            }
        }
    ) { safePadding ->
        Box(
            modifier = Modifier.padding(

                bottom = safePadding.calculateBottomPadding()

            )
        ) {
            content()
        }

    }

}

You can use the following code to record the router of different pages:

var recordRouter by remember { mutableStateOf("") }

Switch to a different page and update the recordRouter value.

When the FloatingActionButton is clicked, determine which page the recordRouter belongs to, and then trigger the corresponding event method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM