feat: QMUITopBarWithLazyScrollState

This commit is contained in:
cgspine
2021-12-28 20:54:34 +08:00
parent ba20d0cc17
commit 3f2de373a7
4 changed files with 92 additions and 37 deletions
-2
View File
@@ -1,6 +1,4 @@
import com.qmuiteam.plugin.Dep
import java.io.FileInputStream
import java.util.*
plugins {
id("com.android.library")
@@ -23,6 +23,7 @@ import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.qmuiteam.compose.ui.qmuiCommonHorSpace
import com.qmuiteam.compose.ui.qmuiPrimaryColor
val DefaultDialogPaddingHor = 20.dp
@@ -33,7 +34,7 @@ fun QMUIDialog(
horEdge: Dp = qmuiCommonHorSpace,
verEdge: Dp = 20.dp,
widthLimit: Dp = 360.dp,
radius: Dp = 12.dp,
radius: Dp = 2.dp,
background: Color = Color.White,
content: @Composable (QMUIModal) -> Unit
) {
@@ -157,7 +158,7 @@ fun QMUIDialogMsgContent(
fun QMUIDialogAction(
text: String,
fontSize: TextUnit = 14.sp,
color: Color = Color.Blue,
color: Color = qmuiPrimaryColor,
fontWeight: FontWeight? = FontWeight.Bold,
fontFamily: FontFamily? = null,
paddingVer:Dp = 9.dp,
@@ -4,8 +4,14 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
val qmuiPrimaryColor = Color(0xFF00A8E1)
val qmuiSeparatorColor = Color.White.copy(alpha = 0.5f)
val qmuiSeparatorColor = Color(0xFFCCCCCC)
val qmuiTopBarHeight = 48.dp
val qmuiTopBarZIndex = 32f
val qmuiCommonHorSpace = 20.dp
val qmuiScrollAlphaChangeMaxOffset = 20.dp
@@ -4,33 +4,32 @@ import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.*
import androidx.compose.ui.layout.*
import androidx.compose.ui.platform.InspectorInfo
import androidx.compose.ui.platform.InspectorValueInfo
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.debugInspectorInfo
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.*
import androidx.compose.ui.zIndex
import androidx.core.view.WindowInsetsCompat
import com.qmuiteam.compose.R
import com.qmuiteam.compose.helper.OnePx
import com.qmuiteam.compose.provider.QMUILocalWindowInsets
import com.qmuiteam.compose.provider.dp
val qmuiTopBarHeight = 48.dp
fun interface QMUITopBarItem {
@Composable
fun Compose(topBarHeight: Dp)
@@ -166,15 +165,54 @@ open class QMUITopBarTextItem(
}
@Composable
fun QMUITopBarWithLazyScrollState(
scrollState: LazyListState,
title: CharSequence,
subTitle: CharSequence = "",
alignTitleCenter: Boolean = true,
height: Dp = qmuiTopBarHeight,
zIndex: Float = qmuiTopBarZIndex,
backgroundColor: Color = qmuiPrimaryColor,
changeWithBackground: Boolean = false,
scrollAlphaChangeMaxOffset: Dp = qmuiScrollAlphaChangeMaxOffset,
shadowElevation: Dp = 16.dp,
shadowAlpha: Float = 0.6f,
separatorHeight: Dp = OnePx(),
separatorColor: Color = qmuiSeparatorColor,
paddingStart: Dp = 4.dp,
paddingEnd: Dp = 4.dp,
titleBoxPaddingHor: Dp = 8.dp,
leftItems: List<QMUITopBarItem> = emptyList(),
rightItems: List<QMUITopBarItem> = emptyList(),
titleLayout: QMUITopBarTitleLayout = remember { DefaultQMUITopBarTitleLayout() }
){
val percent = with(LocalDensity.current){
if(scrollState.firstVisibleItemIndex > 0 || scrollState.firstVisibleItemScrollOffset.toDp() > scrollAlphaChangeMaxOffset){
1f
} else scrollState.firstVisibleItemScrollOffset.toDp() / scrollAlphaChangeMaxOffset
}
QMUITopBar(
title, subTitle,
alignTitleCenter, height, zIndex,
if(changeWithBackground) backgroundColor.copy(backgroundColor.alpha * percent) else backgroundColor,
shadowElevation, shadowAlpha * percent,
separatorHeight, separatorColor.copy(separatorColor.alpha * percent),
paddingStart, paddingEnd,
titleBoxPaddingHor, leftItems, rightItems, titleLayout
)
}
@Composable
fun QMUITopBar(
title: CharSequence,
subTitle: CharSequence = "",
alignTitleCenter: Boolean = true,
height: Dp = qmuiTopBarHeight,
zIndex: Float = qmuiTopBarZIndex,
backgroundColor: Color = qmuiPrimaryColor,
shadowElevation: Dp = 16.dp,
shadowShape: Shape = RectangleShape,
shadowAlpha: Float = 0.4f,
separatorHeight: Dp = OnePx(),
separatorColor: Color = qmuiSeparatorColor,
paddingStart: Dp = 4.dp,
@@ -187,35 +225,47 @@ fun QMUITopBar(
val insets = QMUILocalWindowInsets.current.getInsetsIgnoringVisibility(
WindowInsetsCompat.Type.statusBars() or WindowInsetsCompat.Type.displayCutout()
).dp()
Box(
modifier = Modifier
.fillMaxWidth()
.shadow(shadowElevation, shadowShape)
.background(backgroundColor)
.padding(top = insets.top)
.height(height)
) {
QMUITopBarContent(
title,
subTitle,
alignTitleCenter,
height,
paddingStart,
paddingEnd,
titleBoxPaddingHor,
leftItems,
rightItems,
titleLayout
)
if(separatorHeight > 0.dp && separatorColor != Color.Transparent){
Box(modifier = Modifier
Box(modifier = Modifier
.fillMaxWidth()
.height(IntrinsicSize.Max)
.zIndex(zIndex)
){
Box(modifier = Modifier.fillMaxSize().graphicsLayer {
this.alpha = shadowAlpha
this.shadowElevation = shadowElevation.toPx()
this.shape = RectangleShape
this.clip = shadowElevation > 0.dp
})
Box(
modifier = Modifier
.fillMaxWidth()
.height(separatorHeight)
.align(Alignment.BottomStart)
.background(separatorColor)
.background(backgroundColor)
.padding(top = insets.top)
.height(height)
) {
QMUITopBarContent(
title,
subTitle,
alignTitleCenter,
height,
paddingStart,
paddingEnd,
titleBoxPaddingHor,
leftItems,
rightItems,
titleLayout
)
if(separatorHeight > 0.dp && separatorColor != Color.Transparent){
Box(modifier = Modifier
.fillMaxWidth()
.height(separatorHeight)
.align(Alignment.BottomStart)
.background(separatorColor)
)
}
}
}
}
@Composable