@@ -11,10 +11,7 @@ import androidx.compose.foundation.text.KeyboardOptions
11
11
import androidx.compose.material.icons.Icons
12
12
import androidx.compose.material.icons.filled.Close
13
13
import androidx.compose.material3.*
14
- import androidx.compose.runtime.Composable
15
- import androidx.compose.runtime.LaunchedEffect
16
- import androidx.compose.runtime.mutableStateOf
17
- import androidx.compose.runtime.remember
14
+ import androidx.compose.runtime.*
18
15
import androidx.compose.ui.Alignment
19
16
import androidx.compose.ui.Modifier
20
17
import androidx.compose.ui.graphics.Color
@@ -48,38 +45,38 @@ fun ShoppingList(viewModel: ShoppingListViewModel) {
48
45
}
49
46
50
47
@Composable
51
- fun Input (onCreateItem : (ShoppingListItem ) -> Unit ) = Column {
52
- Row (
53
- modifier = Modifier
54
- .fillMaxWidth()
55
- .padding(8 .dp),
56
- verticalAlignment = Alignment .CenterVertically
57
- ) {
58
- val input = remember { mutableStateOf(TextFieldValue ()) }
48
+ fun Input (onCreateItem : (ShoppingListItem ) -> Unit ) {
49
+ Column {
50
+ Row (
51
+ modifier = Modifier .fillMaxWidth().padding(8 .dp),
52
+ verticalAlignment = Alignment .CenterVertically
53
+ ) {
54
+ var input by remember { mutableStateOf(TextFieldValue ()) }
59
55
60
- fun createShoppingItem () {
61
- val text = input.value. text
62
- if (text.isBlank()) return
63
- onCreateItem(ShoppingListItem (text.replace(" !" , " " ), text.count { it == ' !' }))
64
- input.value = input.value .copy(text = " " )
65
- }
56
+ fun createShoppingItem () {
57
+ val text = input.text.trim()
58
+ if (text.isBlank()) return
59
+ onCreateItem(ShoppingListItem (text.replace(" !" , " " ), text.count { it == ' !' }))
60
+ input = input.copy(text = " " )
61
+ }
66
62
67
- OutlinedTextField (
68
- value = input.value,
69
- singleLine = true ,
70
- onValueChange = { input.value = it },
71
- keyboardOptions = KeyboardOptions (imeAction = ImeAction .Done ),
72
- keyboardActions = KeyboardActions (onDone = { createShoppingItem() }),
73
- modifier = Modifier .weight(1f ).padding(8 .dp),
74
- )
75
- OutlinedButton (
76
- onClick = { createShoppingItem() },
77
- modifier = Modifier .padding(8 .dp)
78
- ) {
79
- Text (" Create" )
63
+ OutlinedTextField (
64
+ value = input,
65
+ singleLine = true ,
66
+ onValueChange = { input = it },
67
+ keyboardOptions = KeyboardOptions (imeAction = ImeAction .Done ),
68
+ keyboardActions = KeyboardActions (onDone = { createShoppingItem() }),
69
+ modifier = Modifier .weight(1f ).padding(8 .dp),
70
+ )
71
+ OutlinedButton (
72
+ onClick = { createShoppingItem() },
73
+ modifier = Modifier .padding(8 .dp)
74
+ ) {
75
+ Text (" Create" )
76
+ }
80
77
}
78
+ Divider ()
81
79
}
82
- Divider ()
83
80
}
84
81
85
82
@Composable
0 commit comments