@@ -95,7 +95,6 @@ fun SuperDropdown(
9595 items : List <String >,
9696 alwaysRight : Boolean = false,
9797 insideMargin : DpSize = DpSize (16.dp, 16.dp),
98- popupHorizontalPadding : Dp = 12.dp,
9998 defaultWindowInsetsPadding : Boolean = true,
10099 selectedIndex : Int ,
101100 onSelectedIndexChange : (Int ) -> Unit ,
@@ -114,11 +113,13 @@ fun SuperDropdown(
114113 val textMeasurer = rememberTextMeasurer()
115114 val textStyle = remember { TextStyle (fontWeight = FontWeight .Medium , fontSize = 17 .sp) }
116115 val textWidthDp = remember(items) { items.maxOfOrNull { with (density) { textMeasurer.measure(text = it, style = textStyle).size.width.toDp() } } }
117- var dropdownHeightPx by remember { mutableStateOf(0 ) }
116+ val windowWeightPx by rememberUpdatedState(getWindowSize().width)
117+ val windowHeightPx by rememberUpdatedState(getWindowSize().height)
118118 var dropdownOffsetPx by remember { mutableStateOf(0 ) }
119+ var dropdownHeightPx by remember { mutableStateOf(0 ) }
119120 var componentHeightPx by remember { mutableStateOf(0 ) }
121+ var componentWidthPx by remember { mutableStateOf(0 ) }
120122 var offsetPx by remember { mutableStateOf(0 ) }
121- val windowHeightPx by rememberUpdatedState(getWindowSize().height)
122123 val statusBarPx by rememberUpdatedState(
123124 with (density) { WindowInsets .statusBars.asPaddingValues().calculateTopPadding().toPx() }.roundToInt()
124125 )
@@ -146,6 +147,7 @@ fun SuperDropdown(
146147 val positionInWindow = coordinates.positionInWindow()
147148 dropdownOffsetPx = positionInWindow.y.toInt()
148149 componentHeightPx = coordinates.size.height
150+ componentWidthPx = coordinates.size.width
149151 },
150152 insideMargin = insideMargin,
151153 title = title,
@@ -204,7 +206,7 @@ fun SuperDropdown(
204206 ) {
205207 LazyColumn (
206208 modifier = Modifier
207- .padding(horizontal = popupHorizontalPadding )
209+ .padding(horizontal = (windowWeightPx.dp - componentWidthPx.dp) / 2 / density.density )
208210 .onGloballyPositioned { layoutCoordinates ->
209211 dropdownHeightPx = layoutCoordinates.size.height
210212 offsetPx = calculateOffsetPx(
@@ -321,7 +323,7 @@ fun DropdownImpl(
321323 * @param windowHeightPx The height of the window.
322324 * @param dropdownOffsetPx The default offset of the dropdown.
323325 * @param dropdownHeightPx The height of the dropdown.
324- * @param componentHeight The height of the click component.
326+ * @param componentHeightPx The height of the component.
325327 * @param insideHeightPx The height of the component inside.
326328 * @param statusBarPx The height of the status bar padding.
327329 * @param navigationBarPx The height of the navigation bar padding.
@@ -332,16 +334,16 @@ fun calculateOffsetPx(
332334 windowHeightPx : Int ,
333335 dropdownOffsetPx : Int ,
334336 dropdownHeightPx : Int ,
335- componentHeight : Int ,
337+ componentHeightPx : Int ,
336338 insideHeightPx : Int ,
337339 statusBarPx : Int ,
338340 navigationBarPx : Int ,
339341 captionBarPx : Int
340342): Int {
341- return if (windowHeightPx - dropdownOffsetPx < dropdownHeightPx / 2 + captionBarPx + navigationBarPx + insideHeightPx + componentHeight / 2 ) {
343+ return if (windowHeightPx - dropdownOffsetPx < dropdownHeightPx / 2 + captionBarPx + navigationBarPx + insideHeightPx + componentHeightPx / 2 ) {
342344 windowHeightPx - dropdownHeightPx - insideHeightPx - captionBarPx - navigationBarPx
343345 } else {
344- val offset = dropdownOffsetPx - dropdownHeightPx / 2 + componentHeight / 2
346+ val offset = dropdownOffsetPx - dropdownHeightPx / 2 + componentHeightPx / 2
345347 if (offset > insideHeightPx + statusBarPx) offset else insideHeightPx + statusBarPx
346348 }
347349}
0 commit comments