Created
February 7, 2020 14:50
-
-
Save BMonsalvatge/c009d1ec8dbe99092fb8f88564626172 to your computer and use it in GitHub Desktop.
Body Of Our List Add Item View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var body: some View { | |
ZStack { | |
VStack { | |
NavigationView { | |
List { | |
ForEach(listItems, id: \.self) { item in | |
Text(item.name).listRowBackground(Color(item.color)) | |
} | |
} | |
.navigationBarTitle(Text("List Items")) | |
// Adds the plus button to our navbar | |
.navigationBarItems(trailing: | |
Button(action: { | |
// toggles the value of our bool from false to true, | |
// which will present our sheet. | |
self.addItem.toggle() | |
}, label: { | |
Image(systemName: "plus") | |
}) | |
) | |
// This is our secondary view that'll take in our user's input. | |
// Is presented when addItem is set to true. | |
}.sheet(isPresented: $addItem) { | |
VStack { | |
HStack { | |
Text("Item Name: ") | |
TextField("Add an Item", text: self.$addItemName) | |
} | |
// A simple picker that gives our user some different selections of color. | |
Picker(selection: self.$colorSelection, label: Text("Select a Color")) { | |
Text("Red").tag(UIColor.systemRed) | |
Text("Blue").tag(UIColor.systemBlue) | |
Text("Green").tag(UIColor.systemGreen) | |
Text("Yellow").tag(UIColor.systemYellow) | |
} | |
// Button that will submit our data to the list and reset our user selected | |
// variables for when then add another item. | |
Button(action: { | |
self.listItems.append(Item(name: self.addItemName, color: self.colorSelection)) | |
// This will close our sheet view when the user click our Add button. | |
self.addItem.toggle() | |
// Reset Values | |
self.addItemName = "" | |
self.colorSelection = UIColor.systemRed | |
}, label: { | |
Text("Add") | |
}) | |
}.padding(100) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment