[go: nahoru, domu]

blob: c8910aca6aebb9fd539fffd3186a834aee4b0b0d [file] [log] [blame]
Matvei Malkov0f236612019-07-04 15:54:58 +01001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.ui.material.samples
18
19import androidx.annotation.Sampled
20import androidx.compose.Composable
Matvei Malkov0f236612019-07-04 15:54:58 +010021import androidx.compose.state
Matvei Malkov0f236612019-07-04 15:54:58 +010022import androidx.ui.core.Text
Clara Bayarri0e00c9642019-07-02 13:08:40 +010023import androidx.ui.foundation.ColoredRect
Matvei Malkov0f236612019-07-04 15:54:58 +010024import androidx.ui.graphics.Color
Matvei Malkov0f236612019-07-04 15:54:58 +010025import androidx.ui.layout.Center
26import androidx.ui.layout.Column
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010027import androidx.ui.layout.Container
Adam Powell712dc992019-12-04 12:48:30 -080028import androidx.ui.layout.LayoutHeight
Adam Powell31c1ebd2020-01-09 09:48:24 -080029import androidx.ui.layout.LayoutWidth
Matvei Malkov0f236612019-07-04 15:54:58 +010030import androidx.ui.layout.Row
George Mount842c8c12020-01-08 16:03:42 -080031import androidx.ui.layout.Spacer
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010032import androidx.ui.material.BottomDrawerLayout
Matvei Malkov0f236612019-07-04 15:54:58 +010033import androidx.ui.material.Button
34import androidx.ui.material.DrawerState
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010035import androidx.ui.material.ModalDrawerLayout
Matvei Malkov0f236612019-07-04 15:54:58 +010036import androidx.ui.material.StaticDrawer
George Mount842c8c12020-01-08 16:03:42 -080037import androidx.ui.unit.dp
Matvei Malkov0f236612019-07-04 15:54:58 +010038
39@Sampled
40@Composable
41fun StaticDrawerSample() {
Adam Powell31c1ebd2020-01-09 09:48:24 -080042 Row(LayoutWidth.Fill) {
Matvei Malkov0f236612019-07-04 15:54:58 +010043 StaticDrawer {
44 Center {
45 Text("Drawer Content")
46 }
47 }
48 ColoredRect(Color.Black, width = 1.dp)
49 Text("Rest of App")
50 }
51}
52
53@Sampled
54@Composable
55fun ModalDrawerSample() {
Leland Richardson7f848ab2019-12-12 13:43:41 -080056 val (state, onStateChange) = state { DrawerState.Closed }
Matvei Malkov0f236612019-07-04 15:54:58 +010057 val appContentText =
58 if (state == DrawerState.Closed) ">>> Pull to open >>>" else "<<< Swipe to close <<<"
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010059 ModalDrawerLayout(
60 drawerState = state,
61 onStateChange = onStateChange,
62 drawerContent = { YourDrawerContent(onStateChange) },
63 bodyContent = { YourAppContent(appContentText, onStateChange) }
64 )
Matvei Malkov0f236612019-07-04 15:54:58 +010065}
66
67@Sampled
68@Composable
69fun BottomDrawerSample() {
Leland Richardson7f848ab2019-12-12 13:43:41 -080070 val (state, onStateChange) = state { DrawerState.Closed }
Matvei Malkov0f236612019-07-04 15:54:58 +010071 val appContentText =
72 if (state == DrawerState.Closed) "▲▲▲ Pull to open ▲▲▲" else "▼▼▼ Drag down to close ▼▼▼"
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010073 BottomDrawerLayout(
74 drawerState = state,
75 onStateChange = onStateChange,
76 drawerContent = { YourDrawerContent(onStateChange) },
77 bodyContent = { YourAppContent(appContentText, onStateChange) }
78 )
79}
80
81@Composable
82private fun YourDrawerContent(onStateChange: (DrawerState) -> Unit) {
83 Container(expanded = true) {
Adam Powell31c1ebd2020-01-09 09:48:24 -080084 Column(LayoutHeight.Fill) {
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010085 Text(text = "Drawer Content")
Adam Powell712dc992019-12-04 12:48:30 -080086 Spacer(LayoutHeight(20.dp))
Louis Pullen-Freiliche386f97dd2020-01-31 17:32:33 +000087 Button(onClick = { onStateChange(DrawerState.Closed) }) {
88 Text("Close Drawer")
89 }
Matvei Malkov0f236612019-07-04 15:54:58 +010090 }
91 }
92}
93
94@Composable
95private fun YourAppContent(text: String, onDrawerStateChange: (DrawerState) -> Unit) {
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010096 Center {
Adam Powell31c1ebd2020-01-09 09:48:24 -080097 Column(LayoutHeight.Fill) {
Matvei Malkov7aa7fd22019-08-16 16:52:59 +010098 Text(text = text)
Adam Powell712dc992019-12-04 12:48:30 -080099 Spacer(LayoutHeight(20.dp))
Louis Pullen-Freiliche386f97dd2020-01-31 17:32:33 +0000100 Button(onClick = { onDrawerStateChange(DrawerState.Opened) }) {
101 Text("Click to open")
102 }
Matvei Malkov7aa7fd22019-08-16 16:52:59 +0100103 }
Matvei Malkov0f236612019-07-04 15:54:58 +0100104 }
105}