Search code examples
flutterflutter-layout

Flutter DataTable truncates text


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


Solution

  • 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.