Home > OS >  Where is tabPositions instanced when I use TabRow with Compose?
Where is tabPositions instanced when I use TabRow with Compose?

Time:12-08

The Code A is source code of fun TabRow.

There is a paramter named indicator which need to be passed when invoked, and Android give a default implementation @Composable { tabPositions -> TabRowDefaults.Indicator( Modifier.tabIndicatorOffset(tabPositions[selectedTabIndex])) }

But I don't know where the paramter tabPositions is instanced, could you tell me?

Code A

@Composable
fun TabRow(
    selectedTabIndex: Int,
    modifier: Modifier = Modifier,
    backgroundColor: Color = MaterialTheme.colors.primarySurface,
    contentColor: Color = contentColorFor(backgroundColor),
    indicator: @Composable (tabPositions: List<TabPosition>) -> Unit = @Composable { tabPositions ->
        TabRowDefaults.Indicator(
            Modifier.tabIndicatorOffset(tabPositions[selectedTabIndex])
        )
    },
    divider: @Composable () -> Unit = @Composable {
        TabRowDefaults.Divider()
    },
    tabs: @Composable () -> Unit
) {
    Surface(
        modifier = modifier.selectableGroup(),
        color = backgroundColor,
        contentColor = contentColor
    ) {
        ...
    }
}

CodePudding user response:

The indicator parameter is a lambda that you provide and the code behind TabRow will call it and pass in the tabPositions. Just search the source code for indicator(:

TabRow

You'll find that it gets called here:

subcompose(TabSlots.Indicator) {
                    indicator(tabPositions)
                }.fastForEach {
                    it.measure(Constraints.fixed(tabRowWidth, tabRowHeight)).placeRelative(0, 0)
                }
  • Related