Exemple #1
0
        public override Widget build(BuildContext context)
        {
            D.assert(!_debugInteractive || material_.debugCheckHasMaterial(context));

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

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

            List <TableColumnWidth> tableColumns =
                new List <TableColumnWidth>(new TableColumnWidth[columns.Count + (displayCheckboxColumn ? 1 : 0)]);

            List <TableRow> tableRows = LinqUtils <TableRow, int> .SelectList(Enumerable.Range(0, rows.Count + 1), (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>(new Widget[tableColumns.Count])
                           ));
            });

            int rowIndex;

            int displayColumnIndex = 0;

            if (displayCheckboxColumn)
            {
                tableColumns[0]          = new FixedColumnWidth(horizontalMargin + Checkbox.width + horizontalMargin / 2.0f);
                tableRows[0].children[0] = _buildCheckbox(
                    color: theme.accentColor,
                    isChecked: allChecked,
                    onCheckboxChanged: _check => _handleSelectAll(_check ?? false)
                    );
                rowIndex = 1;
                foreach (DataRow row in rows)
                {
                    tableRows[rowIndex].children[0] = _buildCheckbox(
                        color: theme.accentColor,
                        isChecked: row.selected,
                        onRowTap: () => {
                        if (row.onSelectChanged != null)
                        {
                            row.onSelectChanged(!row.selected);
                        }
                    },
                        onCheckboxChanged: _select => row.onSelectChanged(_select ?? false)
                        );
                    rowIndex += 1;
                }

                displayColumnIndex += 1;
            }

            for (int dataColumnIndex = 0; dataColumnIndex < columns.Count; dataColumnIndex += 1)
            {
                DataColumn column = columns[dataColumnIndex];

                float paddingStart;
                if (dataColumnIndex == 0 && displayCheckboxColumn)
                {
                    paddingStart = horizontalMargin / 2.0f;
                }
                else if (dataColumnIndex == 0 && !displayCheckboxColumn)
                {
                    paddingStart = horizontalMargin;
                }
                else
                {
                    paddingStart = columnSpacing / 2.0f;
                }

                float paddingEnd;
                if (dataColumnIndex == columns.Count - 1)
                {
                    paddingEnd = horizontalMargin;
                }
                else
                {
                    paddingEnd = columnSpacing / 2.0f;
                }

                EdgeInsetsDirectional padding = EdgeInsetsDirectional.only(
                    start: paddingStart,
                    end: paddingEnd
                    );
                if (dataColumnIndex == _onlyTextColumn)
                {
                    tableColumns[displayColumnIndex] = new IntrinsicColumnWidth(flex: 1.0f);
                }
                else
                {
                    tableColumns[displayColumnIndex] = new IntrinsicColumnWidth();
                }

                var currentColumnIndex = dataColumnIndex;
                tableRows[0].children[displayColumnIndex] = _buildHeadingCell(
                    context: context,
                    padding: padding,
                    label: column.label,
                    tooltip: column.tooltip,
                    numeric: column.numeric,
                    onSort: column.onSort != null
                        ? () => column.onSort(currentColumnIndex, sortColumnIndex != currentColumnIndex || !sortAscending)
                        : (VoidCallback)null,
                    sorted: dataColumnIndex == sortColumnIndex,
                    ascending: sortAscending
                    );
                rowIndex = 1;
                foreach (DataRow row in rows)
                {
                    DataCell cell   = row.cells[dataColumnIndex];
                    var      curRow = row;
                    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: () => {
                        if (curRow.onSelectChanged != null)
                        {
                            curRow.onSelectChanged(!curRow.selected);
                        }
                    });
                    rowIndex += 1;
                }

                displayColumnIndex += 1;
            }
            return(new Table(
                       columnWidths: LinqUtils <int, TableColumnWidth> .SelectDictionary(tableColumns, ((TableColumnWidth x) => tableColumns.IndexOf(x))),
                       children: tableRows
                       ));
        }