Advertisement
marcusziade

Untitled

Jan 27th, 2025
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.69 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct Item: Identifiable, Hashable {
  4.     let id = UUID()
  5.     let name: String
  6. }
  7.  
  8. struct ItemsView: View {
  9.     @State private var items: [Item] = [
  10.         Item(name: "Item 1"),
  11.         Item(name: "Item 2"),
  12.         Item(name: "Item 3")
  13.     ]
  14.    
  15.     var body: some View {
  16.         NavigationStack {
  17.             ZStack {
  18.                 Color.background
  19.                     .ignoresSafeArea()
  20.                
  21.                 List {
  22.                     ForEach(items, id: \.id) { item in
  23.                         Section {
  24.                             // Content wrapper without navigation link
  25.                             VStack(alignment: .leading, spacing: 16) {
  26.                                 Text(item.name)
  27.                                     .font(.headline)
  28.                                
  29.                                 HStack(spacing: 16) {
  30.                                     // Delete Button
  31.                                     Button(role: .destructive) {
  32.                                         deleteItem(item)
  33.                                     } label: {
  34.                                         Text("Delete")
  35.                                             .frame(maxWidth: .infinity)
  36.                                             .padding()
  37.                                             .background(Color.red.opacity(0.2))
  38.                                             .cornerRadius(8)
  39.                                     }
  40.                                     .buttonStyle(.borderedProminent)
  41.                                     .controlSize(.regular)
  42.                                    
  43.                                     // Share Button
  44.                                     NavigationLink {
  45.                                         ShareDetailView(item: item)
  46.                                     } label: {
  47.                                         Text("Share")
  48.                                             .frame(maxWidth: .infinity)
  49.                                             .padding()
  50.                                             .background(Color.blue.opacity(0.2))
  51.                                             .cornerRadius(8)
  52.                                     }
  53.                                     .buttonStyle(.plain)
  54.                                 }
  55.                             }
  56.                             .padding()
  57.                             .background(Color.secondary.opacity(0.1))
  58.                             .cornerRadius(10)
  59.                             .overlay(
  60.                                 RoundedRectangle(cornerRadius: 10)
  61.                                     .stroke(Color.gray.opacity(0.5), lineWidth: 0.5)
  62.                             )
  63.                         }
  64.                         .listRowBackground(Color.clear)
  65.                         .listRowInsets(EdgeInsets(top: 8, leading: 8, bottom: 8, trailing: 8))
  66.                     }
  67.                 }
  68.                 .scrollContentBackground(.hidden)
  69.                 .scrollIndicators(.hidden)
  70.             }
  71.             .navigationTitle("Items")
  72.             .navigationBarTitleDisplayMode(.inline)
  73.         }
  74.     }
  75.    
  76.     private func deleteItem(_ item: Item) {
  77.         withAnimation {
  78.             items.removeAll { $0.id == item.id }
  79.         }
  80.     }
  81. }
  82.  
  83. struct ShareDetailView: View {
  84.     let item: Item
  85.    
  86.     var body: some View {
  87.         VStack(spacing: 16) {
  88.             Text("Sharing: \(item.name)")
  89.                 .font(.title)
  90.                 .bold()
  91.             Text("Details about \(item.name)...")
  92.                 .font(.body)
  93.             Spacer()
  94.         }
  95.         .padding()
  96.         .navigationTitle("Share Item")
  97.     }
  98. }
  99.  
  100. #Preview {
  101.     ItemsView()
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement