0

I have been doing dictionary app for a while. when I delete dictionary snackBar shows and writes dictionary is deleted but there is a floating action button and when the snackBar appears on the screen ,the snackbar appears above the floating action button, I don't want it to appear on it. It just stays on the screen for 1-2 seconds. I want the floating action button and snackbar to appear on top of each other. I couldn't adapt this to my own code. How can I do it ? I will share my code and image

CreateYourOwnDictionaryScreen

@Composable
fun CreateYourOwnDictionaryScreen(
    navController: NavController,
    viewModel: CreateYourOwnDictionaryViewModel = hiltViewModel()
) {

    val scaffoldState = rememberScaffoldState()
    val state = viewModel.state.value

    val scope = rememberCoroutineScope()


    Scaffold(
        scaffoldState = scaffoldState,
        topBar = {
            TopAppBar(
                backgroundColor = bar,
                title = {

                    androidx.compose.material3.Text(
                        text = "your dictionaries",
                        modifier = Modifier.fillMaxWidth(),
                        color = Color.White,
                        fontSize = 22.sp
                    )

                },
                navigationIcon = {
                    IconButton(onClick = {
                        navController.navigate(Screen.MainScreen.route)
                    }) {
                        Icon(
                            imageVector = Icons.Filled.ArrowBack,
                            contentDescription = "Go Back"
                        )
                    }
                }

            )

        },

        floatingActionButtonPosition = FabPosition.Center,
        floatingActionButton = {
            FloatingActionButton(
                onClick = { navController.navigate(Screen.CreateDicScreen.route) },
                backgroundColor = bar,

            ) {
                Icon(Icons.Filled.Add, "fab")
            }
        }
    ) {

        Box(modifier = Modifier.background(MaterialTheme.colors.background)) {



            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(16.dp)
            ) {


                LazyColumn(
                    modifier = Modifier.fillMaxSize()
                ) {
                    items(state.dictionaries) { dictionary ->

                        CreateYourOwnDictionaryItem(
                            dictionary = dictionary,
                            modifier = Modifier
                                .fillMaxWidth()
                                .clickable {

                                },
                            onDeleteClick = {
                                viewModel.onEvent(
                                    CreateYourOwnDictionaryEvents.DeleteDictionary(dictionary)
                                )
                                scope.launch {
                                    val result = scaffoldState.snackbarHostState.showSnackbar(
                                        message = "dictionary is deleted",
                                        actionLabel = "Undo",
                                        duration = SnackbarDuration.Short
                                    )
                                }

                            },
                            onEditClick = {


                            })

                    }
                }


            }
        }


    }


}

Image

enter image description here

NewPartizal
  • 604
  • 4
  • 18

1 Answers1

0

I'm afraid this is difficult in Compose, you can dig the ScaffoldLayout and you'll find this code block.

val snackbarOffsetFromBottom = if (snackbarHeight != 0) {
      snackbarHeight + (fabOffsetFromBottom ?: bottomBarHeight)
} else {
      0
}

Scaffold will always offset the snackbar on top of a fab or a bottombar

And based on the answer in this post regarding material specs

This is specifically one of the "Don't" examples from the Material guidelines: https://material.io/components/snackbars#behavior

Making sure visual elements don't move out from underneath (say, when users are about to tap the FAB) is one of the key points to making a predictable UI

Also based on the Material Guidelines

Consecutive snackbars should appear above persistent bottom navigation

z.g.y
  • 5,512
  • 4
  • 10
  • 36