Skip to content

Commit 4a3ab68

Browse files
authored
Fix DataTable example not being scrollable (#131556)
1 parent 0770c60 commit 4a3ab68

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

dev/bots/check_code_samples.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ final Set<String> _knownMissingTests = <String>{
292292
'examples/api/test/material/stepper/stepper.controls_builder.0_test.dart',
293293
'examples/api/test/material/stepper/stepper.0_test.dart',
294294
'examples/api/test/material/flexible_space_bar/flexible_space_bar.0_test.dart',
295-
'examples/api/test/material/data_table/data_table.1_test.dart',
296295
'examples/api/test/material/data_table/data_table.0_test.dart',
297296
'examples/api/test/material/floating_action_button_location/standard_fab_location.0_test.dart',
298297
'examples/api/test/material/chip/deletable_chip_attributes.on_deleted.0_test.dart',

examples/api/lib/material/data_table/data_table.1.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ class DataTableExample extends StatefulWidget {
3030
}
3131

3232
class _DataTableExampleState extends State<DataTableExample> {
33-
static const int numItems = 10;
33+
static const int numItems = 20;
3434
List<bool> selected = List<bool>.generate(numItems, (int index) => false);
3535

3636
@override
3737
Widget build(BuildContext context) {
38-
return SizedBox(
39-
width: double.infinity,
38+
return SingleChildScrollView(
4039
child: DataTable(
4140
columns: const <DataColumn>[
4241
DataColumn(
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/material/data_table/data_table.1.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('DataTable is scrollable', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const example.DataTableExampleApp(),
13+
);
14+
15+
expect(find.byType(SingleChildScrollView), findsOneWidget);
16+
17+
expect(tester.getTopLeft(find.text('Row 5')), const Offset(66.0, 366.0));
18+
19+
await tester.drag(find.byType(SingleChildScrollView), const Offset(0.0, -200.0));
20+
await tester.pumpAndSettle();
21+
22+
expect(tester.getTopLeft(find.text('Row 5')), const Offset(66.0, 186.0));
23+
});
24+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,13 @@ class DataCell {
357357
/// [PaginatedDataTable] which automatically splits the data into
358358
/// multiple pages.
359359
///
360+
/// ## Performance considerations when wrapping [DataTable] with [SingleChildScrollView]
361+
///
362+
/// Wrapping a [DataTable] with [SingleChildScrollView] is expensive as [SingleChildScrollView]
363+
/// mounts and paints the entire [DataTable] even when only some rows are visible. If scrolling in
364+
/// one direction is necessary, then consider using a [CustomScrollView], otherwise use [PaginatedDataTable]
365+
/// to split the data into smaller pages.
366+
///
360367
/// {@tool dartpad}
361368
/// This sample shows how to display a [DataTable] with three columns: name, age, and
362369
/// role. The columns are defined by three [DataColumn] objects. The table

0 commit comments

Comments
 (0)