From b12c86f6a2dd5a8efe6c718d3fe663d1a7c7e148 Mon Sep 17 00:00:00 2001 From: Jack Li Date: Fri, 11 Mar 2022 16:14:31 +0800 Subject: [PATCH 1/4] Expose InViewNotifier to support customized widget --- example/lib/main.dart | 4 ++ example/lib/refresh_list.dart | 105 ++++++++++++++++++++++++++++++++++ example/pubspec.yaml | 1 + lib/inview_notifier_list.dart | 2 +- lib/src/inview_notifier.dart | 5 +- 5 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 example/lib/refresh_list.dart diff --git a/example/lib/main.dart b/example/lib/main.dart index 192a021..396aae9 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,4 +1,5 @@ import 'package:example/csv_example.dart'; +import 'package:example/refresh_list.dart'; import 'package:flutter/material.dart'; import 'my_list.dart'; @@ -27,6 +28,7 @@ class _HomePageState extends State { final List myTabs = [ Tab(text: 'Example 1'), Tab(text: 'Example 2'), + Tab(text: 'Refresh List'), Tab(text: 'Autoplay Video'), Tab(text: 'Custom Scroll View'), ]; @@ -41,6 +43,7 @@ class _HomePageState extends State { title: Text('ÍnViewNotifierList'), centerTitle: true, bottom: TabBar( + isScrollable: true, tabs: myTabs, ), ), @@ -66,6 +69,7 @@ class _HomePageState extends State { color: Colors.redAccent.withOpacity(0.2), ), ), + RefreshList(), VideoList(), CSVExample(), ], diff --git a/example/lib/refresh_list.dart b/example/lib/refresh_list.dart new file mode 100644 index 0000000..3c43606 --- /dev/null +++ b/example/lib/refresh_list.dart @@ -0,0 +1,105 @@ +import 'dart:math'; + +import 'package:flutter/material.dart'; +import 'package:inview_notifier_list/inview_notifier_list.dart'; +import 'package:pull_to_refresh/pull_to_refresh.dart'; + +class RefreshList extends StatefulWidget { + const RefreshList({Key? key}) : super(key: key); + + @override + State createState() => _RefreshListState(); +} + +class _RefreshListState extends State { + final _refreshController = RefreshController(initialRefresh: true); + + final data = []; + + int get nextInt => Random().nextInt(1000); + + @override + void initState() { + super.initState(); + } + + void onLoading() { + Future.delayed(const Duration(milliseconds: 500), () { + data.addAll(List.generate(10, (i) => nextInt)); + _refreshController.loadComplete(); + setState(() {}); + }); + } + + void onRefresh() { + Future.delayed(const Duration(milliseconds: 500), () { + data.clear(); + data.addAll(List.generate(10, (_) => nextInt)); + _refreshController.refreshCompleted(); + setState(() {}); + }); + } + + bool _conditaion(double deltaTop, double deltaBottom, double vpHeight) { + // return deltaTop < (0.5 * vpHeight) && deltaBottom > (0.5 * vpHeight); + return (deltaTop < (0.5 * vpHeight) + 100.0 && + deltaBottom > (0.5 * vpHeight) - 100.0); + } + + @override + Widget build(BuildContext context) { + return Stack( + children: [ + InViewNotifier( + isInViewPortCondition: _conditaion, + child: SmartRefresher( + controller: _refreshController, + enablePullDown: true, + enablePullUp: true, + onLoading: onLoading, + onRefresh: onRefresh, + child: ListView.builder( + itemCount: data.length, + itemBuilder: (_, i) { + final item = data[i]; + return InViewNotifierWidget( + id: '$i', + builder: (_, inView, child) { + if (!inView) { + return child!; + } + return Container( + padding: const EdgeInsets.all(8), + child: child, + ); + }, + child: Container( + alignment: Alignment.center, + child: Text( + '$item', + style: TextStyle( + fontSize: 32, + ), + ), + height: 100, + color: i.isOdd ? Colors.green : Colors.blue, + ), + ); + }, + ), + ), + ), + IgnorePointer( + ignoring: true, + child: Align( + alignment: Alignment.center, + child: Container( + color: Colors.red.withOpacity(0.2), + height: 200, + ), + ), + ), + ], + ); + } +} diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 1fd3394..d305fa4 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -24,6 +24,7 @@ dependencies: inview_notifier_list: path: ../ video_player: ^2.2.10 + pull_to_refresh: ^2.0.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. diff --git a/lib/inview_notifier_list.dart b/lib/inview_notifier_list.dart index a35a567..4135325 100644 --- a/lib/inview_notifier_list.dart +++ b/lib/inview_notifier_list.dart @@ -1,3 +1,3 @@ export 'src/inview_notifier_list.dart'; export 'src/inview_state.dart'; -export 'src/inview_notifier.dart' hide InViewNotifier; +export 'src/inview_notifier.dart'; diff --git a/lib/src/inview_notifier.dart b/lib/src/inview_notifier.dart index 34f6e91..bb7d32e 100644 --- a/lib/src/inview_notifier.dart +++ b/lib/src/inview_notifier.dart @@ -14,7 +14,7 @@ class InViewNotifier extends StatefulWidget { final List initialInViewIds; ///The widget that should be displayed in the [InViewNotifier]. - final ScrollView child; + final Widget child; ///The distance from the bottom of the list where the [onListEndReached] should be invoked. final double endNotificationOffset; @@ -41,9 +41,10 @@ class InViewNotifier extends StatefulWidget { this.endNotificationOffset = 0.0, this.onListEndReached, this.throttleDuration = const Duration(milliseconds: 200), + this.scrollDirection = Axis.vertical, required this.isInViewPortCondition, }) : assert(endNotificationOffset >= 0.0), - scrollDirection = child.scrollDirection, + // scrollDirection = child.scrollDirection, super(key: key); @override From d264b51d26eb3e92af04a075703878c2de8ade76 Mon Sep 17 00:00:00 2001 From: Jack Li Date: Fri, 11 Mar 2022 16:51:20 +0800 Subject: [PATCH 2/4] add missing scrollDirection parameter --- CHANGELOG.md | 9 +++++++++ lib/src/inview_notifier.dart | 8 +++++++- lib/src/inview_notifier_list.dart | 19 +++---------------- origin) | 0 4 files changed, 19 insertions(+), 17 deletions(-) delete mode 100644 origin) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf92a75..b3dee38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## [3.1.0] - 28th December 2021. + +- Expose `InViewNotifier` to support customized `Widget` (e.g. `SmartRefresher`) +- Add `InViewNotifier.of` to read `InViewState` + +**Breaking Changes** + +- Remove `InViewNotifierList.of` and `InViewNotifierCustomScrollView.of` + ## [3.0.0] - 28th December 2021. **Breaking Changes** diff --git a/lib/src/inview_notifier.dart b/lib/src/inview_notifier.dart index bb7d32e..529d291 100644 --- a/lib/src/inview_notifier.dart +++ b/lib/src/inview_notifier.dart @@ -44,11 +44,17 @@ class InViewNotifier extends StatefulWidget { this.scrollDirection = Axis.vertical, required this.isInViewPortCondition, }) : assert(endNotificationOffset >= 0.0), - // scrollDirection = child.scrollDirection, super(key: key); @override _InViewNotifierState createState() => _InViewNotifierState(); + + static InViewState? of(BuildContext context) { + final widget = context + .getElementForInheritedWidgetOfExactType()! + .widget as InheritedInViewWidget; + return widget.inViewState; + } } class _InViewNotifierState extends State { diff --git a/lib/src/inview_notifier_list.dart b/lib/src/inview_notifier_list.dart index a952f66..863a591 100644 --- a/lib/src/inview_notifier_list.dart +++ b/lib/src/inview_notifier_list.dart @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import 'package:inview_notifier_list/src/inview_notifier.dart'; -import 'inherited_inview_widget.dart'; import 'inview_state.dart'; ///builds a [ListView] and notifies when the widgets are on screen within a provided area. @@ -34,6 +33,7 @@ class InViewNotifierList extends InViewNotifier { onListEndReached: onListEndReached, throttleDuration: throttleDuration, isInViewPortCondition: isInViewPortCondition, + scrollDirection: scrollDirection, child: ListView.builder( padding: padding, controller: controller, @@ -47,13 +47,6 @@ class InViewNotifierList extends InViewNotifier { itemBuilder: builder, ), ); - - static InViewState? of(BuildContext context) { - final InheritedInViewWidget widget = context - .getElementForInheritedWidgetOfExactType()! - .widget as InheritedInViewWidget; - return widget.inViewState; - } } ///builds a [CustomScrollView] and notifies when the widgets are on screen within a provided area. @@ -87,6 +80,7 @@ class InViewNotifierCustomScrollView extends InViewNotifier { onListEndReached: onListEndReached, throttleDuration: throttleDuration, isInViewPortCondition: isInViewPortCondition, + scrollDirection: scrollDirection, child: CustomScrollView( slivers: slivers, anchor: anchor, @@ -99,13 +93,6 @@ class InViewNotifierCustomScrollView extends InViewNotifier { center: center, ), ); - - static InViewState? of(BuildContext context) { - final InheritedInViewWidget widget = context - .getElementForInheritedWidgetOfExactType()! - .widget as InheritedInViewWidget; - return widget.inViewState; - } } ///The widget that gets notified if it is currently inside the viewport condition @@ -157,7 +144,7 @@ class _InViewNotifierWidgetState extends State { @override void initState() { super.initState(); - state = InViewNotifierList.of(context)!; + state = InViewNotifier.of(context)!; state.addContext(context: context, id: widget.id); } diff --git a/origin) b/origin) deleted file mode 100644 index e69de29..0000000 From 867ff31ae36bdd2f1f5b4dfc3609255397fa8e27 Mon Sep 17 00:00:00 2001 From: Jack Li Date: Fri, 11 Mar 2022 16:51:52 +0800 Subject: [PATCH 3/4] bump version to 3.1.0 --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 184eb27..8228ef7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: inview_notifier_list description: A Flutter package that builds a listview and notifies when the widgets are on screen. -version: 3.0.0 +version: 3.1.0 # author: Vamsi Krishna homepage: https://github.com/rvamsikrishna/inview_notifier_list From c6c1b08ca14c9d5b028568cf590503d760964741 Mon Sep 17 00:00:00 2001 From: Jack Li Date: Fri, 11 Mar 2022 16:52:44 +0800 Subject: [PATCH 4/4] fix timeline --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3dee38..793c17b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## [3.1.0] - 28th December 2021. +## [3.1.0] - 11th March 2022. - Expose `InViewNotifier` to support customized `Widget` (e.g. `SmartRefresher`) - Add `InViewNotifier.of` to read `InViewState`