From 9a2bf0e70f687627bc1f06e1080947fa62b59155 Mon Sep 17 00:00:00 2001 From: IsmailAbdirahman <56909567+IsmailAbdirahman@users.noreply.github.com> Date: Tue, 17 Nov 2020 00:00:41 +0530 Subject: [PATCH] AdoptAWidget: NotificationListener (#69524) * Added notificationListner sample code * Fixed spacing * fixed spacing * Moved the variables into the Build method * fixed typos * Update packages/flutter/lib/src/widgets/notification_listener.dart Co-authored-by: John Ryan * Update packages/flutter/lib/src/widgets/notification_listener.dart Co-authored-by: John Ryan * Update packages/flutter/lib/src/widgets/notification_listener.dart Co-authored-by: John Ryan * Update packages/flutter/lib/src/widgets/notification_listener.dart Co-authored-by: John Ryan * Update packages/flutter/lib/src/widgets/notification_listener.dart Co-authored-by: John Ryan * Update packages/flutter/lib/src/widgets/notification_listener.dart Co-authored-by: John Ryan * Update packages/flutter/lib/src/widgets/notification_listener.dart Co-authored-by: John Ryan --- .../src/widgets/notification_listener.dart | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/packages/flutter/lib/src/widgets/notification_listener.dart b/packages/flutter/lib/src/widgets/notification_listener.dart index f15cde2a96..c3eed7115b 100644 --- a/packages/flutter/lib/src/widgets/notification_listener.dart +++ b/packages/flutter/lib/src/widgets/notification_listener.dart @@ -11,9 +11,84 @@ import 'framework.dart'; /// Return true to cancel the notification bubbling. Return false to allow the /// notification to continue to be dispatched to further ancestors. /// +/// [NotificationListener] is useful when listening scroll events +/// in [ListView],[NestedScrollView],[GridView] or any Scrolling widgets. /// Used by [NotificationListener.onNotification]. + typedef NotificationListenerCallback = bool Function(T notification); +/// {@tool dartpad --template=stateless_widget_material} +/// +/// This example shows a [NotificationListener] widget +/// that listens for [ScrollNotification] notifications. When a scroll +/// event occurs in the [NestedScrollView], +/// this widget is notified. The events could be either a +/// [ScrollStartNotification]or[ScrollEndNotification]. +/// +/// ```dart +/// Widget build(BuildContext context) { +/// const List _tabs = ["Months", "Days"]; +/// const List _months = [ "January","February","March", ]; +/// const List _days = [ "Sunday", "Monday","Tuesday", ]; +/// return DefaultTabController( +/// length: _tabs.length, +/// child: Scaffold( +/// // Listens to the scroll events and returns the current position. +/// body: NotificationListener( +/// onNotification: (scrollNotification) { +/// if (scrollNotification is ScrollStartNotification) { +/// print('Scrolling has started'); +/// } else if (scrollNotification is ScrollEndNotification) { +/// print("Scrolling has ended"); +/// } +/// // Return true to cancel the notification bubbling. +/// return true; +/// }, +/// child: NestedScrollView( +/// headerSliverBuilder: +/// (BuildContext context, bool innerBoxIsScrolled) { +/// return [ +/// SliverAppBar( +/// title: const Text("Flutter Code Sample"), +/// pinned: true, +/// floating: true, +/// bottom: TabBar( +/// tabs: _tabs.map((name) => Tab(text: name)).toList(), +/// ), +/// ), +/// ]; +/// }, +/// body: TabBarView( +/// children: [ +/// ListView.builder( +/// itemCount: _months.length, +/// itemBuilder: (context, index) { +/// return ListTile(title: Text(_months[index])); +/// }, +/// ), +/// ListView.builder( +/// itemCount: _days.length, +/// itemBuilder: (context, index) { +/// return ListTile(title: Text(_days[index])); +/// }, +/// ), +/// ], +/// ), +/// ), +/// ), +/// ), +/// ); +/// } +/// ``` +/// {@end-tool} +/// +/// See also: +/// +/// * [ScrollNotification] which describes the notification lifecycle. +/// * [ScrollStartNotification] which returns the start position of scrolling. +/// * [ScrollEndNotification] which returns the end position of scrolling. +/// * [NestedScrollView] which creates a nested scroll view. +/// /// A notification that can bubble up the widget tree. /// /// You can determine the type of a notification using the `is` operator to