Skip to content

Commit

Permalink
Simplify example
Browse files Browse the repository at this point in the history
  • Loading branch information
otopba committed Jun 30, 2021
1 parent 566d932 commit d7361d5
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 121 deletions.
36 changes: 15 additions & 21 deletions example/lib/content_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,42 @@ 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<ScrollsToTopEvent> stream;
final bool isOnScreen;

@override
State<ContentPage> createState() => _ContentPageState();
}

class _ContentPageState extends State<ContentPage> {
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,
),
);
}

Expand All @@ -63,6 +55,8 @@ class _ContentPageState extends State<ContentPage> {
}

Future<void> _onScrollsToTop(ScrollsToTopEvent event) async {
if (!widget.isOnScreen) return;

debugPrint('Scroll to top!');
_scrollController.animateTo(
event.to,
Expand Down
76 changes: 27 additions & 49 deletions example/lib/home_page.dart
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -11,61 +9,41 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {
final List<PublishSubject<ScrollsToTopEvent>> _streams = [
PublishSubject<ScrollsToTopEvent>(),
PublishSubject<ScrollsToTopEvent>(),
PublishSubject<ScrollsToTopEvent>(),
];
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<void> _onScrollsToTop(ScrollsToTopEvent event) async {
_streams[_currentIndex].add(event);
}

void _onTabTapped(int index) {
setState(() {
_currentIndex = index;
Expand Down
11 changes: 2 additions & 9 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name: example

publish_to: none
version: 1.0.0+1

Expand All @@ -11,7 +10,6 @@ dependencies:
sdk: flutter
scrolls_to_top:
path: ../
rxdart: ^0.27.1

dev_dependencies:
flutter_lints: ^1.0.3
Expand Down
30 changes: 0 additions & 30 deletions example/test/widget_test.dart

This file was deleted.

14 changes: 7 additions & 7 deletions lib/scrolls_to_top.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,7 +66,7 @@ class _ScrollsToTopState extends State<ScrollsToTop> {
_attach(context);
_attached = true;
}
return widget.scaffold;
return widget.child;
}

void _attach(BuildContext context) {
Expand All @@ -90,21 +90,21 @@ 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<void> animateTo(
double to, {
required Duration duration,
required Curve curve,
}) {
return _onScrollsToTop(
return _callback(
ScrollsToTopEvent(to, duration: duration, curve: curve),
);
}
Expand Down
6 changes: 3 additions & 3 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit d7361d5

Please sign in to comment.