fix Applying decoration for a table row widget will cause render exception (#34285)

This commit is contained in:
chunhtai
2019-06-12 16:58:24 -07:00
committed by GitHub
parent c40c687b2d
commit ad507723ec
2 changed files with 40 additions and 1 deletions

View File

@@ -518,6 +518,8 @@ class RenderTable extends RenderBox {
/// the table, unlike decorations for individual cells, which might not fill
/// either.
List<Decoration> get rowDecorations => List<Decoration>.unmodifiable(_rowDecorations ?? const <Decoration>[]);
// _rowDecorations and _rowDecorationPainters need to be in sync. They have to
// either both be null or have same length.
List<Decoration> _rowDecorations;
List<BoxPainter> _rowDecorationPainters;
set rowDecorations(List<Decoration> value) {
@@ -703,7 +705,7 @@ class RenderTable extends RenderBox {
if (_rowDecorationPainters != null) {
for (BoxPainter painter in _rowDecorationPainters)
painter?.dispose();
_rowDecorationPainters = null;
_rowDecorationPainters = List<BoxPainter>(_rowDecorations.length);
}
for (RenderBox child in _children)
child?.detach();
@@ -1137,6 +1139,7 @@ class RenderTable extends RenderBox {
}
assert(_rowTops.length == rows + 1);
if (_rowDecorations != null) {
assert(_rowDecorations.length == _rowDecorationPainters.length);
final Canvas canvas = context.canvas;
for (int y = 0; y < rows; y += 1) {
if (_rowDecorations.length <= y)

View File

@@ -5,6 +5,7 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
class TestStatefulWidget extends StatefulWidget {
const TestStatefulWidget({ Key key }) : super(key: key);
@@ -67,6 +68,41 @@ void main() {
await run(TextDirection.rtl);
});
testWidgets('Table widget can be detached and re-attached', (WidgetTester tester) async {
final Widget table = Table(
key: GlobalKey(),
children: const <TableRow>[
TableRow(
decoration: BoxDecoration(
color: Colors.yellow
),
children: <Widget>[Placeholder()],
),
],
);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: table,
),
),
);
// Move table to a different location to simulate detaching and re-attaching effect.
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: Center(
child: table
),
),
),
);
expect(tester.takeException(), isNull);
});
testWidgets('Table widget - column offset (LTR)', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(