0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

IDを持ったリストを一覧から撮る

0
Posted at

はじめに

今回は以前自分の記事で書いたiOSっぽいWheelPickerをつかってID持っているCollectionであれば、共通して使える部品を実装してみます
https://qiita.com/ryuji_sato/items/609c731c75d3d0ef1bcb

コード

@Composable
fun <T> IdentifiablePicker(
    value: T,
    onValueChange: (T) -> Unit,
    range: Iterable<T>,
    itemContent: @Composable BoxScope.(T) -> Unit,
    modifier: Modifier = Modifier,
    idSelector: (T) -> Any,
    textStyle: TextStyle = LocalTextStyle.current,
) {
    val list = range.toList()
    val selectedIndex = list.indexOfFirst { idSelector(it) == idSelector(value) }
    
    ListItemPicker(
        modifier = modifier,
        itemContent = itemContent,
        value = if (selectedIndex >= 0) list[selectedIndex] else value,
        onValueChange = onValueChange,
        list = list,
        textStyle = textStyle,
    )
}

// 要素に単位などのラベルをつけるための部品
@Composable
fun <T> IdentifiablePicker(
    value: T,
    onValueChange: (T) -> Unit,
    range: Iterable<T>,
    modifier: Modifier = Modifier,
    idSelector: (T) -> Any,
    label: (T) -> String = { it.toString() },
    textStyle: TextStyle = LocalTextStyle.current,
) {
    val list = range.toList()
    val selectedIndex = list.indexOfFirst { idSelector(it) == idSelector(value) }
    
    ListItemPicker(
        modifier = modifier,
        label = label,
        value = if (selectedIndex >= 0) list[selectedIndex] else value,
        onValueChange = onValueChange,
        list = list,
        textStyle = textStyle,
    )
}

さいごに

実践を考えるとやはりこういったIDごとなどの共通部品として使うことがメインだと思うので、このままコピペで使えるように記事を書いてみました

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?