Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi row layout, how to set the gap combination percentage #265

Open
yznote opened this issue Nov 15, 2024 · 2 comments
Open

multi row layout, how to set the gap combination percentage #265

yznote opened this issue Nov 15, 2024 · 2 comments

Comments

@yznote
Copy link

yznote commented Nov 15, 2024

If it is a multi row layout, how to set the gap combination percentage
Expectation: Two columns per row, for a total of two rows
Actual: Only one column

rootView.flex.direction(.row).wrap(.wrap).shrink(1).paddingHorizontal(15).gap(0).define { (flex) in
    flex.addItem().width(50%).height(25).backgroundColor(.red).shrink(1)
    flex.addItem().width(50%).height(25).backgroundColor(.green).shrink(1)
    flex.addItem().width(50%).height(25).backgroundColor(.yellow).shrink(1)
    flex.addItem().width(50%).height(25).backgroundColor(.blue).shrink(1)
}
addSubview(rootView)
rootView.backgroundColor = .gray

WX20241115-113415

rootView.flex.direction(.row).wrap(.wrap).shrink(1).paddingHorizontal(15).gap(5).define { (flex) in
  flex.addItem().width(50%).height(25).backgroundColor(.red).shrink(1)
  flex.addItem().width(50%).height(25).backgroundColor(.green).shrink(1)
  flex.addItem().width(50%).height(25).backgroundColor(.yellow).shrink(1)
  flex.addItem().width(50%).height(25).backgroundColor(.blue).shrink(1)
}
addSubview(rootView)
rootView.backgroundColor = .gray

WX20241115-113401

Originally posted by @yznote in #262 (comment)

@yznote
Copy link
Author

yznote commented Nov 20, 2024

@lucdion @OhKanghoon Can you provide a good solution? Thank you

That is to say, CSS supports calc(), such as width: calc (50% -5px) May I ask if there are any related APIs in Flexlayout

@yznote
Copy link
Author

yznote commented Nov 21, 2024

@lucdion @OhKanghoon
box ===> width:384
item【red、green、yellow、blue】 ===> width:(384-15*2-5)/2 ==174.5
item【systemOrange】===> width:177【174.5+2.5】
WX20241121-171802
【red、green、yellow、blue】used for layout implementation
【systemOrange】used for comparison

complete code

rootView.flex.direction(.row).wrap(.wrap)
    .justifyContent(.start).alignItems(.center).paddingHorizontal(15)
    .rowGap(5).columnGap(5).grow(1).shrink(1).define { (flex) in
        // 实际布局的四个item
        flex.addItem().width(46%).height(25).backgroundColor(.red).grow(1)
        flex.addItem().width(46%).height(25).backgroundColor(.green).grow(1)
        flex.addItem().width(46%).height(25).backgroundColor(.yellow).grow(1)
        flex.addItem().width(46%).height(25).backgroundColor(.blue).grow(1)
        // 用于对比
        flex.addItem().width(50%).height(25).backgroundColor(.systemOrange)
    }
addSubview(rootView)
rootView.backgroundColor = .hex("#fff", 0.2)

The core is to set an approximate value for width and then use grow alignment.
It looks perfect, but I'm not sure if it's really used this way.

@yznote yznote closed this as completed Nov 25, 2024
@yznote yznote reopened this Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant