Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add clipBehavior and apply borderRadius to DataTable's Material #113205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/flutter/lib/src/material/data_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ class DataTable extends StatelessWidget {
required this.rows,
this.checkboxHorizontalMargin,
this.border,
this.clipBehavior = Clip.none,
}) : assert(columns != null),
assert(columns.isNotEmpty),
assert(sortColumnIndex == null || (sortColumnIndex >= 0 && sortColumnIndex < columns.length)),
Expand All @@ -414,6 +415,7 @@ class DataTable extends StatelessWidget {
assert(rows != null),
assert(!rows.any((DataRow row) => row.cells.length != columns.length)),
assert(dividerThickness == null || dividerThickness >= 0),
assert(clipBehavior != null),
_onlyTextColumn = _initOnlyTextColumn(columns);

/// The configuration and labels for the columns in the table.
Expand Down Expand Up @@ -638,6 +640,13 @@ class DataTable extends StatelessWidget {
/// The style to use when painting the boundary and interior divisions of the table.
final TableBorder? border;

/// {@macro flutter.material.Material.clipBehavior}
///
/// This can be used to clip the content within the border of the [DataTable].
///
/// Defaults to [Clip.none], and must not be null.
final Clip clipBehavior;

// Set by the constructor to the index of the only Column that is
// non-numeric, if there is exactly one, otherwise null.
final int? _onlyTextColumn;
Expand Down Expand Up @@ -1056,6 +1065,8 @@ class DataTable extends StatelessWidget {
decoration: decoration ?? dataTableTheme.decoration ?? theme.dataTableTheme.decoration,
child: Material(
type: MaterialType.transparency,
borderRadius: border?.borderRadius,
clipBehavior: clipBehavior,
child: Table(
columnWidths: tableColumns.asMap(),
children: tableRows,
Expand Down
2 changes: 2 additions & 0 deletions packages/flutter/lib/src/rendering/table_border.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class TableBorder {
final BorderSide verticalInside;

/// The [BorderRadius] to use when painting the corners of this border.
///
/// It is also applied to [DataTable]'s [Material].
final BorderRadius borderRadius;

/// The widths of the sides of this border represented as an [EdgeInsets].
Expand Down
49 changes: 49 additions & 0 deletions packages/flutter/test/material/data_table_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1895,4 +1895,53 @@ void main() {
// Go without crashes.

});

testWidgets('DataTable clip behavior', (WidgetTester tester) async {
const Color selectedColor = Colors.green;
const Color defaultColor = Colors.red;
const BorderRadius borderRadius = BorderRadius.all(Radius.circular(30));

Widget buildTable({bool selected = false, required Clip clipBehavior}) {
return Material(
child: DataTable(
clipBehavior: clipBehavior,
border: TableBorder.all(borderRadius: borderRadius),
columns: const <DataColumn>[
DataColumn(
label: Text('Column1'),
),
],
rows: <DataRow>[
DataRow(
selected: selected,
color: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return selectedColor;
}
return defaultColor;
},
),
cells: const <DataCell>[
DataCell(Text('Content1')),
],
),
],
),
);
}

// Test default clip behavior.
await tester.pumpWidget(MaterialApp(home: buildTable(clipBehavior: Clip.none)));

Material material = tester.widget<Material>(find.byType(Material).last);
expect(material.clipBehavior, Clip.none);
expect(material.borderRadius, borderRadius);

await tester.pumpWidget(MaterialApp(home: buildTable(clipBehavior: Clip.hardEdge)));

material = tester.widget<Material>(find.byType(Material).last);
expect(material.clipBehavior, Clip.hardEdge);
expect(material.borderRadius, borderRadius);
});
}