[Flutter] How does Flutter solve the layout out of bounds

I. Introduction

We have all encountered such a situation: when using Flutter for layout, a red error message suddenly appeared, telling us that some components exceeded the scope of the screen. This is the topic we are going to discuss today - layout out of bounds. In this post, we'll dig into why layouts go out of bounds, and learn how to fix it using Flutter's various tools and techniques.

If you want to learn more about Flutter and master more tips and best practices, I have good news for you: we have a comprehensive Flutter column -> Flutter Developer 101 Getting Started Booklet waiting for you. There, you will get complete and systematic Flutter learning materials, including detailed code examples and in-depth concept analysis. What's more, our columns are constantly being updated and improved, and the price will gradually increase with the enrichment of content. So, join now and you will get all the content at the best price. Now, let's start learning today!

2. Understand layout crossing boundaries

1. What is layout out of bounds

Layout out of bounds, as the name suggests, means that the size of the component exceeds the scope of its parent component. In Flutter, if the size of a component exceeds the bounds of its parent component, Flutter will display a red error message telling us which component is out of bounds.

2. Common reasons for layout out of bounds

There are many common reasons for the layout out of bounds. For example, we may add too much content to the component without limiting the size of the component; or we may not use Flutter's layout components correctly, causing the size of the component to exceed the scope of the screen. .

3. How to discover and identify layout out of bounds

In Flutter, if a component goes outside the scope of its parent component, Flutter will output an error message on the console and display a red error message on the UI. We can find out which component is out of scope by reading the error message.

3. How does Flutter solve the layout out of bounds

In Flutter, there are several common methods that can help solve the problem of layout out of bounds. Three common workarounds are described below, along with simple code examples.

1. Using Flexible and Expanded components:

Flexible and Expanded components can help us flexibly control the size of the layout and prevent the layout from going out of bounds. Flexible components can be used to wrap subcomponents and automatically adjust the size of subcomponents according to the size of the available space. The Expanded component can be used in layouts such as Row, Column or Flex to expand sub-components and fill the remaining space.

Code example:

Row(
  children: [
    Flexible(
      flex: 1,
      child: Container(
        color: Colors.blue,
        height: 100,
      ),
    ),
    Expanded(
      flex: 2,
      child: Container(
        color: Colors.red,
        height: 100,
      ),
    ),
  ],
)

In the example above, the Flexible component wraps the first Container and resizes it according to the available space. The Expanded component expands the second Container to fill the remaining space.

2. Using ListView and ScrollView:

ListView and ScrollView are two very useful components that can help us handle a large amount of layout content and can be scrolled. When the layout content exceeds the visible area, ListView and ScrollView will automatically provide scrolling function, so as to avoid the problem of layout out of bounds.

Code example:

ListView(
  children: [
    Container(height: 100, color: Colors.blue),
    Container(height: 100, color: Colors.red),
    Container(height: 100, color: Colors.yellow),
    // 添加更多的子组件
  ],
)

In the above example, the ListView component contains multiple subcomponents. When the total height of the subcomponents exceeds the visible area, the ListView will automatically provide the scrolling function.

3. Use MediaQuery and LayoutBuilder:

MediaQuery and LayoutBuilder are tools provided by Flutter for obtaining device screen information and layout information. We can use them to dynamically adjust the layout to avoid the problem of layout out of bounds.

Code example:

LayoutBuilder(
  builder: (BuildContext context, BoxConstraints constraints) {
    
    
    if (constraints.maxHeight > 500) {
    
    
      return Container(
        height: 500,
        color: Colors.blue,
      );
    } else {
    
    
      return Container(
        height: constraints.maxHeight,
        color: Colors.red,
      );
    }
  },
)

In the above example, LayoutBuilder builds child components according to the constraints of the parent container. If the parent container's maximum height is greater than 500px, then the child component's height will be constrained to 500px, otherwise the child component's height will be the same as the parent container's maximum height.

Four. Summary

If you are interested in Flutter and want to learn more in-depth, then I would like to recommend you a great resource: our Flutter column -> Flutter Developer 101 Getting Started Booklet . There, you will get complete and systematic Flutter learning materials, including detailed code examples and in-depth concept analysis. For example, do you know how to build a complete app using Flutter? In our column you will find out. What's more, our columns are constantly being updated and improved, and the price will gradually increase with the enrichment of content. So, join now and you will get all the content at the best price. Let's continue exploring in the world of Flutter together! If you want to know more, you can first read our one-stop solution to your needs, Flutter Developer 101 Getting Started Booklet column guide .

Guess you like

Origin blog.csdn.net/diandianxiyu/article/details/131218320