Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated example and v2 of lazy_load_scrollview #16

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
include: package:flutter_lints/flutter.yaml
stargazing-dino marked this conversation as resolved.
Show resolved Hide resolved

analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false

linter:
rules:
# - public_member_api_docs
- require_trailing_commas
- parameter_assignments
- only_throw_errors
- always_use_package_imports
- avoid_dynamic_calls
- prefer_void_to_null
- always_declare_return_types
- avoid_classes_with_only_static_members
- unnecessary_lambdas
- unnecessary_const
- avoid_void_async
2 changes: 1 addition & 1 deletion example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>8.0</string>
<string>9.0</string>
</dict>
</plist>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

197 changes: 123 additions & 74 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,74 +1,84 @@
import 'package:flutter/material.dart';
import 'package:lazy_load_scrollview/lazy_load_scrollview.dart';

void main() => runApp(new MyApp());
void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return new MaterialApp(
return const MaterialApp(
title: 'Lazy Load Demo',
home: new MyHomePage(title: 'Lazy Load Demo'),
home: MyHomePage(title: 'Lazy Load Demo'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
const MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => new _MyHomePageState();
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
final scrollController = ScrollController();

List<int> verticalData = [];
List<int> horizontalData = [];

final int increment = 10;

bool isLoadingVertical = false;
bool isLoadingHorizontal = false;

@override
void initState() {
_loadMoreVertical();
_loadMoreHorizontal();
_loadMoreHorizontalAfter();
super.initState();
}

Future _loadMoreVertical() async {
setState(() {
isLoadingVertical = true;
});
@override
void dispose() {
scrollController.dispose();
super.dispose();
}

Future _loadMoreVertical() async {
// Add in an artificial delay
await new Future.delayed(const Duration(seconds: 2));
await Future<void>.delayed(const Duration(seconds: 2));

verticalData.addAll(
List.generate(increment, (index) => verticalData.length + index));

setState(() {
isLoadingVertical = false;
});
List.generate(increment, (index) => verticalData.length + index),
);
setState(() {});
}

Future _loadMoreHorizontal() async {
setState(() {
isLoadingHorizontal = true;
});
Future _loadMoreHorizontalBefore() async {
// Add in an artificial delay
await Future<void>.delayed(const Duration(seconds: 2));

final firstIndex = horizontalData.first - 1;

final previousToAdd = List.generate(
increment,
(index) => firstIndex - index,
);

horizontalData = [...previousToAdd.reversed, ...horizontalData];

setState(() {});
}

Future _loadMoreHorizontalAfter() async {
// Add in an artificial delay
await new Future.delayed(const Duration(seconds: 2));
await Future<void>.delayed(const Duration(seconds: 2));

horizontalData.addAll(
List.generate(increment, (index) => horizontalData.length + index));

setState(() {
isLoadingHorizontal = false;
});
List.generate(increment, (index) => horizontalData.length + index),
);
setState(() {});
}

@override
Expand All @@ -78,49 +88,87 @@ class _MyHomePageState extends State<MyHomePage> {
title: Text(widget.title),
),
body: LazyLoadScrollView(
isLoading: isLoadingVertical,
onEndOfPage: () => _loadMoreVertical(),
child: Scrollbar(
child: ListView(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Nested horizontal ListView',
textAlign: TextAlign.center,
onLoadAfter: _loadMoreVertical,
controller: scrollController,
builder: (context, isLoadingAfter, _) {
// FIXME: I don't think this is true
// Scrollbar eats notifications ?? so we'll just use a scroll controller
Comment on lines +94 to +95
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate on the concern here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, this was weird. So I think even if I set the predicate to the right layer where the descendant scrollable was, I couldn't get the thing to work (without using a scrollcontroller). I actually think the Scrollbar is handling the notifications and not allowing them to bubble (by returning false in their NotificationListener onNotification). Can you confirm that it worked previously with your implementation (wrt Scrollbar specifically)?

return Scrollbar(
child: ListView(
controller: scrollController,
children: [
const Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'Nested horizontal ListView',
textAlign: TextAlign.center,
),
),
),
Container(
height: 180,
SizedBox(
height: 300.0,
child: LazyLoadScrollView(
isLoading: isLoadingHorizontal,
scrollDirection: Axis.horizontal,
onEndOfPage: () => _loadMoreHorizontal(),
child: Scrollbar(
child: ListView.builder(
itemCount: horizontalData.length,
scrollDirection: Axis.horizontal,
itemBuilder: (context, position) {
return DemoItem(position);
})))),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Vertical ListView',
textAlign: TextAlign.center,
onLoadAfter: _loadMoreHorizontalAfter,
onLoadBefore: _loadMoreHorizontalBefore,
builder: (
BuildContext context,
bool isLoadingAfter,
bool isLoadingBefore,
) {
return CustomScrollView(
scrollDirection: Axis.horizontal,
slivers: [
if (isLoadingBefore)
const SliverToBoxAdapter(
child: Center(child: CircularProgressIndicator()),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
final position = horizontalData[index];

return DemoItem(
position,
key: ValueKey(position),
);
},
childCount: horizontalData.length,
findChildIndexCallback: (key) {
final valueKey = (key as ValueKey<int>).value;

return horizontalData.indexOf(valueKey);
},
),
),
if (isLoadingAfter)
const SliverToBoxAdapter(
child: Center(child: CircularProgressIndicator()),
),
],
);
},
),
),
),
ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: verticalData.length,
itemBuilder: (context, position) {
return DemoItem(position);
},
),
],
),
),
const Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'Vertical ListView',
textAlign: TextAlign.center,
),
),
ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: verticalData.length,
itemBuilder: (context, position) {
return DemoItem(position);
},
),
if (isLoadingAfter)
const Center(child: CircularProgressIndicator()),
],
),
);
},
),
);
}
Expand Down Expand Up @@ -152,12 +200,13 @@ class DemoItem extends StatelessWidget {
height: 40.0,
width: 40.0,
),
SizedBox(width: 8.0),
Text("Item $position"),
const SizedBox(width: 8.0),
Text("Item ${position.toString()}"),
],
),
Text(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed vulputate orci. Proin id scelerisque velit. Fusce at ligula ligula. Donec fringilla sapien odio, et faucibus tortor finibus sed. Aenean rutrum ipsum in sagittis auctor. Pellentesque mattis luctus consequat. Sed eget sapien ut nibh rhoncus cursus. Donec eget nisl aliquam, ornare sapien sit amet, lacinia quam."),
const Text(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed vulputate orci. Proin id scelerisque velit. Fusce at ligula ligula. Donec fringilla sapien odio, et faucibus tortor finibus sed. Aenean rutrum ipsum in sagittis auctor. Pellentesque mattis luctus consequat. Sed eget sapien ut nibh rhoncus cursus. Donec eget nisl aliquam, ornare sapien sit amet, lacinia quam.",
),
],
),
),
Expand Down
26 changes: 20 additions & 6 deletions example/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.5.0"
version: "2.7.0"
boolean_selector:
dependency: transitive
description:
Expand All @@ -28,7 +28,7 @@ packages:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand All @@ -55,6 +55,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_lints:
dependency: "direct dev"
description:
name: flutter_lints
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
flutter_test:
dependency: "direct dev"
description: flutter
Expand All @@ -66,7 +73,14 @@ packages:
path: ".."
relative: true
source: path
version: "1.2.0"
version: "1.3.0"
lints:
dependency: transitive
description:
name: lints
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
matcher:
dependency: transitive
description:
Expand All @@ -80,7 +94,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
Expand All @@ -99,7 +113,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -134,7 +148,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.4.1"
typed_data:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^1.0.0

flutter:
uses-material-design: true
Loading