Skip to content

Commit

Permalink
Added headingRowAlignment
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-saplin committed Dec 16, 2024
1 parent bd8c688 commit 9caa292
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 14 deletions.
1 change: 1 addition & 0 deletions example/lib/screens/data_table2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class DataTable2DemoState extends State<DataTable2Demo> {
: null,
onSort: (columnIndex, ascending) =>
_sort<String>((d) => d.name, columnIndex, ascending),
// headingRowAlignment: MainAxisAlignment.end,
),
DataColumn2(
label: const Text('Calories'),
Expand Down
36 changes: 22 additions & 14 deletions lib/src/data_table_2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class DataColumn2 extends DataColumn {
super.tooltip,
super.numeric = false,
super.onSort,
super.headingRowAlignment,
this.size = ColumnSize.M,
this.fixedWidth});

Expand Down Expand Up @@ -416,13 +417,15 @@ class DataTable2 extends DataTable {
required bool sorted,
required bool ascending,
required double effectiveHeadingRowHeight,
required WidgetStateProperty<Color?>? overlayColor}) {
required WidgetStateProperty<Color?>? overlayColor,
required MainAxisAlignment headingRowAlignment}) {
final ThemeData themeData = Theme.of(context);

var customArrows =
sortArrowBuilder != null ? sortArrowBuilder!(ascending, sorted) : null;
label = Row(
textDirection: numeric ? TextDirection.rtl : null,
mainAxisAlignment: headingRowAlignment,
children: <Widget>[
Flexible(child: label),
if (onSort != null) ...<Widget>[
Expand Down Expand Up @@ -582,6 +585,7 @@ class DataTable2 extends DataTable {
assert(debugCheckHasMaterial(context));

final theme = Theme.of(context);
final DataTableThemeData dataTableTheme = DataTableTheme.of(context);
final effectiveHeadingRowColor =
headingRowColor ?? theme.dataTableTheme.headingRowColor;
final effectiveDataRowColor =
Expand Down Expand Up @@ -824,19 +828,23 @@ class DataTable2 extends DataTable {
FixedColumnWidth(widths[dataColumnIndex]);

var h = _buildHeadingCell(
context: context,
padding: padding,
effectiveHeadingRowHeight: effectiveHeadingRowHeight,
label: column.label,
tooltip: column.tooltip,
numeric: column.numeric,
onSort: column.onSort != null
? () => column.onSort!(dataColumnIndex,
sortColumnIndex != dataColumnIndex || !sortAscending)
: null,
sorted: dataColumnIndex == sortColumnIndex,
ascending: sortAscending,
overlayColor: effectiveHeadingRowColor);
context: context,
padding: padding,
effectiveHeadingRowHeight: effectiveHeadingRowHeight,
label: column.label,
tooltip: column.tooltip,
numeric: column.numeric,
onSort: column.onSort != null
? () => column.onSort!(dataColumnIndex,
sortColumnIndex != dataColumnIndex || !sortAscending)
: null,
sorted: dataColumnIndex == sortColumnIndex,
ascending: sortAscending,
overlayColor: effectiveHeadingRowColor,
headingRowAlignment: column.headingRowAlignment ??
dataTableTheme.headingRowAlignment ??
MainAxisAlignment.start,
);

headingRow.children[displayColumnIndex] =
h; // heading row alone is used to display table header should there be no data rows
Expand Down
55 changes: 55 additions & 0 deletions test/data_table_2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2303,4 +2303,59 @@ void main() {
expect(tableBorder?.bottom.width, null);
expect(tableBorder?.top.color, null);
});

testWidgets('DataColumn label can be centered', (WidgetTester tester) async {
const double horizontalMargin = 24.0;

Widget buildTable(
{MainAxisAlignment? headingRowAlignment, bool sortEnabled = false}) {
return MaterialApp(
home: Material(
child: DataTable2(
columns: <DataColumn>[
DataColumn(
headingRowAlignment: headingRowAlignment,
onSort:
sortEnabled ? (int columnIndex, bool ascending) {} : null,
label: const Text('Header'),
),
],
rows: const <DataRow>[
DataRow(
cells: <DataCell>[
DataCell(Text('Data')),
],
),
],
),
),
);
}

// Test mainAxisAlignment without sort arrow.
await tester.pumpWidget(buildTable());

Offset headerTopLeft = tester.getTopLeft(find.text('Header'));
expect(headerTopLeft.dx, equals(horizontalMargin));

// Test mainAxisAlignment.center without sort arrow.
await tester
.pumpWidget(buildTable(headingRowAlignment: MainAxisAlignment.center));

Offset headerCenter = tester.getCenter(find.text('Header'));
expect(headerCenter.dx, equals(400));

// Test mainAxisAlignment with sort arrow.
await tester.pumpWidget(buildTable(sortEnabled: true));

headerTopLeft = tester.getTopLeft(find.text('Header'));
expect(headerTopLeft.dx, equals(horizontalMargin));

// Test mainAxisAlignment.center with sort arrow.
await tester.pumpWidget(buildTable(
headingRowAlignment: MainAxisAlignment.center, sortEnabled: true));

headerCenter = tester.getCenter(find.text('Header'));
expect(headerCenter.dx > 390, true);
});
}

0 comments on commit 9caa292

Please sign in to comment.