Skip to content

Commit df259c5

Browse files
authored
Add clipBehavior and apply borderRadius to DataTable's Material (#113205)
1 parent 9f5c655 commit df259c5

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

packages/flutter/lib/src/material/data_table.dart

+11
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ class DataTable extends StatelessWidget {
406406
required this.rows,
407407
this.checkboxHorizontalMargin,
408408
this.border,
409+
this.clipBehavior = Clip.none,
409410
}) : assert(columns != null),
410411
assert(columns.isNotEmpty),
411412
assert(sortColumnIndex == null || (sortColumnIndex >= 0 && sortColumnIndex < columns.length)),
@@ -414,6 +415,7 @@ class DataTable extends StatelessWidget {
414415
assert(rows != null),
415416
assert(!rows.any((DataRow row) => row.cells.length != columns.length)),
416417
assert(dividerThickness == null || dividerThickness >= 0),
418+
assert(clipBehavior != null),
417419
_onlyTextColumn = _initOnlyTextColumn(columns);
418420

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

643+
/// {@macro flutter.material.Material.clipBehavior}
644+
///
645+
/// This can be used to clip the content within the border of the [DataTable].
646+
///
647+
/// Defaults to [Clip.none], and must not be null.
648+
final Clip clipBehavior;
649+
641650
// Set by the constructor to the index of the only Column that is
642651
// non-numeric, if there is exactly one, otherwise null.
643652
final int? _onlyTextColumn;
@@ -1056,6 +1065,8 @@ class DataTable extends StatelessWidget {
10561065
decoration: decoration ?? dataTableTheme.decoration ?? theme.dataTableTheme.decoration,
10571066
child: Material(
10581067
type: MaterialType.transparency,
1068+
borderRadius: border?.borderRadius,
1069+
clipBehavior: clipBehavior,
10591070
child: Table(
10601071
columnWidths: tableColumns.asMap(),
10611072
children: tableRows,

packages/flutter/lib/src/rendering/table_border.dart

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class TableBorder {
7474
final BorderSide verticalInside;
7575

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

7981
/// The widths of the sides of this border represented as an [EdgeInsets].

packages/flutter/test/material/data_table_test.dart

+49
Original file line numberDiff line numberDiff line change
@@ -1895,4 +1895,53 @@ void main() {
18951895
// Go without crashes.
18961896

18971897
});
1898+
1899+
testWidgets('DataTable clip behavior', (WidgetTester tester) async {
1900+
const Color selectedColor = Colors.green;
1901+
const Color defaultColor = Colors.red;
1902+
const BorderRadius borderRadius = BorderRadius.all(Radius.circular(30));
1903+
1904+
Widget buildTable({bool selected = false, required Clip clipBehavior}) {
1905+
return Material(
1906+
child: DataTable(
1907+
clipBehavior: clipBehavior,
1908+
border: TableBorder.all(borderRadius: borderRadius),
1909+
columns: const <DataColumn>[
1910+
DataColumn(
1911+
label: Text('Column1'),
1912+
),
1913+
],
1914+
rows: <DataRow>[
1915+
DataRow(
1916+
selected: selected,
1917+
color: MaterialStateProperty.resolveWith<Color>(
1918+
(Set<MaterialState> states) {
1919+
if (states.contains(MaterialState.selected)) {
1920+
return selectedColor;
1921+
}
1922+
return defaultColor;
1923+
},
1924+
),
1925+
cells: const <DataCell>[
1926+
DataCell(Text('Content1')),
1927+
],
1928+
),
1929+
],
1930+
),
1931+
);
1932+
}
1933+
1934+
// Test default clip behavior.
1935+
await tester.pumpWidget(MaterialApp(home: buildTable(clipBehavior: Clip.none)));
1936+
1937+
Material material = tester.widget<Material>(find.byType(Material).last);
1938+
expect(material.clipBehavior, Clip.none);
1939+
expect(material.borderRadius, borderRadius);
1940+
1941+
await tester.pumpWidget(MaterialApp(home: buildTable(clipBehavior: Clip.hardEdge)));
1942+
1943+
material = tester.widget<Material>(find.byType(Material).last);
1944+
expect(material.clipBehavior, Clip.hardEdge);
1945+
expect(material.borderRadius, borderRadius);
1946+
});
18981947
}

0 commit comments

Comments
 (0)