Created
January 12, 2023 10:49
-
-
Save shakil807g/5e65789171c8a48df380a433297671ff to your computer and use it in GitHub Desktop.
SwipeToDismissDemo
This file contains hidden or 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
val list = mutableStateListOf("a","b","c") | |
LazyColumn(Modifier.fillMaxSize()) { | |
items(list,{ it }) { item -> | |
val scope = rememberCoroutineScope() | |
val undoScope = rememberCoroutineScope() | |
val dismissState = rememberDismissState( | |
confirmStateChange = { | |
if(it == DismissValue.DismissedToStart) { | |
scope.launch { | |
delay(4000) | |
list.remove(item) | |
} | |
} | |
true | |
} | |
) | |
SwipeToDismiss( | |
state = dismissState, | |
dismissThresholds = { FractionalThreshold(0.4f) }, | |
background = { | |
val direction = dismissState.dismissDirection | |
val color by animateColorAsState(targetValue = | |
when (dismissState.targetValue) { | |
DismissValue.DismissedToStart -> Color.Green | |
DismissValue.DismissedToEnd -> Color.Red | |
DismissValue.Default -> Color.Gray | |
}) | |
if (direction == DismissDirection.StartToEnd) { | |
Box( | |
modifier = Modifier | |
.fillMaxSize() | |
.background(color) | |
.padding(8.dp) | |
) { | |
Column(modifier = Modifier.align(Alignment.CenterStart)) { | |
Icon( | |
imageVector = Icons.Default.ArrowForward, | |
contentDescription = null, | |
tint = Color.White, | |
modifier = Modifier.align(Alignment.CenterHorizontally) | |
) | |
Text( | |
text = "Move to Archive", fontWeight = FontWeight.Bold, | |
textAlign = TextAlign.Center, | |
color = Color.White | |
) | |
} | |
} | |
} else { | |
val aligment = when (dismissState.targetValue) { | |
DismissValue.DismissedToStart -> Alignment.End | |
DismissValue.DismissedToEnd -> Alignment.Start | |
DismissValue.Default -> null | |
} | |
if(aligment == null) { | |
Box( | |
modifier = Modifier | |
.fillMaxSize() | |
.background(color) | |
.padding(8.dp) | |
) { | |
Row(modifier = Modifier.fillMaxWidth()) { | |
Column(modifier = Modifier.weight(1f)) { | |
Icon( | |
imageVector = Icons.Default.ArrowBack, | |
contentDescription = null, | |
tint = Color.White, | |
modifier = Modifier.align(Alignment.CenterHorizontally) | |
) | |
Spacer(modifier = Modifier.heightIn(5.dp)) | |
Text( | |
text = "Move to Bin", | |
textAlign = TextAlign.Center, | |
fontWeight = FontWeight.Bold, | |
color = Color.LightGray | |
) | |
} | |
Text( | |
text = "Move to Bin", | |
textAlign = TextAlign.Center, | |
fontWeight = FontWeight.Bold, | |
color = Color.LightGray | |
) | |
} | |
} | |
} else { | |
Box( | |
modifier = Modifier | |
.fillMaxSize() | |
.background(color) | |
.padding(8.dp) | |
) { | |
Row(modifier = Modifier.fillMaxWidth()) { | |
Column(modifier = Modifier.weight(1f)) { | |
Icon( | |
imageVector = Icons.Default.ArrowBack, | |
contentDescription = null, | |
tint = Color.White, | |
modifier = Modifier.align(Alignment.CenterHorizontally) | |
) | |
Spacer(modifier = Modifier.heightIn(5.dp)) | |
Text( | |
text = "Move to Bin", | |
textAlign = TextAlign.Center, | |
fontWeight = FontWeight.Bold, | |
color = Color.LightGray | |
) | |
} | |
Button(modifier = Modifier, onClick = { | |
undoScope.launch { | |
dismissState.reset() | |
scope.cancel() | |
} | |
}) { | |
Text("UNDO") | |
} | |
} | |
} | |
} | |
} | |
}, | |
/**** Dismiss Content */ | |
dismissContent = { | |
androidx.compose.material.Card(modifier = Modifier.fillMaxWidth(), elevation = | |
animateDpAsState(targetValue = if(dismissState.dismissDirection != null) 4.dp else 0.dp).value) { | |
Text(item, modifier = Modifier | |
.fillMaxWidth() | |
.padding(30.dp)) | |
} | |
}, | |
directions = setOf(DismissDirection.EndToStart, DismissDirection.StartToEnd), | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment