Home > Enterprise >  Flutter BoxConstraints maxWidth in CustomScrollView not working
Flutter BoxConstraints maxWidth in CustomScrollView not working

Time:01-28

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!

CodePudding user response:

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,
            ),
          ],
        ),
      ),
    ),
  ),
)
  • Related