s/extent/size/ (#91775)
This commit is contained in:
parent
3ece170c40
commit
ab5dfe1d2e
@ -282,111 +282,111 @@ class DraggableScrollableNotification extends Notification with ViewportNotifica
|
|||||||
///
|
///
|
||||||
/// The ScrollPosition knows the number of pixels a user wants to move the sheet.
|
/// The ScrollPosition knows the number of pixels a user wants to move the sheet.
|
||||||
///
|
///
|
||||||
/// The [currentExtent] will never be null.
|
/// The [currentSize] will never be null.
|
||||||
/// The [availablePixels] will never be null, but may be `double.infinity`.
|
/// The [availablePixels] will never be null, but may be `double.infinity`.
|
||||||
class _DraggableSheetExtent {
|
class _DraggableSheetExtent {
|
||||||
_DraggableSheetExtent({
|
_DraggableSheetExtent({
|
||||||
required this.minExtent,
|
required this.minSize,
|
||||||
required this.maxExtent,
|
required this.maxSize,
|
||||||
required this.snap,
|
required this.snap,
|
||||||
required this.snapSizes,
|
required this.snapSizes,
|
||||||
required this.initialExtent,
|
required this.initialSize,
|
||||||
required this.onExtentChanged,
|
required this.onSizeChanged,
|
||||||
ValueNotifier<double>? currentExtent,
|
ValueNotifier<double>? currentSize,
|
||||||
bool? hasChanged,
|
bool? hasChanged,
|
||||||
}) : assert(minExtent != null),
|
}) : assert(minSize != null),
|
||||||
assert(maxExtent != null),
|
assert(maxSize != null),
|
||||||
assert(initialExtent != null),
|
assert(initialSize != null),
|
||||||
assert(minExtent >= 0),
|
assert(minSize >= 0),
|
||||||
assert(maxExtent <= 1),
|
assert(maxSize <= 1),
|
||||||
assert(minExtent <= initialExtent),
|
assert(minSize <= initialSize),
|
||||||
assert(initialExtent <= maxExtent),
|
assert(initialSize <= maxSize),
|
||||||
_currentExtent = (currentExtent ?? ValueNotifier<double>(initialExtent))
|
_currentSize = (currentSize ?? ValueNotifier<double>(initialSize))
|
||||||
..addListener(onExtentChanged),
|
..addListener(onSizeChanged),
|
||||||
availablePixels = double.infinity,
|
availablePixels = double.infinity,
|
||||||
hasChanged = hasChanged ?? false;
|
hasChanged = hasChanged ?? false;
|
||||||
|
|
||||||
final double minExtent;
|
final double minSize;
|
||||||
final double maxExtent;
|
final double maxSize;
|
||||||
final bool snap;
|
final bool snap;
|
||||||
final List<double> snapSizes;
|
final List<double> snapSizes;
|
||||||
final double initialExtent;
|
final double initialSize;
|
||||||
final ValueNotifier<double> _currentExtent;
|
final ValueNotifier<double> _currentSize;
|
||||||
final VoidCallback onExtentChanged;
|
final VoidCallback onSizeChanged;
|
||||||
double availablePixels;
|
double availablePixels;
|
||||||
|
|
||||||
// Used to disable snapping until the extent has changed. We do this because
|
// Used to disable snapping until the user interacts with the sheet. We set
|
||||||
// we don't want to snap away from the initial extent.
|
// this to false on initialization and after programmatic interaction with the
|
||||||
|
// sheet to prevent snapping away from the initial size and after animateTo/jumpTo.
|
||||||
bool hasChanged;
|
bool hasChanged;
|
||||||
|
|
||||||
bool get isAtMin => minExtent >= _currentExtent.value;
|
bool get isAtMin => minSize >= _currentSize.value;
|
||||||
bool get isAtMax => maxExtent <= _currentExtent.value;
|
bool get isAtMax => maxSize <= _currentSize.value;
|
||||||
|
|
||||||
set currentExtent(double value) {
|
set currentSize(double value) {
|
||||||
assert(value != null);
|
assert(value != null);
|
||||||
hasChanged = true;
|
hasChanged = true;
|
||||||
_currentExtent.value = value.clamp(minExtent, maxExtent);
|
_currentSize.value = value.clamp(minSize, maxSize);
|
||||||
}
|
}
|
||||||
double get currentExtent => _currentExtent.value;
|
double get currentSize => _currentSize.value;
|
||||||
double get currentPixels => extentToPixels(_currentExtent.value);
|
double get currentPixels => sizeToPixels(_currentSize.value);
|
||||||
|
|
||||||
double get additionalMinExtent => isAtMin ? 0.0 : 1.0;
|
double get additionalMinSize => isAtMin ? 0.0 : 1.0;
|
||||||
double get additionalMaxExtent => isAtMax ? 0.0 : 1.0;
|
double get additionalMaxSize => isAtMax ? 0.0 : 1.0;
|
||||||
List<double> get pixelSnapSizes => snapSizes.map(extentToPixels).toList();
|
List<double> get pixelSnapSizes => snapSizes.map(sizeToPixels).toList();
|
||||||
|
|
||||||
/// The scroll position gets inputs in terms of pixels, but the extent is
|
/// The scroll position gets inputs in terms of pixels, but the size is
|
||||||
/// expected to be expressed as a number between 0..1.
|
/// expected to be expressed as a number between 0..1.
|
||||||
void addPixelDelta(double delta, BuildContext context) {
|
void addPixelDelta(double delta, BuildContext context) {
|
||||||
if (availablePixels == 0) {
|
if (availablePixels == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
updateExtent(currentExtent + pixelsToExtent(delta), context);
|
updateSize(currentSize + pixelsToSize(delta), context);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the extent to the new value. [newExtent] should be a number between
|
/// Set the size to the new value. [newSize] should be a number between 0..1.
|
||||||
/// 0..1.
|
void updateSize(double newSize, BuildContext context) {
|
||||||
void updateExtent(double newExtent, BuildContext context) {
|
currentSize = newSize;
|
||||||
currentExtent = newExtent;
|
|
||||||
DraggableScrollableNotification(
|
DraggableScrollableNotification(
|
||||||
minExtent: minExtent,
|
minExtent: minSize,
|
||||||
maxExtent: maxExtent,
|
maxExtent: maxSize,
|
||||||
extent: currentExtent,
|
extent: currentSize,
|
||||||
initialExtent: initialExtent,
|
initialExtent: initialSize,
|
||||||
context: context,
|
context: context,
|
||||||
).dispatch(context);
|
).dispatch(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
double pixelsToExtent(double pixels) {
|
double pixelsToSize(double pixels) {
|
||||||
return pixels / availablePixels * maxExtent;
|
return pixels / availablePixels * maxSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
double extentToPixels(double extent) {
|
double sizeToPixels(double extent) {
|
||||||
return extent / maxExtent * availablePixels;
|
return extent / maxSize * availablePixels;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_currentExtent.removeListener(onExtentChanged);
|
_currentSize.removeListener(onSizeChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
_DraggableSheetExtent copyWith({
|
_DraggableSheetExtent copyWith({
|
||||||
required double minExtent,
|
required double minSize,
|
||||||
required double maxExtent,
|
required double maxSize,
|
||||||
required bool snap,
|
required bool snap,
|
||||||
required List<double> snapSizes,
|
required List<double> snapSizes,
|
||||||
required double initialExtent,
|
required double initialSize,
|
||||||
required VoidCallback onExtentChanged,
|
required VoidCallback onSizeChanged,
|
||||||
}) {
|
}) {
|
||||||
return _DraggableSheetExtent(
|
return _DraggableSheetExtent(
|
||||||
minExtent: minExtent,
|
minSize: minSize,
|
||||||
maxExtent: maxExtent,
|
maxSize: maxSize,
|
||||||
snap: snap,
|
snap: snap,
|
||||||
snapSizes: snapSizes,
|
snapSizes: snapSizes,
|
||||||
initialExtent: initialExtent,
|
initialSize: initialSize,
|
||||||
onExtentChanged: onExtentChanged,
|
onSizeChanged: onSizeChanged,
|
||||||
// Use the possibly updated initialExtent if the user hasn't dragged yet.
|
// Use the possibly updated initialExtent if the user hasn't dragged yet.
|
||||||
currentExtent: ValueNotifier<double>(hasChanged
|
currentSize: ValueNotifier<double>(hasChanged
|
||||||
? _currentExtent.value.clamp(minExtent, maxExtent)
|
? _currentSize.value.clamp(minSize, maxSize)
|
||||||
: initialExtent),
|
: initialSize),
|
||||||
hasChanged: hasChanged,
|
hasChanged: hasChanged,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -400,12 +400,12 @@ class _DraggableScrollableSheetState extends State<DraggableScrollableSheet> {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_extent = _DraggableSheetExtent(
|
_extent = _DraggableSheetExtent(
|
||||||
minExtent: widget.minChildSize,
|
minSize: widget.minChildSize,
|
||||||
maxExtent: widget.maxChildSize,
|
maxSize: widget.maxChildSize,
|
||||||
snap: widget.snap,
|
snap: widget.snap,
|
||||||
snapSizes: _impliedSnapSizes(),
|
snapSizes: _impliedSnapSizes(),
|
||||||
initialExtent: widget.initialChildSize,
|
initialSize: widget.initialChildSize,
|
||||||
onExtentChanged: _setExtent,
|
onSizeChanged: _setSize,
|
||||||
);
|
);
|
||||||
_scrollController = _DraggableScrollableSheetScrollController(extent: _extent);
|
_scrollController = _DraggableScrollableSheetScrollController(extent: _extent);
|
||||||
}
|
}
|
||||||
@ -455,11 +455,11 @@ class _DraggableScrollableSheetState extends State<DraggableScrollableSheet> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
_extent.hasChanged = false;
|
_extent.hasChanged = false;
|
||||||
_extent._currentExtent.value = _extent.initialExtent;
|
_extent._currentSize.value = _extent.initialSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _setExtent() {
|
void _setSize() {
|
||||||
setState(() {
|
setState(() {
|
||||||
// _extent has been updated when this is called.
|
// _extent has been updated when this is called.
|
||||||
});
|
});
|
||||||
@ -471,7 +471,7 @@ class _DraggableScrollableSheetState extends State<DraggableScrollableSheet> {
|
|||||||
builder: (BuildContext context, BoxConstraints constraints) {
|
builder: (BuildContext context, BoxConstraints constraints) {
|
||||||
_extent.availablePixels = widget.maxChildSize * constraints.biggest.height;
|
_extent.availablePixels = widget.maxChildSize * constraints.biggest.height;
|
||||||
final Widget sheet = FractionallySizedBox(
|
final Widget sheet = FractionallySizedBox(
|
||||||
heightFactor: _extent.currentExtent,
|
heightFactor: _extent.currentSize,
|
||||||
alignment: Alignment.bottomCenter,
|
alignment: Alignment.bottomCenter,
|
||||||
child: widget.builder(context, _scrollController),
|
child: widget.builder(context, _scrollController),
|
||||||
);
|
);
|
||||||
@ -490,12 +490,12 @@ class _DraggableScrollableSheetState extends State<DraggableScrollableSheet> {
|
|||||||
void _replaceExtent() {
|
void _replaceExtent() {
|
||||||
_extent.dispose();
|
_extent.dispose();
|
||||||
_extent = _extent.copyWith(
|
_extent = _extent.copyWith(
|
||||||
minExtent: widget.minChildSize,
|
minSize: widget.minChildSize,
|
||||||
maxExtent: widget.maxChildSize,
|
maxSize: widget.maxChildSize,
|
||||||
snap: widget.snap,
|
snap: widget.snap,
|
||||||
snapSizes: _impliedSnapSizes(),
|
snapSizes: _impliedSnapSizes(),
|
||||||
initialExtent: widget.initialChildSize,
|
initialSize: widget.initialChildSize,
|
||||||
onExtentChanged: _setExtent,
|
onSizeChanged: _setSize,
|
||||||
);
|
);
|
||||||
// Modify the existing scroll controller instead of replacing it so that
|
// Modify the existing scroll controller instead of replacing it so that
|
||||||
// developers listening to the controller do not have to rebuild their listeners.
|
// developers listening to the controller do not have to rebuild their listeners.
|
||||||
@ -584,7 +584,7 @@ class _DraggableScrollableSheetScrollController extends ScrollController {
|
|||||||
/// This class is a concrete subclass of [ScrollPosition] logic that handles a
|
/// This class is a concrete subclass of [ScrollPosition] logic that handles a
|
||||||
/// single [ScrollContext], such as a [Scrollable]. An instance of this class
|
/// single [ScrollContext], such as a [Scrollable]. An instance of this class
|
||||||
/// manages [ScrollActivity] instances, which changes the
|
/// manages [ScrollActivity] instances, which changes the
|
||||||
/// [_DraggableSheetExtent.currentExtent] or visible content offset in the
|
/// [_DraggableSheetExtent.currentSize] or visible content offset in the
|
||||||
/// [Scrollable]'s [Viewport]
|
/// [Scrollable]'s [Viewport]
|
||||||
///
|
///
|
||||||
/// See also:
|
/// See also:
|
||||||
@ -624,13 +624,13 @@ class _DraggableScrollableSheetScrollPosition
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool applyContentDimensions(double minScrollExtent, double maxScrollExtent) {
|
bool applyContentDimensions(double minScrollSize, double maxScrollSize) {
|
||||||
// We need to provide some extra extent if we haven't yet reached the max or
|
// We need to provide some extra size if we haven't yet reached the max or
|
||||||
// min extents. Otherwise, a list with fewer children than the extent of
|
// min sizes. Otherwise, a list with fewer children than the size of
|
||||||
// the available space will get stuck.
|
// the available space will get stuck.
|
||||||
return super.applyContentDimensions(
|
return super.applyContentDimensions(
|
||||||
minScrollExtent - extent.additionalMinExtent,
|
minScrollSize - extent.additionalMinSize,
|
||||||
maxScrollExtent + extent.additionalMaxExtent,
|
maxScrollSize + extent.additionalMaxSize,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -649,7 +649,7 @@ class _DraggableScrollableSheetScrollPosition
|
|||||||
bool get _isAtSnapSize {
|
bool get _isAtSnapSize {
|
||||||
return extent.snapSizes.any(
|
return extent.snapSizes.any(
|
||||||
(double snapSize) {
|
(double snapSize) {
|
||||||
return (extent.currentExtent - snapSize).abs() <= extent.pixelsToExtent(physics.tolerance.distance);
|
return (extent.currentSize - snapSize).abs() <= extent.pixelsToSize(physics.tolerance.distance);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -803,7 +803,7 @@ class _ResetNotifier extends ChangeNotifier {
|
|||||||
|
|
||||||
class _InheritedResetNotifier extends InheritedNotifier<_ResetNotifier> {
|
class _InheritedResetNotifier extends InheritedNotifier<_ResetNotifier> {
|
||||||
/// Creates an [InheritedNotifier] that the [DraggableScrollableSheet] will
|
/// Creates an [InheritedNotifier] that the [DraggableScrollableSheet] will
|
||||||
/// listen to for an indication that it should change its extent.
|
/// listen to for an indication that it should reset itself back to [DraggableScrollableSheet.initialChildSize].
|
||||||
///
|
///
|
||||||
/// The [child] and [notifier] properties must not be null.
|
/// The [child] and [notifier] properties must not be null.
|
||||||
const _InheritedResetNotifier({
|
const _InheritedResetNotifier({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user