When working with Jetpack Compose, you may need to apply a modifier only under a specific condition. A common approach looks like this:
While this is a valid way of adding a modifier conditionally, it lacks conciseness. For a more streamlined solution, create a helper modifier for conditional application, leveraging the fact that modifiers are defined as extension functions:
This modifier can be used as follows:
When adding multiple modifiers, remember to chain them:
When the condition is that a given value is not null, type-safe access to the not-null value may be required. The following modifier facilitates this:
Use it like so: