Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- struct Item: Identifiable, Hashable {
- let id = UUID()
- let name: String
- }
- struct ItemsView: View {
- @State private var items: [Item] = [
- Item(name: "Item 1"),
- Item(name: "Item 2"),
- Item(name: "Item 3")
- ]
- var body: some View {
- NavigationStack {
- ZStack {
- Color.background
- .ignoresSafeArea()
- List {
- ForEach(items, id: \.id) { item in
- Section {
- // Content wrapper without navigation link
- VStack(alignment: .leading, spacing: 16) {
- Text(item.name)
- .font(.headline)
- HStack(spacing: 16) {
- // Delete Button
- Button(role: .destructive) {
- deleteItem(item)
- } label: {
- Text("Delete")
- .frame(maxWidth: .infinity)
- .padding()
- .background(Color.red.opacity(0.2))
- .cornerRadius(8)
- }
- .buttonStyle(.borderedProminent)
- .controlSize(.regular)
- // Share Button
- NavigationLink {
- ShareDetailView(item: item)
- } label: {
- Text("Share")
- .frame(maxWidth: .infinity)
- .padding()
- .background(Color.blue.opacity(0.2))
- .cornerRadius(8)
- }
- .buttonStyle(.plain)
- }
- }
- .padding()
- .background(Color.secondary.opacity(0.1))
- .cornerRadius(10)
- .overlay(
- RoundedRectangle(cornerRadius: 10)
- .stroke(Color.gray.opacity(0.5), lineWidth: 0.5)
- )
- }
- .listRowBackground(Color.clear)
- .listRowInsets(EdgeInsets(top: 8, leading: 8, bottom: 8, trailing: 8))
- }
- }
- .scrollContentBackground(.hidden)
- .scrollIndicators(.hidden)
- }
- .navigationTitle("Items")
- .navigationBarTitleDisplayMode(.inline)
- }
- }
- private func deleteItem(_ item: Item) {
- withAnimation {
- items.removeAll { $0.id == item.id }
- }
- }
- }
- struct ShareDetailView: View {
- let item: Item
- var body: some View {
- VStack(spacing: 16) {
- Text("Sharing: \(item.name)")
- .font(.title)
- .bold()
- Text("Details about \(item.name)...")
- .font(.body)
- Spacer()
- }
- .padding()
- .navigationTitle("Share Item")
- }
- }
- #Preview {
- ItemsView()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement