From 86be138d9c975956005a7d2f2939e5d1eed96b8b Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Tue, 6 Mar 2018 15:08:39 -0800 Subject: [PATCH] Make BottomNavBar accessible (#15211) --- .../src/material/bottom_navigation_bar.dart | 44 +++++++--- .../material/bottom_navigation_bar_test.dart | 85 +++++++++++++++++-- 2 files changed, 111 insertions(+), 18 deletions(-) diff --git a/packages/flutter/lib/src/material/bottom_navigation_bar.dart b/packages/flutter/lib/src/material/bottom_navigation_bar.dart index 30ec443a77..3c6aa3bb86 100644 --- a/packages/flutter/lib/src/material/bottom_navigation_bar.dart +++ b/packages/flutter/lib/src/material/bottom_navigation_bar.dart @@ -13,6 +13,7 @@ import 'colors.dart'; import 'constants.dart'; import 'ink_well.dart'; import 'material.dart'; +import 'material_localizations.dart'; import 'theme.dart'; import 'typography.dart'; @@ -129,9 +130,11 @@ class _BottomNavigationTile extends StatelessWidget { this.iconSize, { this.onTap, this.colorTween, - this.flex + this.flex, + this.selected: false, + this.indexLabel, } - ); + ): assert(selected != null); final BottomNavigationBarType type; final BottomNavigationBarItem item; @@ -140,6 +143,8 @@ class _BottomNavigationTile extends StatelessWidget { final VoidCallback onTap; final ColorTween colorTween; final double flex; + final bool selected; + final String indexLabel; Widget _buildIcon() { double tweenStart; @@ -255,15 +260,26 @@ class _BottomNavigationTile extends StatelessWidget { } return new Expanded( flex: size, - child: new InkResponse( - onTap: onTap, - child: new Column( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.spaceBetween, - mainAxisSize: MainAxisSize.min, + child: new Semantics( + container: true, + selected: selected, + child: new Stack( children: [ - _buildIcon(), - label, + new InkResponse( + onTap: onTap, + child: new Column( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.spaceBetween, + mainAxisSize: MainAxisSize.min, + children: [ + _buildIcon(), + label, + ], + ), + ), + new Semantics( + label: indexLabel, + ) ], ), ), @@ -368,6 +384,8 @@ class _BottomNavigationBarState extends State with TickerPr } List _createTiles() { + final MaterialLocalizations localizations = MaterialLocalizations.of(context); + assert(localizations != null); final List children = []; switch (widget.type) { case BottomNavigationBarType.fixed: @@ -398,6 +416,8 @@ class _BottomNavigationBarState extends State with TickerPr widget.onTap(i); }, colorTween: colorTween, + selected: i == widget.currentIndex, + indexLabel: localizations.tabLabel(tabIndex: i + 1, tabCount: widget.items.length), ), ); } @@ -415,7 +435,9 @@ class _BottomNavigationBarState extends State with TickerPr widget.onTap(i); }, flex: _evaluateFlex(_animations[i]), - ), + selected: i == widget.currentIndex, + indexLabel: localizations.tabLabel(tabIndex: i + 1, tabCount: widget.items.length), + ) ); } break; diff --git a/packages/flutter/test/material/bottom_navigation_bar_test.dart b/packages/flutter/test/material/bottom_navigation_bar_test.dart index 39869712ff..3cbded6b32 100644 --- a/packages/flutter/test/material/bottom_navigation_bar_test.dart +++ b/packages/flutter/test/material/bottom_navigation_bar_test.dart @@ -2,11 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:ui'; + import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import '../rendering/mock_canvas.dart'; +import '../widgets/semantics_tester.dart'; void main() { testWidgets('BottomNavigationBar callback test', (WidgetTester tester) async { @@ -483,17 +486,85 @@ void main() { await tester.pump(const Duration(milliseconds: 20)); expect(box, paints..circle(x: 600.0)..circle(x: 200.0)..circle(x: 600.0)); }); + + testWidgets('BottomNavigationBar semantics', (WidgetTester tester) async { + final SemanticsTester semantics = new SemanticsTester(tester); + + await tester.pumpWidget( + boilerplate( + textDirection: TextDirection.ltr, + bottomNavigationBar: new BottomNavigationBar( + items: const [ + const BottomNavigationBarItem( + icon: const Icon(Icons.ac_unit), + title: const Text('AC'), + ), + const BottomNavigationBarItem( + icon: const Icon(Icons.access_alarm), + title: const Text('Alarm'), + ), + const BottomNavigationBarItem( + icon: const Icon(Icons.hot_tub), + title: const Text('Hot Tub'), + ), + ], + ), + ), + ); + + // TODO(goderbauer): traversal order is incorrect, https://github.com/flutter/flutter/issues/14375 + final TestSemantics expected = new TestSemantics.root( + children: [ + new TestSemantics( + id: 1, + flags: [SemanticsFlag.isSelected], + actions: [SemanticsAction.tap], + label: 'AC\nTab 1 of 3', + textDirection: TextDirection.ltr, + nextNodeId: -1, + previousNodeId: 3, // Should be 2 + ), + new TestSemantics( + id: 2, + actions: [SemanticsAction.tap], + label: 'Alarm\nTab 2 of 3', + textDirection: TextDirection.ltr, + nextNodeId: 3, + previousNodeId: -1, // Should be 1 + ), + new TestSemantics( + id: 3, + actions: [SemanticsAction.tap], + label: 'Hot Tub\nTab 3 of 3', + textDirection: TextDirection.ltr, + nextNodeId: 1, // Should be -1 + previousNodeId: 2, + ), + ], + ); + expect(semantics, hasSemantics(expected, ignoreTransform: true, ignoreRect: true)); + + semantics.dispose(); + }); + } Widget boilerplate({ Widget bottomNavigationBar, @required TextDirection textDirection }) { assert(textDirection != null); - return new Directionality( - textDirection: textDirection, - child: new MediaQuery( - data: const MediaQueryData(), - child: new Material( - child: new Scaffold( - bottomNavigationBar: bottomNavigationBar, + return new Localizations( + locale: const Locale('en', 'US'), + delegates: >[ + DefaultMaterialLocalizations.delegate, + DefaultWidgetsLocalizations.delegate, + ], + child: new Directionality( + textDirection: textDirection, + child: new MediaQuery( + data: const MediaQueryData(), + child: new Material( + child: new Scaffold( + bottomNavigationBar: bottomNavigationBar, + ), ), ), ),