Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flutter_adaptive_scaffold] : 🐛 #141938 - Drawer stays open even on destination tap. #6289

Merged
merged 18 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>
Jason C.H <[email protected]>
Aliasgar Vohra <[email protected]>
8 changes: 6 additions & 2 deletions packages/flutter_adaptive_scaffold/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
## NEXT
## 0.1.9

* FIX : Drawer stays open even on destination tap - [flutter/flutter#141938](https://github.com/flutter/flutter/issues/141938)

## 0.1.8+1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just eliminate these lines and put all the changes into 0.1.9, since 0.1.8+1 will not be published.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed 0.1.8+1 and merged those change sets into 0.1.9


* 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