flutter/examples/api
Hans Muller 92f8455ff8
Scrollbar thumb drag gestures now produce one start and one end scroll notification (#146654)
The scroll notification events reported for a press-drag-release gesture within a scrollable on a touch screen device begin with a `ScrollStartNotification`, followed by a series of `ScrollUpdateNotifications`, and conclude with a `ScrollEndNotification`. This protocol can be used to defer work until an interactive scroll gesture ends. For example, you might defer updating a scrollable's contents via network requests until the scroll has ended, or you might want to automatically auto-scroll at that time. 

In the example that follows the CustomScrollView automatically scrolls so that the last partially visible fixed-height item is completely visible when the scroll gesture ends. Many iOS applications do this kind of thing. It only makes sense to auto-scroll when the user isn't actively dragging the scrollable around.

It's easy enough to do this by reacting to a ScrollEndNotifcation by auto-scrolling to align the last fixed-height list item ([source code](https://gist.github.com/HansMuller/13e2a7adadc9afb3803ba7848b20c410)).

https://github.com/flutter/flutter/assets/1377460/a6e6fc77-6742-4f98-81ba-446536535f73

Dragging the scrollbar thumb in a desktop application is a similar user gesture. Currently it's not possible to defer work or auto-scroll (or whatever) while the scrollable is actively being dragged via the scrollbar thumb because each scrollbar thumb motion is mapped to a scroll start - scroll update - scroll end series of notifications.  On a desktop platform, the same code behaves quite differently when the scrollbar thumb is dragged.

https://github.com/flutter/flutter/assets/1377460/2593d8a3-639c-407f-80c1-6e6f67fb8c5f

The stream of scroll-end events triggers auto-scrolling every time the thumb moves. From the user's perspective this feels like a losing struggle.

One can also detect the beginning and end of a touch-drag by listening to the value of a ScrollPosition's `isScrollingNotifier`. This approach suffers from a similar problem: during a scrollbar thumb-drag, the `isScrollingNotifier` value isn't updated at all.

This PR refactors the RawScrollbar implementation to effectively use a ScrollDragController to manage scrolls caused by dragging the scrollbar's thumb. Doing so means that dragging the thumb will produce the same notifications as dragging the scrollable on a touch device.

Now desktop applications can choose to respond to scrollbar thumb drags in the same that they respond to drag scrolling on a touch screen. With the changes included here, the desktop or web version of the app works as expected, whether you're listing to scroll notifications or the scroll position's `isScrollingNotifier`.

https://github.com/flutter/flutter/assets/1377460/67435c40-a866-4735-a19b-e3d68eac8139

This PR also makes the second [ScrollPosition API doc example](https://api.flutter.dev/flutter/widgets/ScrollPosition-class.html#cupertino.ScrollPosition.2) work as expected when used with the DartPad that's  part of API doc page.

Desktop applications also see scroll start-update-end notifications due to the mouse wheel.  There is no touch screen analog for the mouse wheel, so an application that wanted to enable this kind of auto-scrolling alignment would have to include a heuristic that dealt with the sequence of small scrolls triggered by the mouse wheel. Here's an example of that: [source code](https://gist.github.com/HansMuller/ce5c474a458f5f4bcc07b0d621843165). This version of the app does not auto-align in response to small changes, wether they're triggered by dragging the scrollbar thumb of the mouse wheel.

Related sliver utility PRs: https://github.com/flutter/flutter/pull/143538,  https://github.com/flutter/flutter/pull/143196, https://github.com/flutter/flutter/pull/143325.
2024-06-04 22:02:12 +00:00
..
lib Scrollbar thumb drag gestures now produce one start and one end scroll notification (#146654) 2024-06-04 22:02:12 +00:00
linux Deletes files that should be ignored (#127984) 2023-06-29 19:45:22 +00:00
macos [macOS] Migrate @NSApplicationMain attribute to @main (#146848) 2024-04-18 03:08:36 +00:00
test Scrollbar thumb drag gestures now produce one start and one end scroll notification (#146654) 2024-06-04 22:02:12 +00:00
test_driver Add smoke tests for all the examples, fix 17 broken examples. (#89021) 2021-09-28 09:32:06 -07:00
web Fix example app names and copyrights (#100795) 2022-03-25 17:40:11 -07:00
windows [Windows] Drop support for Windows 7/8 apps in template (#146668) 2024-04-12 01:07:20 +00:00
.gitignore Update examples/api for android platform (#147102) 2024-04-22 20:14:13 +00:00
.metadata Fix example app names and copyrights (#100795) 2022-03-25 17:40:11 -07:00
analysis_options.yaml Enable unreachable_from_main lint - it is stable now!!1 (#129854) 2023-07-06 00:09:01 +00:00
pubspec.yaml Roll pub packages (#149617) 2024-06-03 22:15:41 +00:00
README.md Add example examples (#111276) 2022-09-12 20:28:09 +00:00

API Example Code

This directory contains the API sample code that is referenced from the API documentation in the framework.

The examples can be run individually by just specifying the path to the example on the command line (or in the run configuration of an IDE).

For example (no pun intended!), to run the first example from the Curve2D class in Chrome, you would run it like so from the api directory:

% flutter run -d chrome lib/animation/curves/curve2_d.0.dart

All of these same examples are available on the API docs site. For instance, the example above is available on this page. Most of the samples are available as interactive examples in Dartpad, but some (the ones marked with {@tool sample} in the framework source code), just don't make sense on the web, and so are available as standalone examples that can be run here. For instance, setting the system overlay style doesn't make sense on the web (it only changes the notification area background color on Android), so you can run the example for that on an Android device like so:

% flutter run -d MyAndroidDevice lib/services/system_chrome/system_chrome.set_system_u_i_overlay_style.1.dart

Naming

lib/library/file/class_name.n.dart

lib/library/file/class_name.member_name.n.dart

The naming scheme for the files is similar to the hierarchy under packages/flutter/lib/src, except that the files are represented as directories (without the .dart suffix), and each sample in the file is a separate file in that directory. So, for the example above, where the examples are from the packages/flutter/lib/src/animation/curves.dart file, the Curve2D class, the first sample (hence the index "0") for that symbol resides in the file named lib/animation/curves/curve2_d.0.dart.

Symbol names are converted from "CamelCase" to "snake_case". Dots are left between symbol names, so the first example for symbol InputDecoration.prefixIconConstraints would be converted to input_decoration.prefix_icon_constraints.0.dart.

If the same example is linked to from multiple symbols, the source will be in the canonical location for one of the symbols, and the link in the API docs block for the other symbols will point to the first symbol's example location.

Authoring

For more detailed information about authoring examples, see the snippets package.

When authoring examples, first place a block in the Dartdoc documentation for the symbol you would like to attach it to. Here's what it might look like if you wanted to add a new example to the Curve2D class:

/// {@tool dartpad}
/// Write a description of the example here. This description will appear in the
/// API web documentation to introduce the example.
///
/// ** See code in examples/api/lib/animation/curves/curve2_d.0.dart **
/// {@end-tool}

The "See code in" line needs to be formatted exactly as above, with no wrapping or newlines, one space after the "**" at the beginning, and one space before the "**" at the end, and the words "See code in" at the beginning of the line. This is what the snippets tool use when finding the example source code that you are creating.

Use {@tool dartpad} for Dartpad examples, and use {@tool sample} for examples that shouldn't be run/shown in Dartpad.

Once that comment block is inserted in the source code, create a new file at the appropriate path under examples/api. See the sample_templates directory for examples of different types of samples with some best practices applied.

The filename should match the location of the source file it is linked from, and is named for the symbol it is attached to, in lower_snake_case, with an index relating to their order within the doc comment. So, for the Curve2D example above, since it's in the animation library, in a file called curves.dart, and it's the first example, it should have the name examples/api/lib/animation/curves/curve2_d.0.dart.

You should also add tests for your sample code under examples/api/test, that matches their location under lib, ending in _test.dart. See the section on writing tests for more information on what kinds of tests to write.

The entire example should be in a single file, so that Dartpad can load it.

Only packages that can be loaded by Dartpad may be imported. If you use one that hasn't been used in an example before, you may have to add it to the pubspec.yaml in the api directory.

Snippets

There is another type of example that can also be authored, using {@tool snippet}. Snippet examples are just written inline in the source, like so:

/// {@tool dartpad}
/// Write a description of the example here. This description will appear in the
/// API web documentation to introduce the example.
///
/// ```dart
/// // Sample code goes here, e.g.:
/// const Widget emptyBox = SizedBox();
/// ```
/// {@end-tool}

The source for these snippets isn't stored under the examples/api directory, or available in Dartpad in the API docs, since they're not intended to be runnable, they just show some incomplete snippet of example code. It must compile (in the context of the sample analyzer), but doesn't need to do anything. See the snippets documentation for more information about the context that the analyzer uses.

Writing Tests

Examples are required to have tests. There is already a "smoke test" that simply builds and runs all the API examples, just to make sure that they start up without crashing. Functionality tests are required the examples, and generally just do what is normally done for writing tests. The one thing that makes it more challenging to do for examples is that they can't really be written for testability in any obvious way, since that would complicate the examples and make them harder to explain.

As an example, in regular framework code, you might include a parameter for a Platform object that can be overridden by a test to supply a dummy platform, but in the example. This would be unnecessarily complex for the example. In all other ways, these are just normal tests. You don't need to re-test the functionality of the widget being used in the example, but you should test the functionality and integrity of the example itself.

Tests go into a directory under test that matches their location under lib. They are named the same as the example they are testing, with _test.dart at the end, like other tests. For instance, a LayoutBuilder example that resides in lib/widgets/layout_builder/layout_builder.0.dart would have its tests in a file named test/widgets/layout_builder/layout_builder.0_test.dart