0

I'm trying to make a text box that changes to a random one from a list when a button is pressed. All im really trying to change I the background color, but then I decided I wanted a gradient instead. It was working fine when I only had colors in the backgrounds list, but now when I try to make a gradient var, so I don't have to put each gradient in the list, I keep getting the same error. Ive tried doing this all kinds of different ways and can't seem to get it to work anyway I try it. I feel like there has to be a way though.

    import SwiftUI




struct ContentView: View {

    lazy var drinkGradient = [LinearGradient(gradient: Gradient(colors: [Color("drinkcard1"), Color("drinkcard2")]), startPoint: .bottom, endPoint: .top)]
    lazy var truthGradient = [LinearGradient(gradient: Gradient(colors: [Color("truthcard"), Color("truthcard")]), startPoint: .bottom, endPoint: .top)]
    lazy var dareGradient = [LinearGradient(gradient: Gradient(colors: [Color("darecard"), Color("darecard")]), startPoint: .bottom, endPoint: .top)]

    @State private var backgrounds = [truthGradient[0], dareGradient[0], truthGradient[0], dareGradient[0], truthGradient[0], dareGradient[0], drinkGradient[0]]
    @State private var text = [String("Truth"), String("Dare"), String("Truth"), String("Dare"), String("Truth"), String("Dare"), String("Drink!!")]
    @State private var foregrounds = [Color.black, Color.white]
    @State private var number = [0]



    var body: some View {

        ZStack{

            //Background
            Rectangle()
                .foregroundColor(Color("background"))
                .edgesIgnoringSafeArea(.all)

            //All content
            VStack {

                //Banner
                HStack {

                    Text("Truth, Dare or Drink")
                        .font(.largeTitle)
                        .foregroundColor(.white)
                        .padding(.bottom, 30)
                        .padding(.top, 80)
                        .frame(minWidth: 0, maxWidth: .infinity)



                }
                .frame(minWidth: 0, maxWidth:  .infinity)
                .edgesIgnoringSafeArea(.all)
                .background(Color("banner"))
                .shadow(radius: 20)
                Spacer()

                Text(text[number[0]])
                .font(.title)
                .foregroundColor(foregrounds[0])
                .padding(.all, 75)
                .background(backgrounds[number[0]])
                .cornerRadius(15)
                .shadow(radius: 10)
                .padding(.all)

                Button(action: {
                    self.number[0] = Int.random(in:
                        0...self.text.count - 1)

                    if self.number[0] == 0{

                    }

                }) {
                    Text("Spin")
                        .font(.title)
                        .padding(.vertical, 15)
                        .padding(.horizontal, 80)
                        .foregroundColor(.black)
                        .background(Color("button"))
                        .cornerRadius(25)
                        .shadow(radius: 15)
                        .padding(.all)

                }
                Spacer()


            }.edgesIgnoringSafeArea(.all)



        }.edgesIgnoringSafeArea(.all)
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
4
  • Are drinkGradient, truthGradient and dareGradient essentially constants, or will they change dynamically, and you want backgrounds to change with them? Commented Mar 10, 2020 at 6:44
  • add code which we can copy and paste and see your error. The snippet you gave us is useless.... Commented Mar 10, 2020 at 6:47
  • @chris I added the rest of the code Commented Mar 10, 2020 at 6:54
  • @sweeper I have a button that randomly chooses a number between 0 and the length of the lists -1 and the text is set to number so I want the certain attributes such as text, background color (aka the gradient), and im going to have my foreground color change based on what the random number is. but the text has to line up with the correct gradient Commented Mar 10, 2020 at 7:02

1 Answer 1

2

one solution could be to do the variables in a class like so:

 class Colors {
    static var drinkGradient = [LinearGradient(gradient: Gradient(colors: [Color("drinkcard1"), Color("drinkcard2")]), startPoint: .bottom, endPoint: .top)]
    static var truthGradient = [LinearGradient(gradient: Gradient(colors: [Color("truthcard"), Color("truthcard")]), startPoint: .bottom, endPoint: .top)]
    static var dareGradient = [LinearGradient(gradient: Gradient(colors: [Color("darecard"), Color("darecard")]), startPoint: .bottom, endPoint: .top)]
 }

struct ContentView: View {

    @State private var backgrounds = [Colors.truthGradient[0], Colors.dareGradient[0], Colors.truthGradient[0], Colors.dareGradient[0], Colors.truthGradient[0], Colors.dareGradient[0], Colors.drinkGradient[0]]
    @State private var text = [String("Truth"), String("Dare"), String("Truth"), String("Dare"), String("Truth"), String("Dare"), String("Drink!!")]
    @State private var foregrounds = [Color.black, Color.white]
    @State private var number = [0]

    var body: some View {

        ZStack{

            //Background
            Rectangle()
                .foregroundColor(Color("background"))
                .edgesIgnoringSafeArea(.all)

            //All content
            VStack {

                //Banner
                HStack {

                    Text("Truth, Dare or Drink")
                        .font(.largeTitle)
                        .foregroundColor(.white)
                        .padding(.bottom, 30)
                        .padding(.top, 80)
                        .frame(minWidth: 0, maxWidth: .infinity)



                }
                .frame(minWidth: 0, maxWidth:  .infinity)
                .edgesIgnoringSafeArea(.all)
                .background(Color("banner"))
                .shadow(radius: 20)
                Spacer()

                Text(text[0])
                .font(.title)
                .foregroundColor(foregrounds[0])
                .padding(.all, 75)
                .background(backgrounds[0])
                .cornerRadius(15)
                .shadow(radius: 10)
                .padding(.all)

                Button(action: {
                    self.number[0] = Int.random(in:
                        0...self.text.count - 1)

                    if self.number[0] == 0{

                    }

                }) {
                    Text("Spin")
                        .font(.title)
                        .padding(.vertical, 15)
                        .padding(.horizontal, 80)
                        .foregroundColor(.black)
                        .background(Color("button"))
                        .cornerRadius(25)
                        .shadow(radius: 15)
                        .padding(.all)

                }
                Spacer()


            }.edgesIgnoringSafeArea(.all)



        }.edgesIgnoringSafeArea(.all)
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That fixed it all. I just started learning swift last night and am still trying to get used to it. Thank you for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.