Untitled

 avatar
unknown
swift
2 years ago
1.9 kB
6
Indexable
//
//  ContentView.swift
//  MultiTable
//
//  Created by David Vickhoff on 2023-02-15.
//

import SwiftUI

struct SecondaryButton: ViewModifier {
    
    var isPressed: Bool
    
    
    func body(content: Content) -> some View {
        return content
            .frame(height:56)
            .fontWeight(.bold)
            .background(isPressed ? .blue : .white)
            .foregroundColor(isPressed ? .white : .blue)
            .cornerRadius(8)
            .overlay(
                    RoundedRectangle(cornerRadius: 8)
                        .strokeBorder(.blue, lineWidth: 3)
                    )
    }
    
}
extension View {
    func secondaryButton(isPressed: Bool) -> some View {
        self.modifier(SecondaryButton(isPressed: isPressed))
    }
}

struct ContentView: View {
    
    @State var isPressed = false
    @State var chosenTable: Int?
    
    let columns = [
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible())
    ]
    
    var body: some View {
        ScrollView {

        VStack {
                
            LazyVGrid(columns: columns) {
                
                ForEach(0..<12) { index in
                    Button {
                        withAnimation(.easeInOut(duration: 0.1)) {
                            chosenTable = index
                            
                        }
                    } label: {
                        Spacer()
                        Text("\(index + 1)")
                        Spacer()
                    }
                    .secondaryButton(isPressed: index == chosenTable)
                    
                }
                
            }
            
            }
            .padding(24)
            
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Editor is loading...