简体   繁体   中英

Flutter BoxConstraints maxWidth in CustomScrollView not working

I am using a CustomScrollView and tried to limit its children's size by using BoxConstraints but they are simply ignored. My text expands to the width of the screen and I don't understand why.

This is my code:

  SliverToBoxAdapter(
    child: ConstrainedBox(
      constraints: maxWidthConstraint,
      child: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              widget.project.description,
            ),
          ],
        ),
      ),
    ),
  ),

What am I missing here? Let me know if you need any more info!

The parent forces it to be the full width so you can wrap it with UnconstrainedBox or Align or Center widget to get rid of that behavior.

SliverToBoxAdapter(
  child: UnconstrainedBox( // this should allow it to have its own constraints
    child: ConstrainedBox(
      constraints: maxWidthConstraint,
      child: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              widget.project.description,
            ),
          ],
        ),
      ),
    ),
  ),
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM