- 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 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:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
context
usingBuildContext.inheritFromWidgetOfExactType
.
If a widget's build
method is to depend on anything else, use a
StatefulWidget
instead.
Source
@override Widget build(BuildContext context) { assert(!_debugInteractive || debugCheckHasMaterial(context)); final ThemeData theme = Theme.of(context); final BoxDecoration _kSelectedDecoration = new BoxDecoration( border: new Border(bottom: new BorderSide(color: theme.dividerColor)), // The backgroundColor has to be transparent so you can see the ink on the material backgroundColor: (Theme.of(context).brightness == Brightness.light) ? _kGrey100Opacity : _kGrey300Opacity ); final BoxDecoration _kUnselectedDecoration = new BoxDecoration( border: new Border(bottom: new BorderSide(color: theme.dividerColor)) ); final bool showCheckboxColumn = rows.any((DataRow row) => row.onSelectChanged != null); final bool allChecked = showCheckboxColumn && !rows.any((DataRow row) => row.onSelectChanged != null && !row.selected); List<TableColumnWidth> tableColumns = new List<TableColumnWidth>(columns.length + (showCheckboxColumn ? 1 : 0)); List<TableRow> tableRows = new List<TableRow>.generate( rows.length + 1, // the +1 is for the header row (int index) { return new TableRow( key: index == 0 ? _headingRowKey : rows[index - 1].key, decoration: index > 0 && rows[index - 1].selected ? _kSelectedDecoration : _kUnselectedDecoration, children: new List<Widget>(tableColumns.length) ); } ); int rowIndex; int displayColumnIndex = 0; if (showCheckboxColumn) { tableColumns[0] = new FixedColumnWidth(_kTablePadding + Checkbox.width + _kTablePadding / 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) { DataColumn column = columns[dataColumnIndex]; final EdgeInsets padding = new EdgeInsets.fromLTRB( dataColumnIndex == 0 ? showCheckboxColumn ? _kTablePadding / 2.0 : _kTablePadding : _kColumnSpacing / 2.0, 0.0, dataColumnIndex == columns.length - 1 ? _kTablePadding : _kColumnSpacing / 2.0, 0.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) { DataCell cell = row.cells[dataColumnIndex]; tableRows[rowIndex].children[displayColumnIndex] = _buildDataCell( context: context, padding: padding, label: cell.widget, numeric: column.numeric, placeholder: cell.placeholder, showEditIcon: cell.showEditIcon, onTap: cell.onTap, onSelectChanged: () => row.onSelectChanged(!row.selected) ); rowIndex += 1; } displayColumnIndex += 1; } return new Table( columnWidths: tableColumns.asMap(), children: tableRows ); }