5

This is my DataTable:

SizedBox(
  height: 200,
  child: SingleChildScrollView(
    scrollDirection: Axis.vertical,
    child: DataTable(
      columnSpacing: 0,
      columns: const [
        DataColumn(label: Text("Domain")),
        DataColumn(label: Text("Request count")),
      ],
      rows: const [
        DataRow(cells: [
          DataCell(Text("A very long text which causes the right column to be pushed out of the screen")),
          DataCell(Text("12345")),
        ]),
        DataRow(cells: [
          DataCell(Text("It would be the best if this text would be shown in to lines so that there is enough space for the second column")),
          DataCell(Text("678890")),
        ]),
      ],
    ),
  ),
)

The exception is:

The following assertion was thrown during layout:
A RenderFlex overflowed by 39 pixels on the right.

The relevant error-causing widget was
DataTable

I want the table to be limited in height, so that you can scroll through it, but horizontally the width should be fixed, so that you cannot scroll. If the content does not fit into the columns, as the above error shows, then the text should not disappear from the screen, but instead the text should go into a second row so that there is enough space for both columns on the screen. I have already tried many things, but I simply cannot limit the width of the table so that it is only as wide as the parent widget.

4
  • words be cut off, or go to next line, I am confused with this part Commented Jan 23, 2022 at 15:28
  • At the moment, the second column is simply cut off on the right-hand side. Instead, the text from one or both columns should go over into a second line so that both columns fit next to each other. Commented Jan 23, 2022 at 15:30
  • Can you provide simple code-snippet while it is containing extra code that reference is missing like snapshot? Commented Jan 23, 2022 at 15:32
  • I have replaced the code by a simpler code-snippet. Commented Jan 23, 2022 at 15:37

2 Answers 2

5

Providing width for every DataCell child solve the issue.

DataCell(
  SizedBox(
    width: x,
    child: Text(
      "A very long text which causes the right column to be pushed out of the screen",
      overflow: TextOverflow.visible,
      softWrap: true,
    ),
  ),
),

To get responsive width, you can use LayoutBuilder on body and use it's width: constraints.maxWidth * .5, to get 50% width of the screen or MediaQuery, (make sure minimizing the padding).

Sign up to request clarification or add additional context in comments.

3 Comments

I am now using the LayoutBuilder wrapped around the DataTable and tried width: constraints.maxWidth * .8 and width: constraints.maxWidth * .2. I don't know why it doesn't fully work out, I have to use (constraints.maxWidth - 50) * factor, otherwise the right column is still cut off. Do you have an idea why that is? I have wrapped the LayoutBuilder directly around the DataTable and have set columnSpacing: 0 and I am not using any paddings here.
Could be issue decoration, it is having padding or something that is taking some space like you solved with minimizing 50px
I just found out that I also have to set horizontalMargin: 0, then I don't need to take away 50px.
3

Wrapping DataTable widget with FittedBox solved all!

2 Comments

Your answer could be improved by providing an example of the solution and how it helps the OP.
DataCell(FittedBox( child: Row( children: [ logLabel("User ID: ", data[index].userId), logLabel(", User IP: ", data[index].ip), logLabel(", Browser: ", data[index].browser), logLabel(", Operating System: ", data[index].os) ], ), )),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.