scroll
This commit is contained in:
parent
d8f73a3c38
commit
609317bc35
@ -1,6 +1,7 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:math' as math;
|
import 'dart:math' as math;
|
||||||
|
|
||||||
|
import 'package:flutter/gestures.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
@ -76,6 +77,39 @@ class _SwipeQuestionTileState extends State<SwipeQuestionTile>
|
|||||||
double get _snappedVerticalOffset =>
|
double get _snappedVerticalOffset =>
|
||||||
_snappedSliderValue / _sliderMax * _maxVerticalDrag;
|
_snappedSliderValue / _sliderMax * _maxVerticalDrag;
|
||||||
|
|
||||||
|
void _applyVerticalInputDelta(double dy) {
|
||||||
|
setState(() {
|
||||||
|
_verticalOffset -= dy;
|
||||||
|
_verticalOffset = _verticalOffset.clamp(
|
||||||
|
-_maxVerticalDrag,
|
||||||
|
_maxVerticalDrag,
|
||||||
|
);
|
||||||
|
if (_atZero) {
|
||||||
|
_dragOffset = 0;
|
||||||
|
}
|
||||||
|
_maybeTriggerSnapFeedback(_snappedSliderValue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One wheel notch / trackpad tick → one integer on the [-10, 10] scale.
|
||||||
|
void _stepSliderFromScroll(double scrollDy) {
|
||||||
|
if (scrollDy == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final int next = (_snappedSliderValue + (scrollDy > 0 ? -1 : 1))
|
||||||
|
.clamp(_sliderMin, _sliderMax);
|
||||||
|
if (next == _snappedSliderValue) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setState(() {
|
||||||
|
_verticalOffset = next / _sliderMax * _maxVerticalDrag;
|
||||||
|
if (_atZero) {
|
||||||
|
_dragOffset = 0;
|
||||||
|
}
|
||||||
|
_maybeTriggerSnapFeedback(next);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void _maybeTriggerSnapFeedback(int snapped) {
|
void _maybeTriggerSnapFeedback(int snapped) {
|
||||||
if (snapped == _lastSnappedValue) {
|
if (snapped == _lastSnappedValue) {
|
||||||
return;
|
return;
|
||||||
@ -216,26 +250,25 @@ class _SwipeQuestionTileState extends State<SwipeQuestionTile>
|
|||||||
bottom: 8,
|
bottom: 8,
|
||||||
left: constraints.maxWidth * 0.22,
|
left: constraints.maxWidth * 0.22,
|
||||||
right: constraints.maxWidth * 0.22,
|
right: constraints.maxWidth * 0.22,
|
||||||
|
child: Listener(
|
||||||
|
onPointerSignal: (PointerSignalEvent event) {
|
||||||
|
if (widget.busy || _acting) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event is PointerScrollEvent) {
|
||||||
|
_stepSliderFromScroll(
|
||||||
|
event.scrollDelta.dy,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
child: GestureDetector(
|
child: GestureDetector(
|
||||||
behavior: HitTestBehavior.opaque,
|
behavior: HitTestBehavior.opaque,
|
||||||
onVerticalDragUpdate: widget.busy || _acting
|
onVerticalDragUpdate: widget.busy || _acting
|
||||||
? null
|
? null
|
||||||
: (DragUpdateDetails details) {
|
: (DragUpdateDetails details) =>
|
||||||
setState(() {
|
_applyVerticalInputDelta(
|
||||||
_verticalOffset -= details.delta.dy;
|
details.delta.dy,
|
||||||
_verticalOffset =
|
),
|
||||||
_verticalOffset.clamp(
|
|
||||||
-_maxVerticalDrag,
|
|
||||||
_maxVerticalDrag,
|
|
||||||
);
|
|
||||||
if (_atZero) {
|
|
||||||
_dragOffset = 0;
|
|
||||||
}
|
|
||||||
_maybeTriggerSnapFeedback(
|
|
||||||
_snappedSliderValue,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
child: Center(
|
child: Center(
|
||||||
child: Transform.translate(
|
child: Transform.translate(
|
||||||
offset:
|
offset:
|
||||||
@ -249,6 +282,7 @@ class _SwipeQuestionTileState extends State<SwipeQuestionTile>
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user