Created
November 22, 2020 09:17
-
-
Save SlappyAUS/029f26625abbf88b5f81a51cab2b706f to your computer and use it in GitHub Desktop.
WatchKit Lists #watchkit #ui #swiftui #ios #swift
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
| // | |
| // ContentView.swift | |
| // FirstWatch WatchKit Extension | |
| // | |
| // Created by Greg Eales on 22/11/20. | |
| // | |
| import SwiftUI | |
| struct Person: Identifiable, Equatable { | |
| var id = UUID() | |
| var name: String | |
| var age: Int | |
| var weight: Double | |
| var height: Int | |
| } | |
| struct ContentView: View { | |
| @State var people: [Person] = [ | |
| Person(name: "Greg", age: 41, weight: 80, height: 180), | |
| Person(name: "Carlo", age: 51, weight: 90, height: 182), | |
| Person(name: "Dave", age: 55, weight: 100, height: 190), | |
| Person(name: "Pete", age: 60, weight: 75, height: 190), | |
| Person(name: "Rasheed", age: 35, weight: 81, height: 175), | |
| Person(name: "Oko", age: 42, weight: 90, height: 170) | |
| ] | |
| @State var selectPerson: Person? | |
| var body: some View { | |
| List { | |
| ForEach(people) { person in | |
| VStack(alignment: .leading) { | |
| Button(action: { | |
| if selectPerson == person { | |
| selectPerson = nil | |
| } else { | |
| selectPerson = person | |
| } | |
| }) { | |
| Text("\(person.name)") | |
| } | |
| .padding() | |
| if selectPerson == person { | |
| VStack(alignment: .leading) { | |
| Text("Age: \(person.age)") | |
| Text("Weight: \(String(format: "%.2f", person.weight))") | |
| Text("Height: \(person.height)") | |
| } | |
| .padding() | |
| } | |
| } | |
| .listRowPlatterColor(selectPerson == person ? Color.red : Color.blue) | |
| .animation(.easeOut(duration: 0.3)) | |
| } | |
| .onMove(perform: move) | |
| .onDelete(perform: delete) | |
| } | |
| } | |
| func delete(at offset: IndexSet) { | |
| people.remove(atOffsets: offset) | |
| } | |
| func move(fromOffsets source: IndexSet, toOffset destination: Int) { | |
| people.move(fromOffsets: source, toOffset: destination) | |
| } | |
| } | |
| struct ContentView_Previews: PreviewProvider { | |
| static var previews: some View { | |
| ContentView() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment