-
-
Notifications
You must be signed in to change notification settings - Fork 355
/
MaterialActivity.kt
741 lines (677 loc) 路 35.4 KB
/
MaterialActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
package com.example.jetpackcompose.material
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.Checkbox
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.LinearProgressIndicator
import androidx.compose.material.ListItem
import androidx.compose.material.MaterialTheme
import androidx.compose.material.RadioButton
import androidx.compose.material.Slider
import androidx.compose.material.Snackbar
import androidx.compose.material.Switch
import androidx.compose.material.Text
import androidx.compose.material.TriStateCheckbox
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.state.ToggleableState
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.jetpackcompose.R
import com.example.jetpackcompose.image.TitleComponent
class MaterialActivity : AppCompatActivity() {
@ExperimentalMaterialApi
@Suppress("LongMethod")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// This is an extension function of Activity that sets the @Composable function that's
// passed to it as the root view of the activity. This is meant to replace the .xml file
// that we would typically set using the setContent(R.id.xml_file) method. The setContent
// block defines the activity's layout.
setContent {
// LazyColumn is a vertically scrolling list that only composes and lays out the currently
// visible items. This is very similar to what RecyclerView tries to do as well.
LazyColumn {
// item is a DSL available in the LazyColumn scope. This allows you to render a composable
// for a single element in the list
item {
// Title Component is a custom composable that we created which is capable of
// rendering text on the screen in a certain font style & text size.
TitleComponent("This is a simple Material card")
MaterialCardComponent()
}
item {
TitleComponent("This is a loading progress indicator ")
MaterialLinearProgressIndicatorComponent()
}
item {
TitleComponent("This is a determinate progress indicator")
MaterialDeterminateLinearProgressIndicatorComponent()
}
item {
MaterialCircularProgressIndicatorComponent()
TitleComponent("This is a loading circular progress indicator")
}
item {
TitleComponent("This is a determinate circular progress indicator")
MaterialDeterminateCircularProgressIndicatorComponent()
}
item {
TitleComponent("This is a material Snackbar")
MaterialSnackbarComponent()
}
item {
TitleComponent("This is a non-discrete slider")
MaterialContinousSliderComponent()
}
item {
TitleComponent("This is a discrete slider")
MaterialDiscreteSliderComponent()
}
item {
TitleComponent("This is a checkbox that represents two states")
MaterialCheckboxComponent()
}
item {
TitleComponent("This is a checkbox that represents three states")
MaterialTriStateCheckboxComponent()
}
item {
TitleComponent("This is a radio button group")
MaterialRadioButtonGroupComponent()
}
item {
TitleComponent("This is a switch component")
MaterialSwitchComponent()
}
item {
TitleComponent("This is how you add a ripple effect to a view")
MaterialRippleComponent()
}
}
}
}
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun MaterialCardComponent() {
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
// You can think of Modifiers as implementations of the decorators pattern that are used to
// modify the composable that its applied to. In the example below, we add a padding of
// 8dp to the Card composable.
Card(shape = RoundedCornerShape(4.dp), modifier = Modifier.padding(8.dp)) {
// ListItem is a predefined composable that is a Material Design implementation of [list
// items](https://material.io/components/lists). This component can be used to achieve the
// list item templates existing in the spec
ListItem(text = {
Text(text = "Title")
}, secondaryText = {
Text(text = "Subtitle")
}, icon = {
// Column is a composable that places its children in a vertical sequence. You
// can think of it similar to a LinearLayout with the vertical orientation.
// In addition we also pass a few modifiers to it.
Column(
modifier = Modifier
.width(48.dp)
.height(48.dp)
) {
Image(
painter = painterResource(R.drawable.landscape),
contentDescription = "Landscape"
)
}
})
}
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@Composable
fun MaterialCheckboxComponent() {
// Reacting to state changes is the core behavior of Compose. You will notice a couple new
// keywords that are compose related - remember & mutableStateOf.remember{} is a helper
// composable that calculates the value passed to it only during the first composition. It then
// returns the same value for every subsequent composition. Next, you can think of
// mutableStateOf as an observable value where updates to this variable will redraw all
// the composable functions that access it. We don't need to explicitly subscribe at all. Any
// composable that reads its value will be recomposed any time the value
// changes. This ensures that only the composables that depend on this will be redraw while the
// rest remain unchanged. This ensures efficiency and is a performance optimization. It
// is inspired from existing frameworks like React.
var checked by remember { mutableStateOf(false) }
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
// You can think of Modifiers as implementations of the decorators pattern that are used to
// modify the composable that its applied to. In the example below, we add a padding of
// 8dp to the Card composable. In addition, we configure it out occupy the entire available
// width using the Modifier.fillMaxWidth() modifier.
Card(
shape = RoundedCornerShape(4.dp),
modifier = Modifier
.padding(8.dp)
.fillMaxWidth()
) {
// Row is a composable that places its children in a horizontal sequence. You can think of it
// similar to a LinearLayout with the horizontal orientation. In addition, we pass a modifier
// to the Row composable.
Row(modifier = Modifier.padding(16.dp)) {
// A pre-defined composable that's capable of rendering a checkbox with 2 values - on,
// & off. It honors the Material Design specification.
Checkbox(checked = checked,
onCheckedChange = {
checked = !checked
})
// The Text composable is pre-defined by the Compose UI library; you can use this
// composable to render text on the screen
Text(text = "Use Jetpack Compose", modifier = Modifier.padding(start = 8.dp))
}
}
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@Composable
fun MaterialTriStateCheckboxComponent() {
val toggleableStateArray =
listOf(ToggleableState.Off, ToggleableState.On, ToggleableState.Indeterminate)
// Reacting to state changes is the core behavior of Compose. You will notice a couple new
// keywords that are compose related - remember & mutableStateOf.remember{} is a helper
// composable that calculates the value passed to it only during the first composition. It then
// returns the same value for every subsequent composition. Next, you can think of
// mutableStateOf as an observable value where updates to this variable will redraw all
// the composable functions that access it. We don't need to explicitly subscribe at all. Any
// composable that reads its value will be recomposed any time the value
// changes. This ensures that only the composables that depend on this will be redraw while the
// rest remain unchanged. This ensures efficiency and is a performance optimization. It
// is inspired from existing frameworks like React.
var counter by remember { mutableStateOf(0) }
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
// You can think of Modifiers as implementations of the decorators pattern that are used to
// modify the composable that its applied to. In the example below, we add a padding of
// 8dp to the Card composable. In addition, we configure it out occupy the entire available
// width using the Modifier.fillMaxWidth() modifier.
Card(
shape = RoundedCornerShape(4.dp),
modifier = Modifier
.padding(8.dp)
.fillMaxWidth()
) {
// Row is a composable that places its children in a horizontal sequence. You can think of it
// similar to a LinearLayout with the horizontal orientation. In addition, we pass a modifier
// to the Row composable.
Row(modifier = Modifier.padding(16.dp)) {
// A pre-defined checkbox composable that's capable of rendering 3 values - on, off &
// indeterminate. It honors the Material Design specification.
TriStateCheckbox(
state = toggleableStateArray[counter % 3],
onClick = {
counter++
})
// The Text composable is pre-defined by the Compose UI library; you can use this
// composable to render text on the screen
Text(text = "Use Jetpack Compose", modifier = Modifier.padding(start = 8.dp))
}
}
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@Composable
fun MaterialRadioButtonGroupComponent() {
// Reacting to state changes is the core behavior of Compose. You will notice a couple new
// keywords that are compose related - remember & mutableStateOf.remember{} is a helper
// composable that calculates the value passed to it only during the first composition. It then
// returns the same value for every subsequent composition. Next, you can think of
// mutableStateOf as an observable value where updates to this variable will redraw all
// the composable functions that access it. We don't need to explicitly subscribe at all. Any
// composable that reads its value will be recomposed any time the value
// changes. This ensures that only the composables that depend on this will be redraw while the
// rest remain unchanged. This ensures efficiency and is a performance optimization. It
// is inspired from existing frameworks like React.
var selected by remember { mutableStateOf("Android") }
val radioGroupOptions = listOf("Android", "iOS", "Windows")
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
// You can think of Modifiers as implementations of the decorators pattern that are used to
// modify the composable that its applied to. In the example below, we add a padding of
// 8dp to the Card composable. In addition, we configure it out occupy the entire available
// width using the Modifier.fillMaxWidth() modifier.
Card(
shape = RoundedCornerShape(4.dp),
modifier = Modifier
.padding(8.dp)
.fillMaxWidth()
) {
// A pre-defined composable that's capable of rendering a radio group. It honors the
// Material Design specification.
val onSelectedChange = { text: String ->
selected = text
}
Column {
radioGroupOptions.forEach { text ->
Row(Modifier
.fillMaxWidth()
.selectable(
selected = (text == selected),
onClick = { onSelectedChange(text) }
)
.padding(horizontal = 16.dp)
) {
RadioButton(
selected = (text == selected),
onClick = { onSelectedChange(text) }
)
Text(
text = text,
style = MaterialTheme.typography.body1.merge(),
modifier = Modifier.padding(start = 16.dp)
)
}
}
}
}
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@Composable
fun MaterialLinearProgressIndicatorComponent() {
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
// You can think of Modifiers as implementations of the decorators pattern that are used to
// modify the composable that its applied to. In the example below, we add a padding of
// 8dp to the Card composable. In addition, we configure it out occupy the entire available
// width using the Modifier.fillMaxWidth() modifier.
Card(
shape = RoundedCornerShape(4.dp),
modifier = Modifier
.padding(8.dp)
.fillMaxWidth()
) {
// Row is a composable that places its children in a horizontal sequence. You can think of it
// similar to a LinearLayout with the horizontal orientation. In addition, we pass a modifier
// to the Row composable.
Row(modifier = Modifier.padding(16.dp)) {
// A pre-defined composable that's capable of rendering a progress indicator. It honors
// the Material Design specification. It has fixed width as per Material spec - 240dp
LinearProgressIndicator()
}
}
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@Composable
fun MaterialDeterminateLinearProgressIndicatorComponent() {
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
// You can think of Modifiers as implementations of the decorators pattern that are used to
// modify the composable that its applied to. In the example below, we add a padding of
// 8dp to the Card composable. In addition, we configure it out occupy the entire available
// width using the Modifier.fillMaxWidth() modifier.
Card(
shape = RoundedCornerShape(4.dp),
modifier = Modifier
.padding(8.dp)
.fillMaxWidth()
) {
// Row is a composable that places its children in a horizontal sequence. You can think of it
// similar to a LinearLayout with the horizontal orientation. In addition, we pass a modifier
// to the Row composable.
Row(modifier = Modifier.padding(16.dp)) {
// A pre-defined composable that's capable of rendering a progress indicator. It honors
// the Material Design specification. It has fixed width as per Material spec - 240dp
LinearProgressIndicator(progress = 0.3f)
}
}
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@Composable
fun MaterialCircularProgressIndicatorComponent() {
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
// You can think of Modifiers as implementations of the decorators pattern that are used to
// modify the composable that its applied to. In the example below, we add a padding of
// 8dp to the Card composable. In addition, we configure it out occupy the entire available
// width using the Modifier.fillMaxWidth() modifier.
Card(
shape = RoundedCornerShape(4.dp),
modifier = Modifier
.padding(8.dp)
.fillMaxWidth()
) {
// A pre-defined composable that's capable of rendering a circular progress indicator. It
// honors the Material Design specification.
CircularProgressIndicator(modifier = Modifier.wrapContentWidth(Alignment.CenterHorizontally))
}
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@Composable
fun MaterialDeterminateCircularProgressIndicatorComponent() {
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
// You can think of Modifiers as implementations of the decorators pattern that are used to
// modify the composable that its applied to. In the example below, we add a padding of
// 8dp to the Card composable. In addition, we configure it out occupy the entire available
// width using the Modifier.fillMaxWidth() modifier.
Card(
shape = RoundedCornerShape(4.dp),
modifier = Modifier
.padding(8.dp)
.fillMaxWidth()
) {
// A pre-defined composable that's capable of rendering a circular progress indicator. It
// honors the Material Design specification.
CircularProgressIndicator(
progress = 0.5f,
modifier = Modifier.wrapContentWidth(Alignment.CenterHorizontally)
)
}
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@Composable
fun MaterialSnackbarComponent() {
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
// You can think of Modifiers as implementations of the decorators pattern that are used to
// modify the composable that its applied to. In the example below, we add a padding of
// 8dp to the Card composable.
Card(shape = RoundedCornerShape(4.dp), modifier = Modifier.padding(8.dp)) {
// A pre-defined composable that's capable of rendering a Snackbar. It honors the Material
// Design specification.
Snackbar(
content = {
// The Text composable is pre-defined by the Compose UI library; you can use this
// composable to render text on the screen
Text(text = "I'm a very nice Snackbar")
}, action = {
Text(text = "OK", style = TextStyle(color = MaterialTheme.colors.secondary))
}
)
}
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@Composable
fun MaterialContinousSliderComponent() {
// Reacting to state changes is the core behavior of Compose. You will notice a couple new
// keywords that are compose related - remember & mutableStateOf.remember{} is a helper
// composable that calculates the value passed to it only during the first composition. It then
// returns the same value for every subsequent composition. Next, you can think of
// mutableStateOf as an observable value where updates to this variable will redraw all
// the composable functions that access it. We don't need to explicitly subscribe at all. Any
// composable that reads its value will be recomposed any time the value
// changes. This ensures that only the composables that depend on this will be redraw while the
// rest remain unchanged. This ensures efficiency and is a performance optimization. It
// is inspired from existing frameworks like React.
var sliderValue by remember { mutableStateOf(0f) }
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
// You can think of Modifiers as implementations of the decorators pattern that are used to
// modify the composable that its applied to. In the example below, we add a padding of
// 8dp to the Card composable.
Card(shape = RoundedCornerShape(4.dp), modifier = Modifier.padding(8.dp)) {
// A pre-defined composable that's capable of rendering a slider. It
// honors the Material Design specification.
Slider(value = sliderValue, onValueChange = { newValue ->
sliderValue = newValue
})
}
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@Composable
fun MaterialDiscreteSliderComponent() {
// Reacting to state changes is the core behavior of Compose. You will notice a couple new
// keywords that are compose related - remember & mutableStateOf.remember{} is a helper
// composable that calculates the value passed to it only during the first composition. It then
// returns the same value for every subsequent composition. Next, you can think of
// mutableStateOf as an observable value where updates to this variable will redraw all
// the composable functions that access it. We don't need to explicitly subscribe at all. Any
// composable that reads its value will be recomposed any time the value
// changes. This ensures that only the composables that depend on this will be redraw while the
// rest remain unchanged. This ensures efficiency and is a performance optimization. It
// is inspired from existing frameworks like React.
var sliderValue by remember { mutableStateOf(0f) }
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
// You can think of Modifiers as implementations of the decorators pattern that are used to
// modify the composable that its applied to. In the example below, we add a padding of
// 8dp to the Card composable.
Card(shape = RoundedCornerShape(4.dp), modifier = Modifier.padding(8.dp)) {
// A pre-defined composable that's capable of rendering a slider. It honors the Material
// Design specification. In this example, we create a discrete slider with fixed steps.
Slider(
value = sliderValue,
valueRange = 0f..10f,
steps = 4,
onValueChange = { sliderValue = it })
}
Text(
text = "Slider value is %.1f".format(sliderValue),
modifier = Modifier.padding(8.dp)
)
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@Composable
fun MaterialSwitchComponent() {
// Reacting to state changes is the core behavior of Compose. You will notice a couple new
// keywords that are compose related - remember & mutableStateOf.remember{} is a helper
// composable that calculates the value passed to it only during the first composition. It then
// returns the same value for every subsequent composition. Next, you can think of
// mutableStateOf as an observable value where updates to this variable will redraw all
// the composable functions that access it. We don't need to explicitly subscribe at all. Any
// composable that reads its value will be recomposed any time the value
// changes. This ensures that only the composables that depend on this will be redraw while the
// rest remain unchanged. This ensures efficiency and is a performance optimization. It
// is inspired from existing frameworks like React.
var checked by remember { mutableStateOf(false) }
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
// You can think of Modifiers as implementations of the decorators pattern that are used to
// modify the composable that its applied to. In the example below, we add a padding of
// 8dp to the Card composable. In addition, we configure it out occupy the entire available
// width using the Modifier.fillMaxWidth() modifier.
Card(
shape = RoundedCornerShape(4.dp),
modifier = Modifier
.padding(8.dp)
.fillMaxWidth(),
backgroundColor = Color(249, 249, 249)
) {
// Row is a composable that places its children in a horizontal sequence. You can think of it
// similar to a LinearLayout with the horizontal orientation. In addition, we pass a modifier
// to the Row composable.
Row(modifier = Modifier.padding(16.dp)) {
// A pre-defined composable that's capable of rendering a switch. It honors the Material
// Design specification.
Switch(checked = checked, onCheckedChange = {
checked = !checked
})
// The Text composable is pre-defined by the Compose UI library; you can use this
// composable to render text on the screen
Text(text = "Enable Dark Mode", modifier = Modifier.padding(start = 8.dp))
}
}
}
// We represent a Composable function by annotating it with the @Composable annotation. Composable
// functions can only be called from within the scope of other composable functions. We should
// think of composable functions to be similar to lego blocks - each composable function is in turn
// built up of smaller composable functions.
@Composable
fun MaterialRippleComponent() {
// Card composable is a predefined composable that is meant to represent the card surface as
// specified by the Material Design specification. We also configure it to have rounded
// corners and apply a modifier.
// You can think of Modifiers as implementations of the decorators pattern that are used to
// modify the composable that its applied to. In the example below, we add a padding of
// 8dp to the Card composable. In addition, we configure it out occupy the entire available
// width using the Modifier.fillMaxWidth() modifier.
Card(
shape = RoundedCornerShape(4.dp),
modifier = Modifier
.padding(8.dp)
.fillMaxWidth()
) {
// Column with clickable modifier wraps the child composable and enables it to react to a
// click through the onClick callback similar to the onClick listener that we are accustomed
// to on Android. In order to show a ripple effect, we set indication of Modifier.clickable
// with a RippleIndication.
Column(
modifier = Modifier
.clickable(onClick = {})
.background(
color = Color.LightGray,
shape = RoundedCornerShape(4.dp)
)
) {
// The Text composable is pre-defined by the Compose UI library; you can use this
// composable to render text on the screen
Text(
text = "Click Me",
modifier = Modifier.padding(16.dp),
style = TextStyle(fontSize = 12.sp, fontFamily = FontFamily.Serif)
)
}
}
}
/**
* Android Studio lets you preview your composable functions within the IDE itself, instead of
* needing to download the app to an Android device or emulator. This is a fantastic feature as you
* can preview all your custom components(read composable functions) from the comforts of the IDE.
* The main restriction is, the composable function must not take any parameters. If your composable
* function requires a parameter, you can simply wrap your component inside another composable
* function that doesn't take any parameters and call your composable function with the appropriate
* params. Also, don't forget to annotate it with @Preview & @Composable annotations.
*/
@Preview("MaterialCardComponent")
@Composable
fun MaterialCardComponentPreview() {
MaterialCardComponent()
}
@Preview("MaterialCheckboxComponent")
@Composable
fun MaterialCheckboxComponentPreview() {
MaterialCheckboxComponent()
}
@Preview("MaterialTriStateCheckboxComponent")
@Composable
fun MaterialTriStateCheckboxComponentPreview() {
MaterialTriStateCheckboxComponent()
}
@Preview("MaterialRadioButtonGroupComponent")
@Composable
fun MaterialRadioButtonGroupComponentPreview() {
MaterialRadioButtonGroupComponent()
}
@Preview("MaterialLinearProgressIndicatorComponent")
@Composable
fun MaterialLinearProgressIndicatorComponentPreview() {
MaterialLinearProgressIndicatorComponent()
}
@Preview("MaterialDeterminateLinearProgressIndicatorComponent")
@Composable
fun MaterialDeterminateLinearProgressIndicatorComponentPreview() {
MaterialDeterminateLinearProgressIndicatorComponent()
}
@Preview("MaterialCircularProgressIndicatorComponent")
@Composable
fun MaterialCircularProgressIndicatorComponentPreview() {
MaterialCircularProgressIndicatorComponent()
}
@Preview("MaterialDeterminateCircularProgressIndicatorComponent")
@Composable
fun MaterialDeterminateCircularProgressIndicatorComponentPreview() {
MaterialDeterminateCircularProgressIndicatorComponent()
}
@Preview("MaterialSnackbarComponent")
@Composable
fun MaterialSnackbarComponentPreview() {
MaterialSnackbarComponent()
}
@Preview("MaterialContinousSliderComponent")
@Composable
fun MaterialContinousSliderComponentPreview() {
MaterialContinousSliderComponent()
}
@Preview("MaterialDiscreteSliderComponent")
@Composable
fun MaterialDiscreteSliderComponentPreview() {
MaterialDiscreteSliderComponent()
}
@Preview("MaterialSwitchComponent")
@Composable
fun MaterialSwitchComponentPreview() {
MaterialSwitchComponent()
}
@Preview("MaterialRippleComponent")
@Composable
fun MaterialRippleComponentPreview() {
MaterialRippleComponent()
}