Skip to content

Add detection of drawer open and close in Scaffold widget as a callback method.#67249

Merged
fluttergithubbot merged 14 commits intoflutter:masterfrom
bikcrum:feature/drawer-callback
Dec 1, 2020
Merged

Add detection of drawer open and close in Scaffold widget as a callback method.#67249
fluttergithubbot merged 14 commits intoflutter:masterfrom
bikcrum:feature/drawer-callback

Conversation

@bikcrum
Copy link
Contributor

@bikcrum bikcrum commented Oct 4, 2020

Description

The change contains the addition of drawer callbacks in Scaffold widget. These are useful for the detection of the drawer open or closed, implemented in Scaffold. This accounts for both drawer and endDrawer having their callbacks namely drawerCallback and endDrawerCallback respectively in Scaffold constructor properly. There might be multiple reason when we want to change the state of the widget according to the state of the drawer, open or closed. It is always better to have a simple callback in Scaffold rather than letting users explicitly write a bunch of code to handle this.

Implementation: drawerCallback and endDrawerCallback are added property in the constructor which provides with the state of the drawer whether opened or closed.

  return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      drawer: NavDrawer(),
      onDrawerChanged: (isOpen) {
        // write your callback implementation here
        print('drawer callback isOpen=$isOpen');
      },
      endDrawer: NavDrawerEnd(),
      onEndDrawerChanged: (isOpen) {
        // write your callback implementation here
        print('end drawer callback isOpen=$isOpen');
      },
      body:
      ...

Related Issues

#43512
#14510

Tests

I added the following tests:

I have used the above snippet to test the callback implementation. The following test applies for both drawer and endDrawer

Use case Result
Hamburger menu is clicked Drawer opens, prints "drawer callback isOpen=true"
Drawer is closed by clicking outside drawer Drawer closes, prints "drawer callback isOpen=false"
Back button is pressed in drawer opened state Drawer closes, prints "drawer callback isOpen=false"

Checklist

Before you create this PR, confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Did any tests fail when you ran them? Please read Handling breaking changes.

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@flutter-dashboard flutter-dashboard bot added f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. labels Oct 4, 2020
@HansMuller HansMuller requested a review from chunhtai October 8, 2020 23:35
Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

I think this change looks good because we have exposed every other parameter in drawercontroller.

and the use case in #43512 seems valid. cc @Hixie

We need a test though.

@bikcrum bikcrum force-pushed the feature/drawer-callback branch from 5423b45 to ec46e40 Compare November 19, 2020 13:02
bikcrum and others added 2 commits November 20, 2020 14:42
Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com>
Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com>
@chunhtai
Copy link
Contributor

Hi @bikcrum
The doc lint is complaining

dartdoc:stdout: Generating docs for library material from package:flutter/material.dart...
dartdoc:stderr:   error: unresolved doc reference [Scaffold. endDrawer]
dartdoc:stderr:     from material.Scaffold.endDrawerCallback: (file:///b/s/w/ir/k/flutter%20sdk/packages/flutter/lib/src/material/scaffold.dart:1266:24)
dartdoc:stderr:     in documentation inherited from material.Scaffold.endDrawerCallback: (file:///b/s/w/ir/k/flutter%20sdk/packages/flutter/lib/src/material/scaffold.dart:1266:24)

and we need a test as well.

@bikcrum
Copy link
Contributor Author

bikcrum commented Nov 25, 2020

Hi @bikcrum
The doc lint is complaining

dartdoc:stdout: Generating docs for library material from package:flutter/material.dart...
dartdoc:stderr:   error: unresolved doc reference [Scaffold. endDrawer]
dartdoc:stderr:     from material.Scaffold.endDrawerCallback: (file:///b/s/w/ir/k/flutter%20sdk/packages/flutter/lib/src/material/scaffold.dart:1266:24)
dartdoc:stderr:     in documentation inherited from material.Scaffold.endDrawerCallback: (file:///b/s/w/ir/k/flutter%20sdk/packages/flutter/lib/src/material/scaffold.dart:1266:24)

and we need a test as well.

@chunhtai Thank you for suggesting what went wrong. I have made a few changes to the file and now it has passed all the tests.

@chunhtai
Copy link
Contributor

@bikcrum Hi, we need a test in order to merge this PR

@bikcrum
Copy link
Contributor Author

bikcrum commented Nov 25, 2020

@bikcrum Hi, we need a test in order to merge this PR

Yes, of course. If there is anything that is to be done from my side, please do let me know. Thank you.

@chunhtai
Copy link
Contributor

You can write a test similar to this one in the same file

testWidgets('Drawer scrolling', (WidgetTester tester) async {

The goal is to verify the callback is called when drawer opens or closes

@bikcrum bikcrum force-pushed the feature/drawer-callback branch from 486b61d to 2856cbe Compare November 26, 2020 09:23
@bikcrum
Copy link
Contributor Author

bikcrum commented Nov 26, 2020

You can write a test similar to this one in the same file

testWidgets('Drawer scrolling', (WidgetTester tester) async {

The goal is to verify the callback is called when the drawer opens or closes

I have added a test to the file. As per the automatic checks, I am not sure why 'Mac framework_tests' is failing. Can you provide me any suggestions?

@bikcrum bikcrum force-pushed the feature/drawer-callback branch from c23b8c1 to 2856cbe Compare November 27, 2020 04:11
Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

LGTM

@bikcrum
Copy link
Contributor Author

bikcrum commented Dec 1, 2020

LGTM

Thank you for approving the PR. So, are we waiting for flutter-build to be fixed before merging this PR?

@Hixie
Copy link
Contributor

Hixie commented Dec 1, 2020

I was going to put the "waiting for tree to go green" label on (which automatically lands the patch when flutter-build goes green) but actually can we make a slight change first? For consistency with our style guide, the properties should be called "on Event", as in onDrawerChanged or onEndDrawerChanged or some such.

@Hixie
Copy link
Contributor

Hixie commented Dec 1, 2020

(see https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#naming-rules-for-typedefs-and-function-variables)

@bikcrum
Copy link
Contributor Author

bikcrum commented Dec 1, 2020

I was going to put the "waiting for tree to go green" label on (which automatically lands the patch when flutter-build goes green) but actually can we make a slight change first? For consistency with our style guide, the properties should be called "on Event", as in onDrawerChanged or onEndDrawerChanged or some such.

Thank you for your suggestion. I have renamed the parameters accordingly.

@brendonanderson
Copy link

When will this change make it into the stable channel?

@bikcrum
Copy link
Contributor Author

bikcrum commented Mar 4, 2021

When will this change make it into the stable channel?

It is released now in Flutter version 2.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants