build method

  1. @override
Widget build (BuildContext context)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes).

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

Implementation

@override
Widget build(BuildContext context) {
  assert(!_debugInteractive || debugCheckHasMaterial(context));

  final ThemeData theme = Theme.of(context);
  final BoxDecoration _kSelectedDecoration = BoxDecoration(
    border: Border(bottom: Divider.createBorderSide(context, width: 1.0)),
    // The backgroundColor has to be transparent so you can see the ink on the material
    color: (Theme.of(context).brightness == Brightness.light) ? _grey100Opacity : _grey300Opacity,
  );
  final BoxDecoration _kUnselectedDecoration = BoxDecoration(
    border: Border(bottom: Divider.createBorderSide(context, width: 1.0)),
  );

  final bool showCheckboxColumn = rows.any((DataRow row) => row.onSelectChanged != null);
  final bool allChecked = showCheckboxColumn && !rows.any((DataRow row) => row.onSelectChanged != null && !row.selected);

  final List<TableColumnWidth> tableColumns = List<TableColumnWidth>(columns.length + (showCheckboxColumn ? 1 : 0));
  final List<TableRow> tableRows = List<TableRow>.generate(
    rows.length + 1, // the +1 is for the header row
    (int index) {
      return TableRow(
        key: index == 0 ? _headingRowKey : rows[index - 1].key,
        decoration: index > 0 && rows[index - 1].selected ? _kSelectedDecoration
                                                          : _kUnselectedDecoration,
        children: List<Widget>(tableColumns.length)
      );
    },
  );

  int rowIndex;

  int displayColumnIndex = 0;
  if (showCheckboxColumn) {
    tableColumns[0] = const FixedColumnWidth(_tablePadding + Checkbox.width + _tablePadding / 2.0);
    tableRows[0].children[0] = _buildCheckbox(
      color: theme.accentColor,
      checked: allChecked,
      onCheckboxChanged: _handleSelectAll,
    );
    rowIndex = 1;
    for (DataRow row in rows) {
      tableRows[rowIndex].children[0] = _buildCheckbox(
        color: theme.accentColor,
        checked: row.selected,
        onRowTap: () => row.onSelectChanged(!row.selected),
        onCheckboxChanged: row.onSelectChanged,
      );
      rowIndex += 1;
    }
    displayColumnIndex += 1;
  }

  for (int dataColumnIndex = 0; dataColumnIndex < columns.length; dataColumnIndex += 1) {
    final DataColumn column = columns[dataColumnIndex];
    final EdgeInsetsDirectional padding = EdgeInsetsDirectional.only(
      start: dataColumnIndex == 0 ? showCheckboxColumn ? _tablePadding / 2.0 : _tablePadding : _columnSpacing / 2.0,
      end: dataColumnIndex == columns.length - 1 ? _tablePadding : _columnSpacing / 2.0,
    );
    if (dataColumnIndex == _onlyTextColumn) {
      tableColumns[displayColumnIndex] = const IntrinsicColumnWidth(flex: 1.0);
    } else {
      tableColumns[displayColumnIndex] = const IntrinsicColumnWidth();
    }
    tableRows[0].children[displayColumnIndex] = _buildHeadingCell(
      context: context,
      padding: padding,
      label: column.label,
      tooltip: column.tooltip,
      numeric: column.numeric,
      onSort: () => column.onSort(dataColumnIndex, sortColumnIndex == dataColumnIndex ? !sortAscending : true),
      sorted: dataColumnIndex == sortColumnIndex,
      ascending: sortAscending,
    );
    rowIndex = 1;
    for (DataRow row in rows) {
      final DataCell cell = row.cells[dataColumnIndex];
      tableRows[rowIndex].children[displayColumnIndex] = _buildDataCell(
        context: context,
        padding: padding,
        label: cell.child,
        numeric: column.numeric,
        placeholder: cell.placeholder,
        showEditIcon: cell.showEditIcon,
        onTap: cell.onTap,
        onSelectChanged: () => row.onSelectChanged(!row.selected),
      );
      rowIndex += 1;
    }
    displayColumnIndex += 1;
  }

  return Table(
    columnWidths: tableColumns.asMap(),
    children: tableRows,
  );
}