Search code examples
flutterdartflutter-widget

How to use Expanded with InkWell in order to solve A Renderflex Overflowed By Pixels


The project crashes in line "child: Column" when I click on an item.

Widget renderEditableParamItem(EditableStructParam data) {
    return InkWell(
        onTap: () => _bloc.onEditableItemSelected(data),
        child: Container(
          margin: EdgeInsets.only(left: 15, right: 15),
          padding: EdgeInsets.all(10),
          child: Column(
            children: <Widget>[
              Text(
                data.title,
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 16,
                ),
              ),
            ],
          ),
        ));
  }

ERROR:

The overflowing RenderFlex has an orientation of Axis.vertical.
I/flutter (23777): The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and
I/flutter (23777): black striped pattern. This is usually caused by the contents being too big for the RenderFlex.
I/flutter (23777): Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the
I/flutter (23777): RenderFlex to fit within the available space instead of being sized to their natural size.
I/flutter (23777): This is considered an error condition because it indicates that there is content that cannot be
I/flutter (23777): seen. If the content is legitimately bigger than the available space, consider clipping it with a
I/flutter (23777): ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex,
I/flutter (23777): like a ListView.

Do you know how to use Expanded here?

I tried to return Expanded with InkWell as a child but it is still crashing.


Solution

  • Expanded should use where the child size is unbounded. In your case your Text widget increase the size of it with the bigger String. So you should wrap your Text widget with Expanded. For more information please go through this.

    Widget renderEditableParamItem(EditableStructParam data) {
        return InkWell(
            onTap: () => _bloc.onEditableItemSelected(data),
            child: Container(
              margin: EdgeInsets.only(left: 15, right: 15),
              padding: EdgeInsets.all(10),
              child: Column(
                children: <Widget>[
                  Expanded(
                    child: Text(
                     data.title,
                     style: TextStyle(
                       color: Colors.white,
                       fontSize: 16,
                     ),
                   ),
                  )
                ],
              ),
            ));
      }