Skip to content

Spacing always applied around conditional views, even when they're missing #17

Description

@rocca23

If a View is created based on a condition and when the condition is false, the "ghost" View is took into account when creating spacing. For example:

import SwiftUI
import WrappingHStack

struct Test: View {
    let condition: Bool

    var body: some View {
        WrappingHStack {
            view("view 1")
            if condition {
                view("view 2")
            }
            view("view 3")
            view("view 4")
            view("view 5")
            view("view 6")
        }
        .border(Color.red.opacity(0.5))
        .padding(32)
    }

    private func view(_ text: String) -> some View {
        Text(text)
            .padding(8)
            .border(Color.black)
    }
}

struct Test_Previews: PreviewProvider {
    static var previews: some View {
        Test(condition: false)
    }
}

The result is the following (note the double spacing between view 1 and view 3):
Schermata 2022-03-25 alle 15 36 59

There's a workaround in this case, that is applying spacing as trailing padding on each view and setting WrappingHStack.Spacing to .constant(0), but it would be nice to have it working naturally, especially since this way the trailing padding adds to the view width. Could it be possible to fix this? Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions