GithubHelp home page GithubHelp logo

amenon / compose-collapsing-toolbar Goto Github PK

View Code? Open in Web Editor NEW

This project forked from onebone/compose-collapsing-toolbar

0.0 1.0 0.0 2.58 MB

A simple implementation of collapsing toolbar for Jetpack Compose

License: MIT License

Kotlin 100.00%

compose-collapsing-toolbar's Introduction

compose-collapsing-toolbar

A simple implementation of CollapsingToolbarLayout for Jetpack Compose

Installation

You should add mavenCentral() repository before installation. Then add the following line to the dependencies block in your app level build.gradle:

implementation "me.onebone:toolbar-compose:2.0.1"

or build.gradle.kts:

implementation("me.onebone:toolbar-compose:2.0.1")

Example

An example can be found here.

Usage

Connecting AppBarContainer and CollapsingToolbar

The first step you should do to use collapsing toolbar is to connect AppBarContainer and CollapsingToolbar. This can be accomplished by passing the same CollapsingToolbarState instance to AppBarContainer and CollapsingToolbar. A most common way to get the state is by using rememberCollapsingToolbarState():

val state = rememberCollapsingToolbarState()

AppBarContainer(
    collapsingToolbarState = state,
    /* ... */
) {
    CollapsingToolbar(
        // Be sure to pass the same CollapsingToolbarState instance you passed to AppBarContainer
        collapsingToolbarState = state,
        /* ... */
    ) {
        /* ... */
    }
}

Adding child to CollapsingToolbar

Similar to CollapsingToolbarLayout, you may add children to the CollapsingToolbar. The toolbar will collapse until it gets as small as the smallest child, and will expand as large as the largest child.

Adding child to AppBarContainer

The AppBarContainer may consist of at most one CollapsingToolbar and unlimited number of body composable. Each body composable should be marked with appBarBody() modifier:

AppBarContainer(/* ... */) {
    CollapsingToolbar(/* ... */) {
        /* ... */
    }

    LazyColumn(
        modifier = Modifier
            .appBarBody() // <<--- body composable should be marked with `appBarBody()` modifier 
    ) {
        /* ... */
    }
}

Note that the CollapsingToolbar only if the body composable is scrollable. You don't need to care about anything when using LazyColumn because it is scrollable by default, however, if you hope to use non-scrollable such as Column or Row as body you should use verticalScroll() modifier for CollapsingToolbar to inspect nested scroll.

AppBarContainer(/* ... */) {
    CollapsingToolbar(/* ... */) {
        /* ... */
    }

    Column(
        modifier = Modifier
            .verticalScroll(rememberScrollState())
            // ^^ body composable should be scrollable for collapsing toolbar to play with nested scroll
            .appBarBody()
    ) {
        /* ... */
    }
}

parallax, pin, road

You can tell children of CollapsingToolbar how to deal with a collapse/expansion. This works almost the same way to the collapseMode in the CollapsingToolbarLayout except for the road modifier.

CollapsingToolbar(/* ... */) {
    Image(
        modifier = Modifier.parallax(ratio = 0.2f) // parallax, pin, road are available
    )
}

road modifier

The road() modifier allows you to place a child relatively to the toolbar. It receives two arguments: whenCollapsed and whenExpanded. As the name suggests, these describe how to place a child when the toolbar is collapsed or expanded, respectively. This can be used to display a title text on the toolbar which is moving as the scroll is fed.

CollapsingToolbar(/* ... */) {
    Text(
        text = "Title",
        modifier = Modifier
            .road(
                whenCollapsed = Alignment.CenterStart,
                whenExpanded = Alignment.BottomEnd
            )
    )
}

The above code orders the title Text to be placed at the CenterStart position when the toolbar is collapsed and BottomEnd position when it is expanded.

Scroll Strategy

ScrollStrategy defines how CollapsingToolbar consumes scroll. You can set your desired behavior by providing scrollStrategy at AppBarContainer:

AppBarContainer(
    /* ... */
    scrollStrategy = ScrollStrategy.EnterAlways // EnterAlways, EnterAlwaysCollapsed, ExitUntilCollapsed are available
) {
    /* ... */
}

ScrollStrategy.EnterAlways

EnterAlways

ScrollStrategy.EnterAlwaysCollapsed

EnterAlwaysCollapsed

ScrollStrategy.ExitUntilCollapsed

ExitUntilCollapsed

compose-collapsing-toolbar's People

Contributors

onebone avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.