0

I have a game view that has a board and I have several parts in a VStack and the final 2 HStack are some 'buttons' and then a row of tiles for the game.

        VStack(spacing: 5) {  
        HStack {}
        HStack {}
        ......
        ......
        HStack {

                ResetButton {
                    self.returnLetters()
                }
                NewLine {
                    self.newLine()
                }
                CalcButton {
                    self.calcButton()
                }
                StartButton {
                                   self.startButton()
                               }
                }

        HStack {
            ForEach(0..<7) { number in
                Letter(text: self.tray[number], index: number, onChanged: self.letterMoved, onEnded: self.letterDropped)
                            }
                }

This sets the screen all very well However I would ideally not want to show the Start and Calc buttons until later in the game, or indeed replace the Reset and NewLine buttons with Start and Calc.

As it looks As it Looks Now

Ideally What I would have until later in game Required look

Changing to this on the last go Endgamne screen

Is there a way to not show items in the Stack or add items to the stack later please?

thanks

Lynxbci
  • 71
  • 7

2 Answers2

0

Here is a pattern, introduce corresponding @State variable with desired initial state value. The state can be changed later at any time somewhere in view and view will be updated automatically showing conditionally dependent button:

  @State private var newLineVisible = false // initially invisible
  ...
  var body: some View {
        ...
        HStack {

                ResetButton {
                    self.returnLetters()
                }
                if self.newLineVisible { // << include conditionally
                   NewLine {
                       self.newLine()
                   }
                }
                CalcButton {
                    self.calcButton()
                }
Asperi
  • 228,894
  • 20
  • 464
  • 690
0

Use @State boolean variable that define if the buttons should be shown or not, when you change their value the view will be refreshed.

@State var isResetVisible = true
...
if isResetVisible {
    ResetButton {
        self.returnLetters()
    }
}

then you set it to false somewhere and it will hide

Watermamal
  • 357
  • 3
  • 12