Skip to content

PullToRefresh

PullToRefresh is a pull-to-refresh component in Miuix that provides refresh functionality for lists and other scrollable content. It features an animated interactive refresh indicator suitable for various scenarios where data refresh is needed.

WARNING

This component is only available in touch-enabled scenes and does not work well in the Web build target!

For a demonstration, see the DropDown page of the Miuix Example.

Import

kotlin
import top.yukonga.miuix.kmp.basic.PullToRefresh
import top.yukonga.miuix.kmp.basic.rememberPullToRefreshState

Basic Usage

PullToRefresh can wrap any scrollable content:

kotlin
var isRefreshing by rememberSaveable { mutableStateOf(false) }
val pullToRefreshState = rememberPullToRefreshState()
var items by remember { mutableStateOf(1) }

LaunchedEffect(isRefreshing) {
    if (isRefreshing) {
        delay(500)
        items += 6
        isRefreshing = false
    }
}

Surface {
    PullToRefresh(
        isRefreshing = isRefreshing,
        onRefresh = { isRefreshing = true },
        pullToRefreshState = pullToRefreshState,
    ) {
        LazyColumn(
            modifier = Modifier.fillMaxSize()
        ) {
            items(items) { index ->
                SuperArrow(
                    title = "Item $index",
                    modifier = Modifier.padding(horizontal = 16.dp),
                    onClick = { /* Click event */ }
                )
            }
        }
    }
}

Component States

PullToRefresh has the following states:

  1. Idle: Initial state, no interaction
  2. Pulling: User is pulling but hasn't reached the refresh threshold
  3. ThresholdReached: Pull threshold reached, release to refresh
  4. Refreshing: Currently refreshing
  5. RefreshComplete: Refresh completed, returning to initial state

Properties

PullToRefresh Properties

Property NameTypeDescriptionDefault ValueRequired
isRefreshingBooleanRefresh stateNoneYes
onRefresh() -> UnitRefresh callback functionNoneYes
modifierModifierContainer modifierModifierNo
pullToRefreshStatePullToRefreshStatePullToRefresh staterememberPullToRefreshState()No
contentPaddingPaddingValuesContent paddingPaddingValues(0.dp)No
topAppBarScrollBehaviorScrollBehaviorTop app bar scroll behaviornullNo
colorColorIndicator colorPullToRefreshDefaults.colorNo
circleSizeDpIndicator circle sizePullToRefreshDefaults.circleSizeNo
refreshTextsList<String>Text list for different statesPullToRefreshDefaults.refreshTextsNo
refreshTextStyleTextStyleRefresh text stylePullToRefreshDefaults.refreshTextStyleNo
content@Composable () -> UnitScrollable content composableNoneYes

PullToRefreshState Class

PullToRefreshState manages the UI state of the refresh indicator and can be created using rememberPullToRefreshState(). It should only be used for UI state, while refresh logic should be controlled by isRefreshing and onRefresh.

Property NameTypeDescription
refreshStateRefreshStateCurrent refresh state
isRefreshingBooleanWhether it is refreshing
pullProgressFloatPull progress (0-1)
refreshCompleteAnimProgressFloatRefresh complete animation

PullToRefreshDefaults Object

PullToRefreshDefaults provides default values for the component.

Property NameTypeDescriptionDefault Value
colorColorDefault indicator colorColor.Gray
circleSizeDpDefault indicator size20.dp
refreshTextsList<String>Default text list["Pull down to refresh", "Release to refresh", "Refreshing...", "Refreshed successfully"]
refreshTextStyleTextStyleDefault text styleTextStyle(fontSize = 14.sp, fontWeight = Bold, color = color)

Advanced Usage

Custom Indicator Color

kotlin
PullToRefresh(
    color = Color.Blue,
    // Other properties
) {
    // Content
}

Custom Refresh Texts

kotlin
PullToRefresh(
    refreshTexts = listOf(
        "Pull to refresh",
        "Release to refresh",
        "Refreshing",
        "Refresh successful",
    ),
    // Other properties
) {
    // Content
}

Released under the Apache-2.0 License