2

I am writing a flutter app and am trying to insert a DataTable. I am having issues with the text getting truncated though and not overflowing to a new line. It will do 2 lines but not 3 on the device I am on. I tried adding the text in an Expanded widget, but that won't work and throws a 'Incorrect use of ParentDataWidget'. Any way to get really long text to span 3, 4, or 5+ lines? Below is the code.

import 'package:flutter/material.dart';

class ClaimTypeTable extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: new DataTableWidgetExtract(),
    );
  }
}

class DataTableWidgetExtract extends StatelessWidget {
  const DataTableWidgetExtract({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DataTable(
      columnSpacing: 10.0,
      horizontalMargin: 5,
      columns: <DataColumn>[
        DataColumn(label: Text('Type')),
        DataColumn(label: Text('Description',)),
      ],
      rows: claimTypesList
          .map(
            (itemRow) => DataRow(
              cells: [
                DataCell(Text(itemRow.shortName),
                    showEditIcon: false, placeholder: false, onTap: () {
                  print('We tapped it - ${itemRow.shortName}');
                }),
                DataCell(
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text(itemRow.description,),
                  ),
                  showEditIcon: false,
                  placeholder: false,
                ),
              ],
            ),
          )
          .toList(),
    );
  }
}

class ClaimType {
  String shortName;
  String description;

  ClaimType({
    this.shortName,
    this.description,
  });
}

var claimTypesList = <ClaimType>[
  ClaimType(
    shortName: 'Fire',
    description:
        'There was a fire. The fire department may or may not have been called, but shit got burnt up.',
  ),
  ClaimType(
    shortName: 'Water',
    description:
        'Water damage. Examples of typical water damage include: burst pipe, broken water heater, or faulty toilet.',
  ),
  ClaimType(
    shortName: 'Wind',
    description:
        'There is non-hurricane wind damage to your residence. If it is related to a hurricane, please select Hurricane as the claim type instead. This one is really long and gets truncated.',
  ),
  ClaimType(
      shortName: 'Crime',
      description:
          'Vandalism, theft, or other criminal behavior that resulted in a loss.'),
];

And here is the image of what it looks like: Truncated DataTable Row

2
  • 1
    If you try to use AutoSizeText (pub.dev/packages/auto_size_text) ? Commented Dec 3, 2019 at 14:11
  • Hey @RogerCuesta - thanks for the suggestion. That feels more like a hack though as I want it to span multiple lines if it can. There must be a way I am just not able to find it. Commented Dec 3, 2019 at 16:26

3 Answers 3

5

Three to four things to apply-

  1. Inside the DataCell widget you need to use a ConstrainedBox. In the ConstrainedBox specify the maxWidth and minWidth to cover the height.

  2. In Datacell widget's child if Text widget give overflow: TextOverflow.visible, softWrap: true,

  3. In DataTable widget give dataRowHeight property.

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

2 Comments

Thank you for your help. The first two do not work and the third with the height does work. However, it expands all of them. I wish there were a way to dynamically expand the height of just the one the overflows. Some of these claims are self-explanatory and have a few words (like Hurricane, for instance). However, some are quite large in explanation with what the business wants.
Yes there is a way to dynamically expand. You have to use constrained box advice from above with conditions to set height. I would suggest not to use DataTable because i tried a lot but couldn't make it responsive for me. However, since my suggestion worked. Care to mark it as answer.
3

Data tables are so hard to work with. I just ended up using a listview with dividers.

Comments

0

This is what I did in the right hand column which carries the long text. The row now shows all the text if maxRows permits. I use a media query to set the right column maxWidth to 75% of the screen width.

final dataTable = DataTable(
      dataRowMaxHeight: double.infinity,
      horizontalMargin: 6,
      columns: const [

------------

DataCell(
              ConstrainedBox(
                constraints: BoxConstraints(
                    maxWidth: rightColumnMaxWidth, maxHeight: double.infinity),
                child: Text(

Comments

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.