Skip to content

Commit 49eac1f

Browse files
authored
[two_dimensional_scrollables] Add borderRadius support to TableSpanDecoration (#5184)
Part of #134655 ![Screenshot 2023-10-19 at 3 01 17 PM](https://github.com/flutter/packages/assets/16964204/b185ce08-9be0-4771-83df-d79df66774b6) This may look wonky, but it is verifying all the combinations of consuming/not consuming the padding with decorations. One golden file to rule them all. � Thankfully we can refactor this very soon in - #136933 ![tableSpanDecoration defaultMainAxis](https://github.com/flutter/packages/assets/16964204/87d06e02-7708-47a6-b473-f62999a6c74b)
1 parent e890f6b commit 49eac1f

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

packages/two_dimensional_scrollables/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## NEXT
22

33
* Fixes bug where having one reversed axis caused incorrect painting of a pinned row.
4+
* Adds support for BorderRadius in TableSpanDecorations.
45

56
## 0.0.4
67

packages/two_dimensional_scrollables/lib/src/table_view/table_span.dart

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,19 @@ class TableSpanDecoration {
296296
const TableSpanDecoration({
297297
this.border,
298298
this.color,
299+
this.borderRadius,
299300
this.consumeSpanPadding = true,
300301
});
301302

302303
/// The border drawn around the span.
303304
final TableSpanBorder? border;
304305

306+
/// The radius by which the leading and trailing ends of a row or
307+
/// column will be rounded.
308+
///
309+
/// Applies to the [border] and [color] of the given [TableSpan].
310+
final BorderRadius? borderRadius;
311+
305312
/// The color to fill the bounds of the span with.
306313
final Color? color;
307314

@@ -364,15 +371,20 @@ class TableSpanDecoration {
364371
/// cells.
365372
void paint(TableSpanDecorationPaintDetails details) {
366373
if (color != null) {
367-
details.canvas.drawRect(
368-
details.rect,
369-
Paint()
370-
..color = color!
371-
..isAntiAlias = false,
372-
);
374+
final Paint paint = Paint()
375+
..color = color!
376+
..isAntiAlias = borderRadius != null;
377+
if (borderRadius == null || borderRadius == BorderRadius.zero) {
378+
details.canvas.drawRect(details.rect, paint);
379+
} else {
380+
details.canvas.drawRRect(
381+
borderRadius!.toRRect(details.rect),
382+
paint,
383+
);
384+
}
373385
}
374386
if (border != null) {
375-
border!.paint(details);
387+
border!.paint(details, borderRadius);
376388
}
377389
}
378390
}
@@ -416,24 +428,33 @@ class TableSpanBorder {
416428
/// cell representing the pinned column and separately with another
417429
/// [TableSpanDecorationPaintDetails.rect] containing all the other unpinned
418430
/// cells.
419-
void paint(TableSpanDecorationPaintDetails details) {
431+
void paint(
432+
TableSpanDecorationPaintDetails details,
433+
BorderRadius? borderRadius,
434+
) {
420435
final AxisDirection axisDirection = details.axisDirection;
421436
switch (axisDirectionToAxis(axisDirection)) {
422437
case Axis.horizontal:
423-
paintBorder(
424-
details.canvas,
425-
details.rect,
438+
final Border border = Border(
426439
top: axisDirection == AxisDirection.right ? leading : trailing,
427440
bottom: axisDirection == AxisDirection.right ? trailing : leading,
428441
);
429-
break;
430-
case Axis.vertical:
431-
paintBorder(
442+
border.paint(
432443
details.canvas,
433444
details.rect,
445+
borderRadius: borderRadius,
446+
);
447+
break;
448+
case Axis.vertical:
449+
final Border border = Border(
434450
left: axisDirection == AxisDirection.down ? leading : trailing,
435451
right: axisDirection == AxisDirection.down ? trailing : leading,
436452
);
453+
border.paint(
454+
details.canvas,
455+
details.rect,
456+
borderRadius: borderRadius,
457+
);
437458
break;
438459
}
439460
}

packages/two_dimensional_scrollables/test/table_view/table_span_test.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,21 @@ void main() {
161161
rect: rect,
162162
axisDirection: AxisDirection.down,
163163
);
164+
final BorderRadius radius = BorderRadius.circular(10.0);
164165
decoration.paint(details);
165166
expect(canvas.rect, rect);
166167
expect(canvas.paint.color, const Color(0xffff0000));
167168
expect(canvas.paint.isAntiAlias, isFalse);
168169
final TestTableSpanBorder border = TestTableSpanBorder(
169170
leading: const BorderSide(),
170171
);
171-
decoration = TableSpanDecoration(border: border);
172+
decoration = TableSpanDecoration(
173+
border: border,
174+
borderRadius: radius,
175+
);
172176
decoration.paint(details);
173177
expect(border.details, details);
178+
expect(border.radius, radius);
174179
});
175180
}
176181

@@ -194,8 +199,10 @@ class TestCanvas implements Canvas {
194199
class TestTableSpanBorder extends TableSpanBorder {
195200
TestTableSpanBorder({super.leading});
196201
TableSpanDecorationPaintDetails? details;
202+
BorderRadius? radius;
197203
@override
198-
void paint(TableSpanDecorationPaintDetails details) {
204+
void paint(TableSpanDecorationPaintDetails details, BorderRadius? radius) {
199205
this.details = details;
206+
this.radius = radius;
200207
}
201208
}

packages/two_dimensional_scrollables/test/table_view/table_test.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,17 +1256,18 @@ void main() {
12561256
// TODO(Piinks): Rewrite this to remove golden files from this repo when
12571257
// mock_canvas is public - https://github.com/flutter/flutter/pull/131631
12581258
// * foreground, background, and precedence per mainAxis
1259-
// * Break out a separate test for padding decorations to validate paint
1260-
// rect calls
1259+
// * Break out a separate test for padding and radius decorations to
1260+
// validate paint rect calls
12611261
TableView tableView = TableView.builder(
12621262
rowCount: 2,
12631263
columnCount: 2,
12641264
columnBuilder: (int index) => TableSpan(
12651265
extent: const FixedTableSpanExtent(200.0),
12661266
padding: index == 0 ? const TableSpanPadding(trailing: 10) : null,
1267-
foregroundDecoration: const TableSpanDecoration(
1267+
foregroundDecoration: TableSpanDecoration(
12681268
consumeSpanPadding: false,
1269-
border: TableSpanBorder(
1269+
borderRadius: BorderRadius.circular(10.0),
1270+
border: const TableSpanBorder(
12701271
trailing: BorderSide(
12711272
color: Colors.orange,
12721273
width: 3,
@@ -1276,14 +1277,16 @@ void main() {
12761277
backgroundDecoration: TableSpanDecoration(
12771278
// consumePadding true by default
12781279
color: index.isEven ? Colors.red : null,
1280+
borderRadius: BorderRadius.circular(30.0),
12791281
),
12801282
),
12811283
rowBuilder: (int index) => TableSpan(
12821284
extent: const FixedTableSpanExtent(200.0),
12831285
padding: index == 1 ? const TableSpanPadding(leading: 10) : null,
1284-
foregroundDecoration: const TableSpanDecoration(
1286+
foregroundDecoration: TableSpanDecoration(
12851287
// consumePadding true by default
1286-
border: TableSpanBorder(
1288+
borderRadius: BorderRadius.circular(30.0),
1289+
border: const TableSpanBorder(
12871290
leading: BorderSide(
12881291
color: Colors.green,
12891292
width: 3,
@@ -1292,6 +1295,7 @@ void main() {
12921295
),
12931296
backgroundDecoration: TableSpanDecoration(
12941297
color: index.isOdd ? Colors.blue : null,
1298+
borderRadius: BorderRadius.circular(30.0),
12951299
consumeSpanPadding: false,
12961300
),
12971301
),

0 commit comments

Comments
 (0)