@@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.ExperimentalLayoutApi
99import androidx.compose.foundation.layout.FlowRow
1010import androidx.compose.foundation.layout.fillMaxWidth
1111import androidx.compose.foundation.layout.padding
12+ import androidx.compose.foundation.layout.widthIn
1213import androidx.compose.foundation.shape.RoundedCornerShape
1314import androidx.compose.material.icons.Icons
1415import androidx.compose.material3.LocalAbsoluteTonalElevation
@@ -27,8 +28,13 @@ import androidx.compose.ui.Alignment
2728import androidx.compose.ui.Modifier
2829import androidx.compose.ui.draw.clip
2930import androidx.compose.ui.draw.drawBehind
31+ import androidx.compose.ui.platform.LocalDensity
3032import androidx.compose.ui.text.SpanStyle
33+ import androidx.compose.ui.text.TextMeasurer
34+ import androidx.compose.ui.text.TextStyle
3135import androidx.compose.ui.text.buildAnnotatedString
36+ import androidx.compose.ui.text.rememberTextMeasurer
37+ import androidx.compose.ui.text.style.TextAlign
3238import androidx.compose.ui.text.withStyle
3339import androidx.compose.ui.unit.Dp
3440import androidx.compose.ui.unit.coerceAtLeast
@@ -122,6 +128,7 @@ fun LargeTypeContent(
122128 groups.forEach { item ->
123129 SymbolItem (
124130 item = item,
131+ length = groups.size,
125132 elevation = elevation,
126133 selectedIndex = indexState,
127134 )
@@ -149,6 +156,7 @@ fun LargeTypeContent(
149156private fun SymbolItem (
150157 modifier : Modifier = Modifier ,
151158 item : LargeTypeState .Item ,
159+ length : Int ,
152160 selectedIndex : State <Int >,
153161 elevation : Dp ,
154162) {
@@ -218,11 +226,42 @@ private fun SymbolItem(
218226 fontFamily = monoFontFamily,
219227 style = MaterialTheme .typography.headlineMedium,
220228 )
229+
230+ val indexTextStyle = MaterialTheme .typography.labelSmall
231+ val indexTextWidth = kotlin.run {
232+ val text = remember(length) {
233+ val digits = length.toString().length
234+ " " .padStart(digits, ' 0' )
235+ }
236+ rememberTextMeasuredWidthDp(
237+ text = text,
238+ style = indexTextStyle,
239+ ) + 1 .dp // just in case the measurement is slightly incorrect
240+ }
221241 Text (
242+ modifier = Modifier
243+ .widthIn(min = indexTextWidth),
222244 text = index.plus(1 ).toString(),
223245 color = LocalContentColor .current
224246 .combineAlpha(MediumEmphasisAlpha ),
225- style = MaterialTheme .typography.labelSmall,
247+ style = indexTextStyle,
248+ textAlign = TextAlign .Center ,
226249 )
227250 }
228251}
252+
253+ @Composable
254+ private fun rememberTextMeasuredWidthDp (
255+ text : String ,
256+ style : TextStyle ,
257+ measurer : TextMeasurer = rememberTextMeasurer(),
258+ ): Dp {
259+ val density = LocalDensity .current
260+ return remember(measurer, density, text) {
261+ val width = measurer.measure(
262+ text = text,
263+ style = style,
264+ ).size.width / density.density
265+ width.dp
266+ }
267+ }
0 commit comments