5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Android]Jetpack ComposeのComposableのModifierを条件によって切り替えたい

Posted at

例えば

val modifier = 
if (isMax) {
         Modifier.fillMaxWidth()
                 .fillMaxHeight()
} else {
         Modifier.fillMaxWidth()
                 .wrapContentHeight()
}

AnyComposable(modifier = modifier)

というようにheightの部分だけ条件によって変更したい時
以下のコードだと.applyの中身はmodifierとして適用されない

AnyComposable(modifier = 
         Modifier.fillMaxWidth()
                 .apply {
                     if (isMax) {
                          fillMaxHeight()
                     } else {
                          wrapContentHeight()
                     }
                 }
)

thenを使う
以下が正解

AnyComposable(modifier = 
         Modifier.fillMaxWidth()
                 .then (
                     if (isMax) {
                          Modifier.fillMaxHeight()
                     } else {
                          Modofier.wrapContentHeight()
                     }
                 )
)
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?