Skip to content
Merged
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
3 changes: 3 additions & 0 deletions packages/superdeck/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
`irondash_engine_context`, so macOS and iOS builds need no CocoaPods and
Android builds avoid that plugin's 16 KB page-size crash. Highlighted output
is unchanged.
- Drop the `scrollable_positioned_list` dependency. The thumbnail panel keeps
the active slide in view with a plain `ListView` and no longer rebuilds while
it scrolls.
- **Breaking:** remove `superDeckAspectRatio`; it had no production use. Use
`superDeckSlideSize` for slide geometry.

Expand Down
113 changes: 47 additions & 66 deletions packages/superdeck/lib/src/ui/panels/thumbnail_panel.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart' show Colors;
import 'package:flutter/widgets.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';

class ThumbnailPanel extends StatefulWidget {
const ThumbnailPanel({
Expand All @@ -26,34 +23,53 @@ class ThumbnailPanel extends StatefulWidget {
}

class _ThumbnailPanelState extends State<ThumbnailPanel> {
final _duration = const Duration(milliseconds: 300);
final _pageStorageBucket = PageStorageBucket();
late final ItemScrollController _itemScrollController;
late final ItemPositionsListener _itemPositionsListener;
late List<ItemPosition> _visibleItems;
static const _padding = 20.0;
static const _duration = Duration(milliseconds: 300);
static const _curve = Curves.easeInOutCubic;

@override
void initState() {
super.initState();
_itemScrollController = ItemScrollController();
_itemPositionsListener = ItemPositionsListener.create();
_visibleItems = [];
_itemPositionsListener.itemPositions.addListener(_listener);
}
/// Where a thumbnail ahead of the view lands, as a fraction of the viewport,
/// so the thumbnails after it stay in view.
static const _forwardAlignment = 0.7;

void _listener() {
final newVisibleItems = _itemPositionsListener.itemPositions.value.toList();
final _pageStorageBucket = PageStorageBucket();
final _scrollController = ScrollController();

if (listEquals(newVisibleItems, _visibleItems)) return;
/// Scrolls just far enough to show the thumbnail at [index] in full.
///
/// Every thumbnail has the same extent, so the list's scroll extent is exact
/// and a thumbnail's offset follows from its index, even when it is not
/// built yet.
void _scrollToActiveSlide(int index) {
if (!_scrollController.hasClients || widget.itemCount == 0) return;
final position = _scrollController.position;
if (position.maxScrollExtent <= 0) return;

final viewport = position.viewportDimension;
final extent =
(position.maxScrollExtent + viewport - 2 * _padding) / widget.itemCount;
final itemStart = _padding + index * extent;
final itemEnd = itemStart + extent;
final viewStart = position.pixels;
final viewEnd = viewStart + viewport;
if (itemStart >= viewStart && itemEnd <= viewEnd) return;

final double target;
if (itemEnd <= viewStart) {
target = itemStart;
} else if (itemStart >= viewEnd) {
target = itemStart - viewport * _forwardAlignment;
} else if (itemEnd > viewEnd) {
// Cut off at the trailing edge: align its end with the view's end.
target = itemEnd - viewport;
} else {
target = itemStart;
}

// Schedule setState after the current build frame to avoid build-time calls
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted && !listEquals(newVisibleItems, _visibleItems)) {
setState(() {
_visibleItems = newVisibleItems;
});
}
});
_scrollController.animateTo(
target.clamp(position.minScrollExtent, position.maxScrollExtent),
duration: _duration,
curve: _curve,
);
}

@override
Expand All @@ -67,61 +83,26 @@ class _ThumbnailPanelState extends State<ThumbnailPanel> {
}
}

void _scrollToActiveSlide(int index) {
final visibleItems = _visibleItems;
double alignment;

if (visibleItems.isEmpty) return;

final visibleItem = visibleItems.firstWhereOrNull((e) => e.index == index);

if (visibleItem == null) {
final isBeginning = visibleItems.first.index > index;

alignment = isBeginning ? 0 : 0.7;
} else {
if (visibleItem.itemTrailingEdge > 1) {
final totalSpace =
visibleItem.itemTrailingEdge - visibleItem.itemLeadingEdge;
alignment = 1 - totalSpace;
} else if (visibleItem.itemLeadingEdge < 0) {
alignment = 0;
} else {
alignment = visibleItem.itemLeadingEdge;
}
}

_itemScrollController.scrollTo(
index: index,
alignment: alignment,
duration: _duration,
curve: _curve,
);
}

@override
void dispose() {
_itemPositionsListener.itemPositions.removeListener(_listener);
_scrollController.dispose();
super.dispose();
}

final _curve = Curves.easeInOutCubic;

@override
Widget build(BuildContext context) {
return PageStorage(
bucket: _pageStorageBucket,
child: Container(
color: Colors.black,
child: ScrollablePositionedList.builder(
child: ListView.builder(
key: PageStorageKey<String>(
'thumbnail-panel-${widget.scrollDirection.name}',
),
controller: _scrollController,
scrollDirection: widget.scrollDirection,
padding: const EdgeInsets.all(_padding),
itemCount: widget.itemCount,
itemPositionsListener: _itemPositionsListener,
itemScrollController: _itemScrollController,
padding: const EdgeInsets.all(20),
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 10),
Expand Down
1 change: 0 additions & 1 deletion packages/superdeck/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ dependencies:
collection: ^1.18.0
string_scanner: ^1.4.0
path: ^1.9.0
scrollable_positioned_list: ^0.3.8
go_router: ^17.3.0
path_provider: ^2.1.4
mix: ^2.2.0-beta.0
Expand Down
121 changes: 121 additions & 0 deletions packages/superdeck/test/src/ui/panels_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,126 @@ void main() {

semanticsHandle.dispose();
});

group('keeps the active thumbnail in view', () {
Future<void> pumpPanel(
WidgetTester tester, {
required int activeIndex,
Axis direction = Axis.vertical,
}) {
final horizontal = direction == Axis.horizontal;

return tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Align(
alignment: Alignment.topLeft,
child: SizedBox(
width: horizontal ? 400 : 200,
height: horizontal ? 200 : 400,
child: ThumbnailPanel(
itemBuilder: (index, selected) => SizedBox(
key: ValueKey<String>('thumb-$index'),
height: 100,
width: 100,
),
itemCount: 50,
activeIndex: activeIndex,
onItemTap: (_) {},
scrollDirection: direction,
),
),
),
),
),
);
}

Rect viewport(WidgetTester tester) =>
tester.getRect(find.byType(ThumbnailPanel));

bool fullyVisible(WidgetTester tester, int index) {
final item = find.byKey(ValueKey<String>('thumb-$index'));
if (item.evaluate().isEmpty) return false;
final rect = tester.getRect(item);
final view = viewport(tester);

return rect.top >= view.top - 0.5 &&
rect.bottom <= view.bottom + 0.5 &&
rect.left >= view.left - 0.5 &&
rect.right <= view.right + 0.5;
}

for (final direction in Axis.values) {
testWidgets('jumps forward to a far slide ($direction)', (
tester,
) async {
await pumpPanel(tester, activeIndex: 0, direction: direction);
await tester.pumpAndSettle();
expect(fullyVisible(tester, 40), isFalse);

await pumpPanel(tester, activeIndex: 40, direction: direction);
await tester.pumpAndSettle();

expect(fullyVisible(tester, 40), isTrue);
});

testWidgets('jumps back to an earlier slide ($direction)', (
tester,
) async {
await pumpPanel(tester, activeIndex: 0, direction: direction);
await tester.pumpAndSettle();
await pumpPanel(tester, activeIndex: 40, direction: direction);
await tester.pumpAndSettle();
expect(fullyVisible(tester, 5), isFalse);

await pumpPanel(tester, activeIndex: 5, direction: direction);
await tester.pumpAndSettle();

expect(fullyVisible(tester, 5), isTrue);
});

testWidgets(
'reveals the next slide when stepping forward ($direction)',
(tester) async {
await pumpPanel(tester, activeIndex: 0, direction: direction);
await tester.pumpAndSettle();
var index = 0;
while (fullyVisible(tester, index + 1)) {
index++;
}
expect(fullyVisible(tester, index + 1), isFalse);

await pumpPanel(
tester,
activeIndex: index + 1,
direction: direction,
);
await tester.pumpAndSettle();

expect(fullyVisible(tester, index + 1), isTrue);
},
);

testWidgets(
'does not scroll for a slide already in view ($direction)',
(tester) async {
await pumpPanel(tester, activeIndex: 0, direction: direction);
await tester.pumpAndSettle();
final before = tester.getRect(
find.byKey(const ValueKey('thumb-0')),
);

await pumpPanel(tester, activeIndex: 1, direction: direction);
await tester.pumpAndSettle();

expect(
tester.getRect(find.byKey(const ValueKey('thumb-0'))),
before,
);
},
);
}
});
});
}
8 changes: 0 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1360,14 +1360,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.2.2"
scrollable_positioned_list:
dependency: transitive
description:
name: scrollable_positioned_list
sha256: "1b54d5f1329a1e263269abc9e2543d90806131aa14fe7c6062a8054d57249287"
url: "https://pub.dev"
source: hosted
version: "0.3.8"
shelf:
dependency: transitive
description:
Expand Down
Loading