@@ -11,10 +11,7 @@ import androidx.compose.foundation.text.KeyboardOptions
1111import androidx.compose.material.icons.Icons
1212import androidx.compose.material.icons.filled.Close
1313import 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.*
1815import androidx.compose.ui.Alignment
1916import androidx.compose.ui.Modifier
2017import androidx.compose.ui.graphics.Color
@@ -48,38 +45,38 @@ fun ShoppingList(viewModel: ShoppingListViewModel) {
4845}
4946
5047@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 ()) }
5955
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+ }
6662
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+ }
8077 }
78+ Divider ()
8179 }
82- Divider ()
8380}
8481
8582@Composable
0 commit comments