-
-
Save mahmoud-abdallah863/867390033528ebf12101bc803da903ee to your computer and use it in GitHub Desktop.
top bar with center title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.klikatech.rollo.ui.compose.components | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.material.* | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.RectangleShape | |
import androidx.compose.ui.graphics.Shape | |
import androidx.compose.ui.layout.onGloballyPositioned | |
import androidx.compose.ui.platform.LocalDensity | |
import androidx.compose.ui.unit.Dp | |
import androidx.compose.ui.unit.dp | |
import com.klikatech.rollo.ui.extensions.common.toPx | |
import kotlin.math.abs | |
import kotlin.math.max | |
val AppBarHeight = 56.dp | |
val AppBarHorizontalPadding = 4.dp | |
val TitleIconModifier = Modifier.fillMaxHeight() | |
var iconWidth = 72.dp - AppBarHorizontalPadding | |
var withoutIconWidth = 16.dp - AppBarHorizontalPadding | |
@Composable | |
fun CenterTopAppBar( | |
title: @Composable () -> Unit, | |
modifier: Modifier = Modifier, | |
navigationIcon: @Composable (() -> Unit)? = null, | |
actions: @Composable RowScope.() -> Unit = {}, | |
backgroundColor: Color = MaterialTheme.colors.primarySurface, | |
contentColor: Color = contentColorFor(backgroundColor), | |
elevation: Dp = AppBarDefaults.TopAppBarElevation | |
) { | |
val defLeftSectionWidth = if (navigationIcon == null) withoutIconWidth else iconWidth | |
var leftSectionWidth by remember { mutableStateOf(defLeftSectionWidth) } | |
var rightSectionWidth by remember { mutableStateOf(-1f) } | |
var rightSectionPadding by remember { mutableStateOf(0f) } | |
AppBar( | |
backgroundColor, | |
contentColor, | |
elevation, | |
AppBarDefaults.ContentPadding, | |
RectangleShape, | |
modifier | |
) { | |
if (navigationIcon == null) { | |
Spacer(Modifier.width(leftSectionWidth)) | |
} else { | |
Row( | |
TitleIconModifier.width(leftSectionWidth), | |
verticalAlignment = Alignment.CenterVertically | |
) { | |
CompositionLocalProvider( | |
LocalContentAlpha provides ContentAlpha.high, | |
content = navigationIcon | |
) | |
} | |
} | |
Row( | |
Modifier | |
.fillMaxHeight() | |
.weight(1f), | |
verticalAlignment = Alignment.CenterVertically, | |
horizontalArrangement = Arrangement.Center | |
) { | |
if (leftSectionWidth != defLeftSectionWidth | |
|| rightSectionPadding != 0f | |
) { | |
ProvideTextStyle(value = MaterialTheme.typography.h6) { | |
CompositionLocalProvider( | |
LocalContentAlpha provides ContentAlpha.high, | |
content = title | |
) | |
} | |
} | |
} | |
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) { | |
with(LocalDensity.current) { | |
Row( | |
Modifier | |
.fillMaxHeight() | |
.padding(start = rightSectionPadding.toDp()) | |
.onGloballyPositioned { | |
rightSectionWidth = it.size.width.toFloat() | |
if (leftSectionWidth == defLeftSectionWidth | |
&& rightSectionWidth != -1f | |
&& rightSectionPadding == 0f | |
) { | |
/* | |
Find the maximum width of the sections (left or right). | |
As a result, both sections should have the same width. | |
*/ | |
val maxWidth = max(leftSectionWidth.value.toPx, rightSectionWidth) | |
leftSectionWidth = maxWidth.toDp() | |
rightSectionPadding = abs(rightSectionWidth - maxWidth) | |
} | |
}, | |
horizontalArrangement = Arrangement.End, | |
verticalAlignment = Alignment.CenterVertically, | |
content = actions | |
) | |
} | |
} | |
} | |
} | |
@Composable | |
fun AppBar( | |
backgroundColor: Color, | |
contentColor: Color, | |
elevation: Dp, | |
contentPadding: PaddingValues, | |
shape: Shape, | |
modifier: Modifier = Modifier, | |
content: @Composable RowScope.() -> Unit | |
) { | |
Surface( | |
color = backgroundColor, | |
contentColor = contentColor, | |
elevation = elevation, | |
shape = shape, | |
modifier = modifier | |
) { | |
Row( | |
Modifier | |
.fillMaxWidth() | |
.padding(contentPadding) | |
.height(AppBarHeight), | |
horizontalArrangement = Arrangement.Start, | |
verticalAlignment = Alignment.CenterVertically, | |
content = content | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment