diff --git a/example/lib/content_page.dart b/example/lib/content_page.dart index a07d1b8..b4d42d8 100644 --- a/example/lib/content_page.dart +++ b/example/lib/content_page.dart @@ -7,11 +7,11 @@ class ContentPage extends StatefulWidget { const ContentPage({ Key? key, required this.backgroundColor, - required this.stream, + required this.isOnScreen, }) : super(key: key); final Color backgroundColor; - final Stream stream; + final bool isOnScreen; @override State createState() => _ContentPageState(); @@ -19,38 +19,30 @@ class ContentPage extends StatefulWidget { class _ContentPageState extends State { final _scrollController = ScrollController(); - StreamSubscription? _subscription; - - @override - void initState() { - super.initState(); - _subscription = widget.stream.listen(_onScrollsToTop); - } @override void dispose() { _scrollController.dispose(); - _subscription?.cancel(); super.dispose(); } @override Widget build(BuildContext context) { - return PrimaryScrollController( - controller: _scrollController, - child: Scaffold( - primary: true, - body: _list(), - backgroundColor: widget.backgroundColor, - ), + return Scaffold( + primary: true, + body: _list(), + backgroundColor: widget.backgroundColor, ); } Widget _list() { - return ListView.builder( - itemBuilder: _itemBuilder, - itemCount: 100, - controller: _scrollController, + return ScrollsToTop( + onScrollsToTop: _onScrollsToTop, + child: ListView.builder( + itemBuilder: _itemBuilder, + itemCount: 100, + controller: _scrollController, + ), ); } @@ -63,6 +55,8 @@ class _ContentPageState extends State { } Future _onScrollsToTop(ScrollsToTopEvent event) async { + if (!widget.isOnScreen) return; + debugPrint('Scroll to top!'); _scrollController.animateTo( event.to, diff --git a/example/lib/home_page.dart b/example/lib/home_page.dart index 833ae4d..e2181c5 100644 --- a/example/lib/home_page.dart +++ b/example/lib/home_page.dart @@ -1,7 +1,5 @@ import 'package:example/content_page.dart'; import 'package:flutter/material.dart'; -import 'package:rxdart/rxdart.dart'; -import 'package:scrolls_to_top/scrolls_to_top.dart'; class HomePage extends StatefulWidget { const HomePage({Key? key}) : super(key: key); @@ -11,61 +9,41 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State { - final List> _streams = [ - PublishSubject(), - PublishSubject(), - PublishSubject(), - ]; int _currentIndex = 0; - @override - void dispose() { - for (var it in _streams) { - it.close(); - } - super.dispose(); - } - @override Widget build(BuildContext context) { - return ScrollsToTop( - onScrollsToTop: _onScrollsToTop, - scaffold: Scaffold( - appBar: AppBar(title: const Text('Scroll to top')), - body: IndexedStack( - index: _currentIndex, - children: [ - ContentPage( - backgroundColor: Colors.white, - stream: _streams[0], - ), - ContentPage( - backgroundColor: Colors.deepOrange, - stream: _streams[1], - ), - ContentPage( - backgroundColor: Colors.green, - stream: _streams[2], - ), - ], - ), - bottomNavigationBar: BottomNavigationBar( - onTap: _onTabTapped, - currentIndex: _currentIndex, - items: const [ - BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'), - BottomNavigationBarItem(icon: Icon(Icons.mail), label: 'Messages'), - BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile') - ], - ), + return Scaffold( + appBar: AppBar(title: const Text('Scroll to top')), + body: IndexedStack( + index: _currentIndex, + children: [ + ContentPage( + backgroundColor: Colors.white, + isOnScreen: _currentIndex == 0, + ), + ContentPage( + backgroundColor: Colors.deepOrange, + isOnScreen: _currentIndex == 1, + ), + ContentPage( + backgroundColor: Colors.green, + isOnScreen: _currentIndex == 2, + ), + ], + ), + bottomNavigationBar: BottomNavigationBar( + onTap: _onTabTapped, + currentIndex: _currentIndex, + items: const [ + BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'), + BottomNavigationBarItem(icon: Icon(Icons.mail), label: 'Messages'), + BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile') + ], ), ); } - Future _onScrollsToTop(ScrollsToTopEvent event) async { - _streams[_currentIndex].add(event); - } - void _onTabTapped(int index) { setState(() { _currentIndex = index; diff --git a/example/pubspec.lock b/example/pubspec.lock index 66dd0fa..4d93005 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -40,21 +40,14 @@ packages: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.3.0" - rxdart: - dependency: "direct main" - description: - name: rxdart - url: "https://pub.dartlang.org" - source: hosted - version: "0.27.1" + version: "1.4.0" scrolls_to_top: dependency: "direct main" description: path: ".." relative: true source: path - version: "1.0.0" + version: "1.0.1" sky_engine: dependency: transitive description: flutter diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 1902bba..462b9fa 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,5 +1,4 @@ name: example - publish_to: none version: 1.0.0+1 @@ -11,7 +10,6 @@ dependencies: sdk: flutter scrolls_to_top: path: ../ - rxdart: ^0.27.1 dev_dependencies: flutter_lints: ^1.0.3 diff --git a/example/test/widget_test.dart b/example/test/widget_test.dart deleted file mode 100644 index a19dabb..0000000 --- a/example/test/widget_test.dart +++ /dev/null @@ -1,30 +0,0 @@ -// This is a basic Flutter widget test. -// -// To perform an interaction with a widget in your test, use the WidgetTester -// utility that Flutter provides. For example, you can send tap and scroll -// gestures. You can also use WidgetTester to find child widgets in the widget -// tree, read text, and verify that the values of widget properties are correct. - -import 'package:flutter/material.dart'; -import 'package:flutter_test/flutter_test.dart'; - -import 'package:example/main.dart'; - -void main() { - testWidgets('Counter increments smoke test', (WidgetTester tester) async { - // Build our app and trigger a frame. - await tester.pumpWidget(const MyApp()); - - // Verify that our counter starts at 0. - expect(find.text('0'), findsOneWidget); - expect(find.text('1'), findsNothing); - - // Tap the '+' icon and trigger a frame. - await tester.tap(find.byIcon(Icons.add)); - await tester.pump(); - - // Verify that our counter has incremented. - expect(find.text('0'), findsNothing); - expect(find.text('1'), findsOneWidget); - }); -} diff --git a/lib/scrolls_to_top.dart b/lib/scrolls_to_top.dart index c3bced9..1797335 100644 --- a/lib/scrolls_to_top.dart +++ b/lib/scrolls_to_top.dart @@ -29,12 +29,12 @@ class ScrollsToTop extends StatefulWidget { /// Creates new ScrollsToTop widget const ScrollsToTop({ Key? key, - required this.scaffold, + required this.child, required this.onScrollsToTop, }) : super(key: key); - /// Primary scaffold of your app - final Scaffold scaffold; + /// Any child widget + final Widget child; /// Callback for handle scrolls-to-top event final ScrollsToTopCallback onScrollsToTop; @@ -66,7 +66,7 @@ class _ScrollsToTopState extends State { _attach(context); _attached = true; } - return widget.scaffold; + return widget.child; } void _attach(BuildContext context) { @@ -90,13 +90,13 @@ class _FakeScrollPositionWithSingleContext _FakeScrollPositionWithSingleContext({ required BuildContext context, required ScrollsToTopCallback callback, - }) : _onScrollsToTop = callback, + }) : _callback = callback, super( physics: const NeverScrollableScrollPhysics(), context: _FakeScrollContext(context), ); - final ScrollsToTopCallback _onScrollsToTop; + final ScrollsToTopCallback _callback; @override Future animateTo( @@ -104,7 +104,7 @@ class _FakeScrollPositionWithSingleContext required Duration duration, required Curve curve, }) { - return _onScrollsToTop( + return _callback( ScrollsToTopEvent(to, duration: duration, curve: curve), ); } diff --git a/pubspec.lock b/pubspec.lock index 25dfc82..4ff3b82 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.6.1" + version: "2.7.0" boolean_selector: dependency: transitive description: @@ -87,7 +87,7 @@ packages: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.3.0" + version: "1.4.0" path: dependency: transitive description: @@ -141,7 +141,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.3.0" + version: "0.4.0" typed_data: dependency: transitive description: