Skip to content

Commit

Permalink
fixing too quick selection of a game when spinning and allowing for m…
Browse files Browse the repository at this point in the history
…ultiple spins (#157)
  • Loading branch information
mkieres authored Dec 21, 2022
1 parent 66afa3b commit ca21df6
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions board_games_companion/lib/pages/plays/plays_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:board_games_companion/common/enums/collection_type.dart';
import 'package:board_games_companion/common/enums/plays_tab.dart';
import 'package:board_games_companion/extensions/int_extensions.dart';
Expand Down Expand Up @@ -259,7 +261,7 @@ class _GameSpinnerFilters extends StatelessWidget {
);
}

class _GameSpinnerSliver extends StatelessWidget {
class _GameSpinnerSliver extends StatefulWidget {
const _GameSpinnerSliver({
Key? key,
required this.scrollController,
Expand All @@ -276,6 +278,21 @@ class _GameSpinnerSliver extends StatelessWidget {
final VoidCallback onSpin;
final VoidCallback onGameSelected;

@override
State<_GameSpinnerSliver> createState() => _GameSpinnerSliverState();
}

class _GameSpinnerSliverState extends State<_GameSpinnerSliver> {
static const int _debounceSpinnerThresholdInMilliseconds = 300;

Timer? _debounce;

@override
void dispose() {
_debounce?.cancel();
super.dispose();
}

@override
Widget build(BuildContext context) {
return SliverToBoxAdapter(
Expand All @@ -288,20 +305,29 @@ class _GameSpinnerSliver extends StatelessWidget {
Expanded(
child: NotificationListener<ScrollNotification>(
onNotification: (scrollNotification) {
// Fimber.d(scrollNotification.runtimeType.toString());
if (scrollNotification is ScrollStartNotification &&
(_debounce?.isActive ?? false)) {
_debounce!.cancel();
}

if (scrollNotification is ScrollEndNotification) {
onGameSelected();
_debounce = Timer(
const Duration(milliseconds: _debounceSpinnerThresholdInMilliseconds),
() => widget.onGameSelected(),
);
}

return false;
},
child: ListWheelScrollView.useDelegate(
controller: scrollController,
itemExtent: _gameSpinnerItemHeight,
squeeze: _gameSpinnerItemSqueeze,
controller: widget.scrollController,
itemExtent: _GameSpinnerSliver._gameSpinnerItemHeight,
squeeze: _GameSpinnerSliver._gameSpinnerItemSqueeze,
perspective: 0.0035,
childDelegate: ListWheelChildLoopingListDelegate(
children: [
for (final boardGame in shuffledBoardGames) ...[
for (final boardGame in widget.shuffledBoardGames) ...[
Center(
child: Container(
constraints: const BoxConstraints(
Expand Down Expand Up @@ -330,7 +356,7 @@ class _GameSpinnerSliver extends StatelessWidget {
Material(
color: Colors.transparent,
child: InkWell(
onTap: () => onSpin(),
onTap: () => widget.onSpin(),
child: Padding(
padding: const EdgeInsets.all(Dimensions.standardSpacing),
child: Column(
Expand Down

0 comments on commit ca21df6

Please sign in to comment.