From bf5e4b45feafb479c47ab22d6297ba9137895838 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 28 Aug 2018 19:21:00 -0700 Subject: [PATCH] Make sure modal BottomSheets routes are identified as dialog routes (#21075) --- .../lib/src/material/bottom_sheet.dart | 43 +++++++++++++---- .../material/modal_bottom_sheet_test.dart | 48 +++++++++++++++++++ 2 files changed, 82 insertions(+), 9 deletions(-) diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index 995c755d0c..f84da1122f 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -4,6 +4,7 @@ import 'dart:async'; +import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; import 'colors.dart'; @@ -141,6 +142,7 @@ class _BottomSheetState extends State { onVerticalDragUpdate: _handleDragUpdate, onVerticalDragEnd: _handleDragEnd, child: bottomSheet, + excludeFromSemantics: true, ); } } @@ -190,20 +192,43 @@ class _ModalBottomSheet extends StatefulWidget { class _ModalBottomSheetState extends State<_ModalBottomSheet> { @override Widget build(BuildContext context) { + final MediaQueryData mediaQuery = MediaQuery.of(context); + final MaterialLocalizations localizations = MaterialLocalizations.of(context); + String routeLabel; + switch (defaultTargetPlatform) { + case TargetPlatform.iOS: + routeLabel = ''; + break; + case TargetPlatform.android: + case TargetPlatform.fuchsia: + routeLabel = localizations.dialogLabel; + break; + } + return new GestureDetector( + excludeFromSemantics: true, onTap: () => Navigator.pop(context), child: new AnimatedBuilder( animation: widget.route.animation, builder: (BuildContext context, Widget child) { - return new ClipRect( - child: new CustomSingleChildLayout( - delegate: new _ModalBottomSheetLayout(widget.route.animation.value), - child: new BottomSheet( - animationController: widget.route._animationController, - onClosing: () => Navigator.pop(context), - builder: widget.route.builder - ) - ) + // Disable the initial animation when accessible navigation is on so + // that the semantics are added to the tree at the correct time. + final double animationValue = mediaQuery.accessibleNavigation ? 1.0 : widget.route.animation.value; + return new Semantics( + scopesRoute: true, + namesRoute: true, + label: routeLabel, + explicitChildNodes: true, + child: new ClipRect( + child: new CustomSingleChildLayout( + delegate: new _ModalBottomSheetLayout(animationValue), + child: new BottomSheet( + animationController: widget.route._animationController, + onClosing: () => Navigator.pop(context), + builder: widget.route.builder, + ), + ), + ), ); } ) diff --git a/packages/flutter/test/material/modal_bottom_sheet_test.dart b/packages/flutter/test/material/modal_bottom_sheet_test.dart index dd71a673ff..ca5861b82c 100644 --- a/packages/flutter/test/material/modal_bottom_sheet_test.dart +++ b/packages/flutter/test/material/modal_bottom_sheet_test.dart @@ -7,6 +7,8 @@ import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter/gestures.dart'; +import '../widgets/semantics_tester.dart'; + void main() { testWidgets('Verify that a tap dismisses a modal BottomSheet', (WidgetTester tester) async { BuildContext savedContext; @@ -203,4 +205,50 @@ void main() { const EdgeInsets.only(left: 50.0, right: 50.0, bottom: 50.0), ); }); + + testWidgets('modal BottomSheet has semantics', (WidgetTester tester) async { + final SemanticsTester semantics = new SemanticsTester(tester); + final GlobalKey scaffoldKey = new GlobalKey(); + + await tester.pumpWidget(new MaterialApp( + home: new Scaffold( + key: scaffoldKey, + body: const Center(child: Text('body')) + ) + )); + + + showModalBottomSheet(context: scaffoldKey.currentContext, builder: (BuildContext context) { + return new Container( + child: const Text('BottomSheet') + ); + }); + + await tester.pump(); // bottom sheet show animation starts + await tester.pump(const Duration(seconds: 1)); // animation done + + expect(semantics, hasSemantics(new TestSemantics.root( + children: [ + new TestSemantics.rootChild( + children: [ + new TestSemantics( + label: 'Dialog', + textDirection: TextDirection.ltr, + flags: [ + SemanticsFlag.scopesRoute, + SemanticsFlag.namesRoute, + ], + children: [ + new TestSemantics( + label: 'BottomSheet', + textDirection: TextDirection.ltr, + ), + ], + ), + ], + ), + ], + ), ignoreTransform: true, ignoreRect: true, ignoreId: true)); + semantics.dispose(); + }); }