Skip to content

Commit

Permalink
[adaptive_scaffold] : šŸ› #141938 - Drawer stays open even on destinatiā€¦
Browse files Browse the repository at this point in the history
ā€¦on tap. (#6289)

(#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/flutter#141938
  • Loading branch information
aliasgar4558 authored Mar 21, 2024
1 parent 8df9848 commit b7fbe68
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/flutter_adaptive_scaffold/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
# Name/Organization <email address>

Google Inc.
Jason C.H <ctrysbita@outlook.com>
Jason C.H <ctrysbita@outlook.com>
Aliasgar Vohra <aliasgar.vohra1101@gmail.com>
5 changes: 3 additions & 2 deletions packages/flutter_adaptive_scaffold/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
## NEXT
## 0.1.9

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

## 0.1.8

* Adds `transitionDuration` parameter for specifying how long the animation should be.
* Adds `transitionDuration` parameter for specifying how long the animation should be.

## 0.1.7+2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,16 @@ class AdaptiveScaffold extends StatefulWidget {
}

class _AdaptiveScaffoldState extends State<AdaptiveScaffold> {
// Global scaffold key that will help to manage drawer state.
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

@override
Widget build(BuildContext context) {
final NavigationRailThemeData navRailTheme =
Theme.of(context).navigationRailTheme;

return Scaffold(
key: _scaffoldKey,
appBar: widget.drawerBreakpoint.isActive(context) && widget.useDrawer ||
(widget.appBarBreakpoint?.isActive(context) ?? false)
? widget.appBar ?? AppBar()
Expand All @@ -523,7 +527,7 @@ class _AdaptiveScaffoldState extends State<AdaptiveScaffold> {
.map((NavigationDestination destination) =>
AdaptiveScaffold.toRailDestination(destination))
.toList(),
onDestinationSelected: widget.onSelectedIndexChange,
onDestinationSelected: _onDrawerDestinationSelected,
),
)
: null,
Expand Down Expand Up @@ -670,6 +674,20 @@ class _AdaptiveScaffoldState extends State<AdaptiveScaffold> {
),
);
}

void _onDrawerDestinationSelected(int index) {
if (widget.useDrawer) {
// If [useDrawer] is true, then retrieve the current state.
final ScaffoldState? scaffoldCurrentContext = _scaffoldKey.currentState;
if (scaffoldCurrentContext != null) {
if (scaffoldCurrentContext.isDrawerOpen) {
// If drawer is open, call [closeDrawer] to dismiss drawer as per material guidelines.
scaffoldCurrentContext.closeDrawer();
}
}
}
widget.onSelectedIndexChange?.call(index);
}
}

class _BrickLayout extends StatelessWidget {
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_adaptive_scaffold/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_adaptive_scaffold
description: Widgets to easily build adaptive layouts, including navigation elements.
version: 0.1.8
version: 0.1.9
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_adaptive_scaffold%22
repository: https://github.com/flutter/packages/tree/main/packages/flutter_adaptive_scaffold

Expand Down
113 changes: 113 additions & 0 deletions packages/flutter_adaptive_scaffold/test/adaptive_scaffold_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,119 @@ void main() {
},
);

testWidgets(
'when drawer item tap, it shall close the already open drawer',
(WidgetTester tester) async {
//region Keys
const ValueKey<String> firstDestinationIconKey = ValueKey<String>(
'first-normal-icon',
);
const ValueKey<String> firstDestinationSelectedIconKey = ValueKey<String>(
'first-selected-icon',
);
const ValueKey<String> lastDestinationIconKey = ValueKey<String>(
'last-normal-icon',
);
const ValueKey<String> lastDestinationSelectedIconKey = ValueKey<String>(
'last-selected-icon',
);
//endregion

//region Finder for destinations as per its icon.
final Finder lastDestinationWithIcon = find.byKey(
lastDestinationIconKey,
);
final Finder lastDestinationWithSelectedIcon = find.byKey(
lastDestinationSelectedIconKey,
);
//endregion

const List<NavigationDestination> destinations = <NavigationDestination>[
NavigationDestination(
icon: Icon(
Icons.inbox_outlined,
key: firstDestinationIconKey,
),
selectedIcon: Icon(
Icons.inbox,
key: firstDestinationSelectedIconKey,
),
label: 'Inbox',
),
NavigationDestination(
icon: Icon(
Icons.video_call_outlined,
key: lastDestinationIconKey,
),
selectedIcon: Icon(
Icons.video_call,
key: lastDestinationSelectedIconKey,
),
label: 'Video',
),
];
int selectedDestination = 0;

await tester.pumpWidget(
MaterialApp(
home: MediaQuery(
data: const MediaQueryData(size: Size(450, 900)),
child: StatefulBuilder(
builder: (
BuildContext context,
void Function(void Function()) setState,
) {
return AdaptiveScaffold(
destinations: destinations,
selectedIndex: selectedDestination,
smallBreakpoint: TestBreakpoint400(),
drawerBreakpoint: TestBreakpoint400(),
onSelectedIndexChange: (int value) {
setState(() {
selectedDestination = value;
});
},
);
},
),
),
),
);

expect(selectedDestination, 0);
Finder fDrawer = find.byType(Drawer);
Finder fNavigationRail = find.descendant(
of: fDrawer,
matching: find.byType(NavigationRail),
);
expect(fDrawer, findsNothing);
expect(fNavigationRail, findsNothing);

final ScaffoldState state = tester.state(find.byType(Scaffold));
state.openDrawer();
await tester.pumpAndSettle(Durations.short4);
expect(state.isDrawerOpen, isTrue);

// Need to find again as Scaffold's state has been updated
fDrawer = find.byType(Drawer);
fNavigationRail = find.descendant(
of: fDrawer,
matching: find.byType(NavigationRail),
);
expect(fDrawer, findsOneWidget);
expect(fNavigationRail, findsOneWidget);

expect(lastDestinationWithIcon, findsOneWidget);
expect(lastDestinationWithSelectedIcon, findsNothing);

await tester.ensureVisible(lastDestinationWithIcon);
await tester.tap(lastDestinationWithIcon);
await tester.pumpAndSettle(Durations.short4);
expect(selectedDestination, 1);
expect(state.isDrawerOpen, isFalse);
},
);

// This test checks whether AdaptiveScaffold.standardNavigationRail function
// creates a NavigationRail widget as expected with groupAlignment provided,
// and checks whether the NavigationRail's groupAlignment matches the expected value.
Expand Down

0 comments on commit b7fbe68

Please sign in to comment.