Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 3da78ee

Browse files
author
TheOneWithTheBraid
committed
feat: make SnappingSheetController listenable
- implement `Listenable` in SnappingSheetController - use `ValueNotifier` for current position Signed-off-by: TheOneWithTheBraid <[email protected]>
1 parent 2b4759c commit 3da78ee

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lib/src/snapping_sheet_widget.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/foundation.dart';
12
import 'package:flutter/widgets.dart';
23
import 'package:snapping_sheet/src/above_sheet_size_calculator.dart';
34
import 'package:snapping_sheet/src/below_sheet_size_calculator.dart';
@@ -159,7 +160,7 @@ class SnappingSheet extends StatefulWidget {
159160

160161
class _SnappingSheetState extends State<SnappingSheet>
161162
with TickerProviderStateMixin {
162-
double _currentPositionPrivate = 0;
163+
ValueNotifier<double> _currentPositionPrivate = ValueNotifier(0);
163164
BoxConstraints? _latestConstraints;
164165
late SnappingPosition _lastSnappingPosition;
165166
late AnimationController _animationController;
@@ -220,11 +221,11 @@ class _SnappingSheetState extends State<SnappingSheet>
220221
}
221222

222223
set _currentPosition(double newPosition) {
223-
_currentPositionPrivate = newPosition;
224+
_currentPositionPrivate.value = newPosition;
224225
widget.onSheetMoved?.call(_createPositionData());
225226
}
226227

227-
double get _currentPosition => _currentPositionPrivate;
228+
double get _currentPosition => _currentPositionPrivate.value;
228229

229230
SnappingPosition get _initSnappingPosition {
230231
return widget.initialSnappingPosition ?? widget.snappingPositions.first;
@@ -402,7 +403,7 @@ class _SnappingSheetState extends State<SnappingSheet>
402403
}
403404
}
404405

405-
class SnappingSheetController {
406+
class SnappingSheetController implements Listenable {
406407
_SnappingSheetState? _state;
407408

408409
/// If a state is attached to this controller. [isAttached] must be true
@@ -468,4 +469,12 @@ class SnappingSheetController {
468469
_checkAttachment();
469470
return _state!._animationController.stop();
470471
}
472+
473+
@override
474+
void addListener(VoidCallback listener) =>
475+
_state!._currentPositionPrivate.addListener(listener);
476+
477+
@override
478+
void removeListener(VoidCallback listener) =>
479+
_state!._currentPositionPrivate.removeListener(listener);
471480
}

0 commit comments

Comments
 (0)