How to wrap "AspectRatio" widget to avoid yellow/black overflow line?

Viewed 52

This is my code(The complete project code : https://github.com/mitchkoko/responsivedesign):

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class MyDesktopBody extends StatelessWidget {
  const MyDesktopBody({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.deepPurple[200],
      appBar: AppBar(
        title: Text('D E S K T O P'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Row(
          children: [
            // First column
            Expanded(
              child: Column(
                children: [
                  // videoplayer section
                  AspectRatio(
                    aspectRatio: 16/9,
                    child: Container(
                      color: Colors.deepPurple[400],
                    ),
                  ),
                    
                  // comment section
                  Expanded(
                    child: ListView.builder(
                      itemCount: 8,
                      itemBuilder: (context, index) {
                        return Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Container(
                            color: Colors.deepPurple[300],
                            height: 120,
                          ),
                        );
                      },
                    ),
                  )
                ],
              ),
            ),
        
            // second column
            Expanded(
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Container(
                  width: 200,
                  color: Colors.deepPurple[300],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

When I extend the window size or decrease the window height I get the following error message:

════════ Exception caught by rendering library ═════════════════════════════════ The following assertion was thrown during layout: A RenderFlex overflowed by 5.1 pixels on the bottom.

The relevant error-causing widget was Column lib\responsive\desktop_body.dart:20 You can inspect this widget using the 'Inspect Widget' button in the VS Code notification. The overflowing RenderFlex has an orientation of Axis.vertical. The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and black striped pattern. This is usually caused by the contents being too big for the RenderFlex.

Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the RenderFlex to fit within the available space instead of being sized to their natural size. This is considered an error condition because it indicates that there is content that cannot be seen. If the content is legitimately bigger than the available space, consider clipping it with a ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex, like a ListView.

The specific RenderFlex in question is: RenderFlex#cd135 relayoutBoundary=up7 OVERFLOWING ════════════════════════════════════════════════════════════════════════════════

I think the problem is the AspectRatio widget in videoplayer section. I don't know how to wrap this widget to prevent this error happening but when I comment out that part of the code, I will no longer get the error.

1 Answers

Yes the issue rise from AspectRatio on youtube player section when it tries to fill height based on width on small height with large width. You can wrap with ConstrainedBox to solve this issue.

body: LayoutBuilder(
  builder: (context, constraints) => Padding(
    padding: const EdgeInsets.all(8.0),
    child: Row(
      children: [
        // First column
        Expanded(
          child: Column(
            children: [
              // videoplayer section
              ConstrainedBox(
                constraints: BoxConstraints(
                  maxHeight: constraints.maxHeight - 16,
                ),
                child: AspectRatio(
                  aspectRatio: 16 / 9,

Full widget

class MyDesktopBody extends StatelessWidget {
  const MyDesktopBody({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.deepPurple[200],
      appBar: AppBar(
        title: Text('D E S K T O P'),
      ),
      body: LayoutBuilder(
        builder: (context, constraints) => Padding(
          padding: const EdgeInsets.all(8.0),
          child: Row(
            children: [
              // First column
              Expanded(
                child: Column(
                  children: [
                    // videoplayer section
                    ConstrainedBox(
                      constraints: BoxConstraints(
                        maxHeight: constraints.maxHeight - 16,
                      ),
                      child: AspectRatio(
                        aspectRatio: 16 / 9,
                        child: Container(
                          color: Colors.deepPurple[400],
                        ),
                      ),
                    ),

                    // comment section
                    Expanded(
                      child: ListView.builder(
                        itemCount: 8,
                        itemBuilder: (context, index) {
                          return Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Container(
                              color: Colors.deepPurple[300],
                              height: 120,
                            ),
                          );
                        },
                      ),
                    )
                  ],
                ),
              ),

              // second column
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Container(
                    width: 200,
                    color: Colors.deepPurple[300],
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
Related