Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How to show snackbar in Jetpack Compose material 3

Posted on November 11, 2022 By

Solution 1 :

You can use a scaffoldHostState to show a snackbar inside a scaffold and yes, you should use the scaffoldPadding for the padding of e.g. a topAppBar, navigationBar …

Can you try this?

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Screen() {
    val snackbarHostState = remember { SnackbarHostState() }

    val localCoroutineScope = rememberCoroutineScope()

    LaunchedEffect(Unit) {
        viewModel.loginEvent.collect {
            when (it) {
                is LoginViewModel.LoginEvent.Error -> {
                    // show snackbar with it.error
                    localCoroutineScope.launch {
                        snackbarHostState.showSnackbar(
                            message = "Error message"
                        )
                    }
                }
                // ...
            }
        }
    }

    Scaffold(
        snackbarHost = { SnackbarHost(snackbarHostState) }
    ) { scaffoldPadding ->
        Box(
            modifier = Modifier
                .padding(scaffoldPadding)
                .fillMaxSize()
        ) {
            // your composables ...
        }
    }
}

Problem :

I started to learn Jetpack Compose and it looks confusing how to implement a simple snackbar, using material3 dependencies.

Documentation shows I need a scaffold and it needs a content with padding (?).
Do I need put my screen composable inside scaffolds lambda?
How do I pass my message text to Snackbar?
I have a simple flow collector for event from viewmodel where I want to pass the error text to snackbar.

Test()
LaunchedEffect(Unit) {
    viewModel.loginEvent.collect {
        when (it) {
            is LoginViewModel.LoginEvent.Error -> {
             // show snackbar with it.error
            }
       
            is LoginViewModel.LoginEvent.Success -> {
              // 
            }
        }
    }
}
@Composable
fun Test() {
    Box(modifier = Modifier.fillMaxSize()) {
//my composable screen
}

Comments

Comment posted by Talkative Breakdown

Thanks, it worked! Maybe unrelated question for this issue but – since I added the Scaffold around my composable, now after clicking inside a TextField, white top and bottom bars appear. The Box() has modifier .fillMaxSize(), why it’s resizing?

Comment posted by F.G.

I’m not sure what you mean, can you show your code?

READ  [FIXED] android - although method exists but I still receive the error :"Could not find method methodName(View) in a parent or ancestor Context"
Powered by Inline Related Posts
Android Tags:android, android-jetpack-compose, material3, snackbar

Post navigation

Previous Post: Do you have to pay for MyNetDiary?
Next Post: [FIXED] Simple Cucumber Test is not running on Sample Android Project

Related Posts

[FIXED] android – CameraX analyzer does not run on every frame Android
[FIXED] java – How to edit ionic cordova plugin Android
[FIXED] Pixel 7 pro android 13 – getting the wrong resource Android
[FIXED] android – How do i add sdkmanager to my environments variable path? Android
[FIXED] java – How to get the names of the json columns from firebase? I leave an example below my question Android
[FIXED] android – How to get Current Fragment in ViewPager2 Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme