Add table related semantics role (#162339)
issue: https://github.com/flutter/flutter/issues/45205 This PR added roles: table,cell,row,columnheader in engine side. Also added table, cell , columnheader in framework widgets `Table` and `DataTable`. This PR didn't add `row` in framework side because right now TableCell is ParentDataWidget expecting its parent to be a Table, tableRow is not a widget or a renderObject, but just some data. If we want to add a role to `row`, we need to do some refactor to `TableCell`, `TableRow` and `Table`. If we need to add row, I will do that in another PR. ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
library;
|
||||
|
||||
import 'dart:math' as math;
|
||||
import 'dart:ui' show SemanticsRole;
|
||||
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
@@ -879,22 +880,25 @@ class DataTable extends StatelessWidget {
|
||||
}) {
|
||||
final ThemeData themeData = Theme.of(context);
|
||||
final DataTableThemeData dataTableTheme = DataTableTheme.of(context);
|
||||
label = Row(
|
||||
textDirection: numeric ? TextDirection.rtl : null,
|
||||
mainAxisAlignment: headingRowAlignment,
|
||||
children: <Widget>[
|
||||
if (headingRowAlignment == MainAxisAlignment.center && onSort != null)
|
||||
const SizedBox(width: _SortArrowState._arrowIconSize + _sortArrowPadding),
|
||||
label,
|
||||
if (onSort != null) ...<Widget>[
|
||||
_SortArrow(
|
||||
visible: sorted,
|
||||
up: sorted ? ascending : null,
|
||||
duration: _sortArrowAnimationDuration,
|
||||
),
|
||||
const SizedBox(width: _sortArrowPadding),
|
||||
label = Semantics(
|
||||
role: SemanticsRole.columnHeader,
|
||||
child: Row(
|
||||
textDirection: numeric ? TextDirection.rtl : null,
|
||||
mainAxisAlignment: headingRowAlignment,
|
||||
children: <Widget>[
|
||||
if (headingRowAlignment == MainAxisAlignment.center && onSort != null)
|
||||
const SizedBox(width: _SortArrowState._arrowIconSize + _sortArrowPadding),
|
||||
label,
|
||||
if (onSort != null) ...<Widget>[
|
||||
_SortArrow(
|
||||
visible: sorted,
|
||||
up: sorted ? ascending : null,
|
||||
duration: _sortArrowAnimationDuration,
|
||||
),
|
||||
const SizedBox(width: _sortArrowPadding),
|
||||
],
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
final TextStyle effectiveHeadingTextStyle =
|
||||
@@ -1013,7 +1017,7 @@ class DataTable extends StatelessWidget {
|
||||
child: label,
|
||||
);
|
||||
}
|
||||
return label;
|
||||
return TableCell(child: label);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4,8 +4,10 @@
|
||||
|
||||
import 'dart:collection';
|
||||
import 'dart:math' as math;
|
||||
import 'dart:ui' show SemanticsRole;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/semantics.dart';
|
||||
|
||||
import 'box.dart';
|
||||
import 'object.dart';
|
||||
@@ -601,6 +603,13 @@ class RenderTable extends RenderBox {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void describeSemanticsConfiguration(SemanticsConfiguration config) {
|
||||
super.describeSemanticsConfiguration(config);
|
||||
config.role = SemanticsRole.table;
|
||||
config.explicitChildNodes = true;
|
||||
}
|
||||
|
||||
/// Replaces the children of this table with the given cells.
|
||||
///
|
||||
/// The cells are divided into the specified number of columns before
|
||||
|
||||
@@ -110,8 +110,14 @@ sealed class _DebugSemanticsRoleChecks {
|
||||
SemanticsRole.tab => _semanticsTab,
|
||||
SemanticsRole.tabBar => _semanticsTabBar,
|
||||
SemanticsRole.tabPanel => _noCheckRequired,
|
||||
SemanticsRole.table => _noCheckRequired,
|
||||
SemanticsRole.cell => _semanticsCell,
|
||||
SemanticsRole.row => _unimplementedError,
|
||||
SemanticsRole.columnHeader => _semanticsColumnHeader,
|
||||
}(node);
|
||||
|
||||
static FlutterError? _unimplementedError(SemanticsNode node) =>
|
||||
FlutterError('This semantics role is not implemented');
|
||||
static FlutterError? _noCheckRequired(SemanticsNode node) => null;
|
||||
|
||||
static FlutterError? _semanticsTab(SemanticsNode node) {
|
||||
@@ -140,6 +146,20 @@ sealed class _DebugSemanticsRoleChecks {
|
||||
});
|
||||
return error;
|
||||
}
|
||||
|
||||
static FlutterError? _semanticsCell(SemanticsNode node) {
|
||||
if (node.parent?.role != SemanticsRole.table) {
|
||||
return FlutterError('A cell must be a child of a table');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static FlutterError? _semanticsColumnHeader(SemanticsNode node) {
|
||||
if (node.parent?.role != SemanticsRole.table) {
|
||||
return FlutterError('A columnHeader must be a child of a table');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// A tag for a [SemanticsNode].
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
library;
|
||||
|
||||
import 'dart:collection';
|
||||
import 'dart:ui' show SemanticsRole;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
@@ -438,7 +439,8 @@ class _TableElement extends RenderObjectElement {
|
||||
/// as the [child].
|
||||
class TableCell extends ParentDataWidget<TableCellParentData> {
|
||||
/// Creates a widget that controls how a child of a [Table] is aligned.
|
||||
const TableCell({super.key, this.verticalAlignment, required super.child});
|
||||
TableCell({super.key, this.verticalAlignment, required Widget child})
|
||||
: super(child: Semantics(role: SemanticsRole.cell, child: child));
|
||||
|
||||
/// How this cell is aligned vertically.
|
||||
final TableCellVerticalAlignment? verticalAlignment;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
library;
|
||||
|
||||
import 'dart:math' as math;
|
||||
import 'dart:ui' show SemanticsRole;
|
||||
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -13,6 +14,7 @@ import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:vector_math/vector_math_64.dart' show Matrix3;
|
||||
|
||||
import '../widgets/semantics_tester.dart';
|
||||
import 'data_table_test_utils.dart';
|
||||
|
||||
void main() {
|
||||
@@ -2211,6 +2213,78 @@ void main() {
|
||||
expect(table.columnWidths![1], const IntrinsicColumnWidth());
|
||||
expect(table.columnWidths![2], const IntrinsicColumnWidth(flex: 1));
|
||||
});
|
||||
|
||||
testWidgets('DataTable has correct roles in semantics', (WidgetTester tester) async {
|
||||
final SemanticsTester semantics = SemanticsTester(tester);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: DataTable(
|
||||
columns: const <DataColumn>[
|
||||
DataColumn(label: Text('Column 1')),
|
||||
DataColumn(label: Text('Column 2')),
|
||||
],
|
||||
rows: const <DataRow>[
|
||||
DataRow(
|
||||
cells: <DataCell>[DataCell(Text('Data Cell 1')), DataCell(Text('Data Cell 2'))],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final TestSemantics expectedSemantics = TestSemantics.root(
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
textDirection: TextDirection.ltr,
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
role: SemanticsRole.table,
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
label: 'Column 1',
|
||||
textDirection: TextDirection.ltr,
|
||||
role: SemanticsRole.columnHeader,
|
||||
),
|
||||
TestSemantics(
|
||||
label: 'Column 2',
|
||||
textDirection: TextDirection.ltr,
|
||||
role: SemanticsRole.columnHeader,
|
||||
),
|
||||
TestSemantics(
|
||||
label: 'Data Cell 1',
|
||||
textDirection: TextDirection.ltr,
|
||||
role: SemanticsRole.cell,
|
||||
),
|
||||
TestSemantics(
|
||||
label: 'Data Cell 2',
|
||||
textDirection: TextDirection.ltr,
|
||||
role: SemanticsRole.cell,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
expect(
|
||||
semantics,
|
||||
hasSemantics(expectedSemantics, ignoreTransform: true, ignoreId: true, ignoreRect: true),
|
||||
);
|
||||
|
||||
semantics.dispose();
|
||||
});
|
||||
}
|
||||
|
||||
RenderParagraph _getTextRenderObject(WidgetTester tester, String text) {
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:ui' show SemanticsRole;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'semantics_tester.dart';
|
||||
|
||||
class TestStatefulWidget extends StatefulWidget {
|
||||
const TestStatefulWidget({super.key});
|
||||
@@ -951,5 +953,64 @@ void main() {
|
||||
expect(boxD.size.height, greaterThan(boxA.size.height));
|
||||
});
|
||||
|
||||
// TODO(ianh): Test handling of TableCell object
|
||||
testWidgets('Table has correct roles in semantics', (WidgetTester tester) async {
|
||||
final SemanticsTester semantics = SemanticsTester(tester);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Table(
|
||||
children: <TableRow>[
|
||||
TableRow(
|
||||
children: <Widget>[
|
||||
TableCell(child: const Text('Data Cell 1')),
|
||||
TableCell(child: const Text('Data Cell 2')),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final TestSemantics expectedSemantics = TestSemantics.root(
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
textDirection: TextDirection.ltr,
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
role: SemanticsRole.table,
|
||||
children: <TestSemantics>[
|
||||
TestSemantics(
|
||||
label: 'Data Cell 1',
|
||||
textDirection: TextDirection.ltr,
|
||||
role: SemanticsRole.cell,
|
||||
),
|
||||
TestSemantics(
|
||||
label: 'Data Cell 2',
|
||||
textDirection: TextDirection.ltr,
|
||||
role: SemanticsRole.cell,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
expect(
|
||||
semantics,
|
||||
hasSemantics(expectedSemantics, ignoreTransform: true, ignoreId: true, ignoreRect: true),
|
||||
);
|
||||
|
||||
semantics.dispose();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user