Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 views
in Technique[技术] by (71.8m points)

Flutter - SingleChildScrollView, Column and Expanded

I have a UI where there is SingleChildScrollView -> Column -> Expanded -> ChildOfExpanded But I can't put a SingleChildScrollView as parent of Column because of the Expanded. I've tried multiple solutions, like the one on the docs but no one worked so far.

That's the code:

Column(
          children: [
            // Other widgets (Containers, Padding, ecc.)

            Expanded(
              child: TabBarView(
                children: [
                  // Other widgets
                ],
              ),
            )
          ],
        ),

I don't have a fixed height, so I can't replace the Expanded with a SizedBox or something like that.

That's the error I'm getting:

The following assertion was thrown during performLayout(): RenderFlex children have non-zero flex but incoming height constraints are unbounded.

When a column is in a parent that does not provide a finite height constraint, for example if it is in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining space in the vertical direction. These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child cannot simultaneously expand to fit its parent.

Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible children (using Flexible rather than Expanded). This will allow the flexible children to size themselves to less than the infinite remaining space they would otherwise be forced to take, and then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum constraints provided by the parent.

If this message did not help you determine the problem, consider using debugDumpRenderTree():
https://flutter.dev/debugging/#rendering-layer
http://api.flutter.dev/flutter/rendering/debugDumpRenderTree.html The affected RenderFlex is: RenderFlex#7d8dc relayoutBoundary=up12 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE

I've tried with a Flexible as said in the error, but then I get this:

Horizontal viewport was given unbounded height. The relevant error-causing widget was TabBarView lib/…/screens/specific_launch.dart:193 ════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════ RenderBox was not laid out: RenderViewport#57f46 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE 'package:flutter/src/rendering/box.dart': Failed assertion: line 1940 pos 12: 'hasSize' The relevant error-causing widget was TabBarView lib/…/screens/specific_launch.dart:193

In this UI is fundamental to have a dynamic height, so a ScrollView cannot be avoided

question from:https://stackoverflow.com/questions/65870149/flutter-singlechildscrollview-column-and-expanded

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With the SingleChildScrollView, your screen no longer has a fixed height and is potentially infinitely long. The expanded takes as much height as it can get, but cannot be infinite. One solution would be to give your Column () a defined height. For example like this:

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        height: heightSum, //try to sum the height of the other widgets
        child: Column(
          children: [
            //other widgets
            Expanded(),
          ],
        ),
      ),
    );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...