Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Konfetti on click problem #320

Open
behu-kea opened this issue Dec 14, 2023 · 5 comments
Open

Konfetti on click problem #320

behu-kea opened this issue Dec 14, 2023 · 5 comments

Comments

@behu-kea
Copy link

behu-kea commented Dec 14, 2023

I really love this confetti animation, but am struggling to create konfetti on button click. I have it working when the activity is loaded. I am using Kotlin and Compose without xml. I have tried things like:

setContent {
    var position by remember {
        mutableStateOf(2f)
    }
    Button(onClick = {
        position = 5f;
    }) {
        Text(
            text = "Spray confetti" + position.toString())
    }

    KonfettiView(
        modifier = Modifier.fillMaxSize(),
        parties = listOf(Party(
            speed = 0f,
            maxSpeed = 30f,
            damping = 0.9f,
            spread = 360,
            colors = listOf(0xfce18a, 0xff726d, 0xf4306d, 0xb48def),
            emitter = Emitter(duration = 100, TimeUnit.MILLISECONDS).max(100),
            position = Position.Absolute(position, 1f)
        ))
    )
}

I have also tried

var party by remember { mutableStateOf<Party?>(null) }
            
Button(onClick = {
    party = Party (emitter = Emitter(duration = 5, TimeUnit.SECONDS).perSecond(30))
}) {
    Text(text = "Spray confetti!")
}

KonfettiView(
    modifier = Modifier.fillMaxSize(),
    parties = party?.let { listOf(it) } ?: emptyList()
)

Can someone please help?

@behu-kea behu-kea changed the title Konfetti on click Konfetti on click problem Dec 14, 2023
@behu-kea
Copy link
Author

Related: #305

@jastigall392
Copy link

- ```

@dazza5000
Copy link

What solution did you find?

@dazza5000
Copy link

You can wrap KonfettiView in a key to achieve this effect

            key(content) {
                if (content.konfettiState == KonfettiState.Started) {
                   KonfettiView(
                        modifier = Modifier.fillMaxSize(),
                        party = KonfettiUtil.getParty(colors = Color.KonfettiColors)
                    ) {
                        onFinishConfettiAnimation.invoke()
                    }
                }
            }

key can be something like this

@Composable
fun RecreateComposableExample() {
    var resetKey by remember { mutableStateOf(0) }

    Column {
        // The button will reset the composable below
        Button(onClick = { resetKey++ }) {
            Text("Recreate Composable")
        }

        // The composable to be recreated
        key(resetKey) {
            ComposableToRecreate()
        }
    }
}

@Composable
fun ComposableToRecreate() {
    var counter by remember { mutableStateOf(0) }
    
    Column {
        Text(text = "Counter: $counter")
        Button(onClick = { counter++ }) {
            Text("Increment Counter")
        }
    }
}

@behu-kea
Copy link
Author

behu-kea commented Jan 23, 2025

Ahh thanks. I got it to work with some AI use. If someone else needs this i ahve a link here for a working demo: https://github.com/behu-kea/konfetti-android/blob/7d17de0fb169a8ec7bbddbcf8b45d6639abe6cbd/app/src/main/java/com/example/confetti_timer/MainActivity.kt#L70

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants