Skip to content

Commit b7fbe68

Browse files
authored
[adaptive_scaffold] : šŸ› flutter#141938 - Drawer stays open even on destination tap. (flutter#6289)
(flutter#141938) *Changes included in PR are listed as follows* - As per material guidelines, Drawer shall be dismissed when user taps any destination/item. If drawer is open, and user taps on any item, before calling onDestinationSelected() - we are now dismissing drawer. - CHANGELOG.md file updated. - Updated to v0.1.9. *Issue : flutter#141938
1 parent 8df9848 commit b7fbe68

File tree

5 files changed

+138
-5
lines changed

5 files changed

+138
-5
lines changed

ā€Žpackages/flutter_adaptive_scaffold/AUTHORS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
# Name/Organization <email address>
55

66
Google Inc.
7-
Jason C.H <[email protected]>
7+
Jason C.H <[email protected]>
8+
Aliasgar Vohra <[email protected]>

ā€Žpackages/flutter_adaptive_scaffold/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
## NEXT
1+
## 0.1.9
22

3+
* FIX : Drawer stays open even on destination tap - [flutter/flutter#141938](https://github.com/flutter/flutter/issues/141938)
34
* Updates minimum supported SDK version to Flutter 3.13/Dart 3.1.
45

56
## 0.1.8
67

7-
* Adds `transitionDuration` parameter for specifying how long the animation should be.
8+
* Adds `transitionDuration` parameter for specifying how long the animation should be.
89

910
## 0.1.7+2
1011

ā€Žpackages/flutter_adaptive_scaffold/lib/src/adaptive_scaffold.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,12 +502,16 @@ class AdaptiveScaffold extends StatefulWidget {
502502
}
503503

504504
class _AdaptiveScaffoldState extends State<AdaptiveScaffold> {
505+
// Global scaffold key that will help to manage drawer state.
506+
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
507+
505508
@override
506509
Widget build(BuildContext context) {
507510
final NavigationRailThemeData navRailTheme =
508511
Theme.of(context).navigationRailTheme;
509512

510513
return Scaffold(
514+
key: _scaffoldKey,
511515
appBar: widget.drawerBreakpoint.isActive(context) && widget.useDrawer ||
512516
(widget.appBarBreakpoint?.isActive(context) ?? false)
513517
? widget.appBar ?? AppBar()
@@ -523,7 +527,7 @@ class _AdaptiveScaffoldState extends State<AdaptiveScaffold> {
523527
.map((NavigationDestination destination) =>
524528
AdaptiveScaffold.toRailDestination(destination))
525529
.toList(),
526-
onDestinationSelected: widget.onSelectedIndexChange,
530+
onDestinationSelected: _onDrawerDestinationSelected,
527531
),
528532
)
529533
: null,
@@ -670,6 +674,20 @@ class _AdaptiveScaffoldState extends State<AdaptiveScaffold> {
670674
),
671675
);
672676
}
677+
678+
void _onDrawerDestinationSelected(int index) {
679+
if (widget.useDrawer) {
680+
// If [useDrawer] is true, then retrieve the current state.
681+
final ScaffoldState? scaffoldCurrentContext = _scaffoldKey.currentState;
682+
if (scaffoldCurrentContext != null) {
683+
if (scaffoldCurrentContext.isDrawerOpen) {
684+
// If drawer is open, call [closeDrawer] to dismiss drawer as per material guidelines.
685+
scaffoldCurrentContext.closeDrawer();
686+
}
687+
}
688+
}
689+
widget.onSelectedIndexChange?.call(index);
690+
}
673691
}
674692

675693
class _BrickLayout extends StatelessWidget {

ā€Žpackages/flutter_adaptive_scaffold/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_adaptive_scaffold
22
description: Widgets to easily build adaptive layouts, including navigation elements.
3-
version: 0.1.8
3+
version: 0.1.9
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_adaptive_scaffold%22
55
repository: https://github.com/flutter/packages/tree/main/packages/flutter_adaptive_scaffold
66

ā€Žpackages/flutter_adaptive_scaffold/test/adaptive_scaffold_test.dart

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,119 @@ void main() {
501501
},
502502
);
503503

504+
testWidgets(
505+
'when drawer item tap, it shall close the already open drawer',
506+
(WidgetTester tester) async {
507+
//region Keys
508+
const ValueKey<String> firstDestinationIconKey = ValueKey<String>(
509+
'first-normal-icon',
510+
);
511+
const ValueKey<String> firstDestinationSelectedIconKey = ValueKey<String>(
512+
'first-selected-icon',
513+
);
514+
const ValueKey<String> lastDestinationIconKey = ValueKey<String>(
515+
'last-normal-icon',
516+
);
517+
const ValueKey<String> lastDestinationSelectedIconKey = ValueKey<String>(
518+
'last-selected-icon',
519+
);
520+
//endregion
521+
522+
//region Finder for destinations as per its icon.
523+
final Finder lastDestinationWithIcon = find.byKey(
524+
lastDestinationIconKey,
525+
);
526+
final Finder lastDestinationWithSelectedIcon = find.byKey(
527+
lastDestinationSelectedIconKey,
528+
);
529+
//endregion
530+
531+
const List<NavigationDestination> destinations = <NavigationDestination>[
532+
NavigationDestination(
533+
icon: Icon(
534+
Icons.inbox_outlined,
535+
key: firstDestinationIconKey,
536+
),
537+
selectedIcon: Icon(
538+
Icons.inbox,
539+
key: firstDestinationSelectedIconKey,
540+
),
541+
label: 'Inbox',
542+
),
543+
NavigationDestination(
544+
icon: Icon(
545+
Icons.video_call_outlined,
546+
key: lastDestinationIconKey,
547+
),
548+
selectedIcon: Icon(
549+
Icons.video_call,
550+
key: lastDestinationSelectedIconKey,
551+
),
552+
label: 'Video',
553+
),
554+
];
555+
int selectedDestination = 0;
556+
557+
await tester.pumpWidget(
558+
MaterialApp(
559+
home: MediaQuery(
560+
data: const MediaQueryData(size: Size(450, 900)),
561+
child: StatefulBuilder(
562+
builder: (
563+
BuildContext context,
564+
void Function(void Function()) setState,
565+
) {
566+
return AdaptiveScaffold(
567+
destinations: destinations,
568+
selectedIndex: selectedDestination,
569+
smallBreakpoint: TestBreakpoint400(),
570+
drawerBreakpoint: TestBreakpoint400(),
571+
onSelectedIndexChange: (int value) {
572+
setState(() {
573+
selectedDestination = value;
574+
});
575+
},
576+
);
577+
},
578+
),
579+
),
580+
),
581+
);
582+
583+
expect(selectedDestination, 0);
584+
Finder fDrawer = find.byType(Drawer);
585+
Finder fNavigationRail = find.descendant(
586+
of: fDrawer,
587+
matching: find.byType(NavigationRail),
588+
);
589+
expect(fDrawer, findsNothing);
590+
expect(fNavigationRail, findsNothing);
591+
592+
final ScaffoldState state = tester.state(find.byType(Scaffold));
593+
state.openDrawer();
594+
await tester.pumpAndSettle(Durations.short4);
595+
expect(state.isDrawerOpen, isTrue);
596+
597+
// Need to find again as Scaffold's state has been updated
598+
fDrawer = find.byType(Drawer);
599+
fNavigationRail = find.descendant(
600+
of: fDrawer,
601+
matching: find.byType(NavigationRail),
602+
);
603+
expect(fDrawer, findsOneWidget);
604+
expect(fNavigationRail, findsOneWidget);
605+
606+
expect(lastDestinationWithIcon, findsOneWidget);
607+
expect(lastDestinationWithSelectedIcon, findsNothing);
608+
609+
await tester.ensureVisible(lastDestinationWithIcon);
610+
await tester.tap(lastDestinationWithIcon);
611+
await tester.pumpAndSettle(Durations.short4);
612+
expect(selectedDestination, 1);
613+
expect(state.isDrawerOpen, isFalse);
614+
},
615+
);
616+
504617
// This test checks whether AdaptiveScaffold.standardNavigationRail function
505618
// creates a NavigationRail widget as expected with groupAlignment provided,
506619
// and checks whether the NavigationRail's groupAlignment matches the expected value.

0 commit comments

Comments
Ā (0)