06 - Altri componenti

Picker
...

Un componente grafico che funge da "selezione" di elementi.

Questo componente prevede che venga dichiarate una variabile @State.

@State private varcolorScheme = 1
Picker("Color Scheme", selectoin: $colorScheme) {
	Text("Light").tag(0)
	Text("Dark").tag(1)
}

@State private var nextFullMoonDate = Date()
//...
DatePicker("", selection: $nextFullMoonDate,
			displayedComponents: .date)

Esistono modificatori per i picker:

Picker("Color Scheme", selection: $colorScheme){
	//...
}
.pickerStyle(WheelPickerStyle())
// oppure
.pickerStyle(SegmentedPickerStyle())

Come ultimo parametro abbiamo a disposizione una closure, in cui bisogna mettere un ciclo ForEach che itera su tutti gli elementi del Picker:

var years 0 Array(2000...2021)
@State private var selectedYear = 2021
Picker("Pick a year", selection: $selectedYear) {
	ForEach(years, id: \.self) {
		Text("\($0.formatted(.number.grouping(.never)))")
	}
}.pickerStyle(WheelPickerStyle())

Abbiamo bisogno di un id in quanto ogni elemento del Picker devono essere univoci (spesso si usa un hash).

Form
...

Form {
	Section(header: Text("Section Header Text")) {
		Text("You can add any view in a section header")
		Text("Notice the default foreground color is grey")
	}
}
struct ContentView: View {
    @State private var name: String = ""
    @State private var isHidden: Bool = false

    var body: some View {
        NavigationStack {
            Form {
                Section(header: Text("Profile")) {
                    Text("Name")
                        .font(.headline)
                    TextField("Enter your name", text: $name)
                        .padding(.all)
                        .background(Color(red: 200.0/255.0,
                                          green: 200.0/255.0,
                                          blue: 200.0/255.0,
                                          opacity: 0.7),
                                    cornerRadius: 8.0)
                    Toggle(isOn: $isHidden) {
                        Text("Hide account")
                    }
                }
            }
            .padding(.horizontal, 16)
        }
        .navigationBarTitle("Settings")
    }
}

Liste
...

Un insieme di elementi scorrevoli, anche diversi tra loro, ma che devono restituire una vista.

struct List_MoveRow: View {
    @State private var data = ["Hit the Edit button to reorder", "Practice Coding",
                                "Grocery shopping", "Get tickets", "Clean house",
                                "Do laundry", "Cook dinner", "Paint room"]
    
    var body: some View {
        NavigationStack {
            List {
                ForEach(data, id: \.self) { datum in
                    Text(datum).font(.system(size: 24)).padding()
                }
                .onMove(perform: moveRow)
            }
            .navigationBarTitle(Text("To Do"))
            .navigationBarItems(trailing: EditButton())
            .accentColor(.green)
        }
    }
    
    func moveRow(from indexes: IndexSet, to destination: Int) {
        if let first = indexes.first {
            data.insert(data.remove(at: first), at: destination)
        }
    }
}

struct Restaurant: Identifiable {
    let id = UUID()
    let name: String
}

struct RestaurantRow: View {
    var restaurant: Restaurant
    
    var body: some View {
        Text("Come and eat at \(restaurant.name)")
    }
}

struct ContentView: View {
    let restaurants = [
        Restaurant(name: "Sushiamo"),
        Restaurant(name: "Ma-Kebap"),
        Restaurant(name: "Lino's Pizza")
    ]
    
    var body: some View {
        List(restaurants) { restaurant in
            RestaurantRow(restaurant: restaurant)
        }
    }
}

Il pulsante EditButton() è una funzione incorporata che restituisce una vista (Button) che attiverà automaticamente la modalità di modifica nell’elenco. Il suo testo dice "Modifica" e quando toccato appare le maniglie di spostamento sulle righe ed il testo del pulsante
dice "Fatto".