9bdabb32d9
Keep the Material-only shell path while preserving public cupertino parameters for callers. Narrow matching tests to Material hosts and behavior because the app always runs under MaterialApp.
169 lines
5.5 KiB
Dart
169 lines
5.5 KiB
Dart
import 'package:flutter/gestures.dart' show DragStartBehavior;
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import '../ui/adaptive/resize_grip.dart';
|
|
|
|
/// How wide the divider's invisible grab area is, centred on its line.
|
|
const double kListPaneDividerGrabWidth = 12;
|
|
|
|
/// How far one arrow key press moves the divider.
|
|
const double kListPaneDividerKeyStep = 16;
|
|
|
|
/// The one-pixel line between a list and its open item.
|
|
class ListPaneDividerLine extends StatelessWidget {
|
|
const ListPaneDividerLine({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) => const VerticalDivider(width: 1);
|
|
}
|
|
|
|
/// The grab area over [ListPaneDividerLine]: dragging it (mouse or touch)
|
|
/// resizes the list pane between [min] and [max], the arrow keys move it by
|
|
/// [kListPaneDividerKeyStep], and a double tap or click returns it to the
|
|
/// default policy. The handle is always visible and is emphasized while
|
|
/// hovered, focused or dragged.
|
|
class ListPaneDivider extends StatefulWidget {
|
|
const ListPaneDivider({
|
|
super.key,
|
|
required this.width,
|
|
required this.min,
|
|
required this.max,
|
|
required this.onChanged,
|
|
required this.onChangeEnd,
|
|
required this.onReset,
|
|
});
|
|
|
|
/// The list pane's current width.
|
|
final double width;
|
|
final double min;
|
|
final double max;
|
|
|
|
/// A new list width, live while dragging.
|
|
final ValueChanged<double> onChanged;
|
|
|
|
/// The drag or key press ended: the width is final.
|
|
final VoidCallback onChangeEnd;
|
|
final VoidCallback onReset;
|
|
|
|
@override
|
|
State<ListPaneDivider> createState() => _ListPaneDividerState();
|
|
}
|
|
|
|
class _ListPaneDividerState extends State<ListPaneDivider> {
|
|
bool _hovered = false;
|
|
bool _focused = false;
|
|
// The unclamped width the pointer asks for since the drag began, so moving
|
|
// back past a bound picks up where the pointer is, not where it stopped.
|
|
double? _dragWidth;
|
|
|
|
double _clamp(double value) => value.clamp(widget.min, widget.max);
|
|
|
|
// The list is at the start: in a right-to-left layout a drag to the left
|
|
// widens it.
|
|
double _forward(double dx) =>
|
|
Directionality.of(context) == TextDirection.rtl ? -dx : dx;
|
|
|
|
void _dragStart(DragStartDetails _) =>
|
|
setState(() => _dragWidth = widget.width);
|
|
|
|
void _dragUpdate(DragUpdateDetails details) {
|
|
final width = (_dragWidth ?? widget.width) + _forward(details.delta.dx);
|
|
_dragWidth = width;
|
|
widget.onChanged(_clamp(width));
|
|
}
|
|
|
|
void _dragEnd() {
|
|
if (_dragWidth == null) return;
|
|
setState(() => _dragWidth = null);
|
|
widget.onChangeEnd();
|
|
}
|
|
|
|
// A width a key press sent that the panes have not rebuilt with yet, so
|
|
// presses quicker than frames add up.
|
|
double? _stepped;
|
|
|
|
@override
|
|
void didUpdateWidget(covariant ListPaneDivider oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
_stepped = null;
|
|
}
|
|
|
|
void _step(double delta) {
|
|
final current = _stepped ?? widget.width;
|
|
final width = _clamp(current + delta);
|
|
if (width == current) return;
|
|
_stepped = width;
|
|
widget.onChanged(width);
|
|
widget.onChangeEnd();
|
|
}
|
|
|
|
KeyEventResult _key(FocusNode _, KeyEvent event) {
|
|
if (event is! KeyDownEvent && event is! KeyRepeatEvent) {
|
|
return KeyEventResult.ignored;
|
|
}
|
|
final key = event.logicalKey;
|
|
if (key == LogicalKeyboardKey.arrowRight ||
|
|
key == LogicalKeyboardKey.arrowLeft) {
|
|
final right = key == LogicalKeyboardKey.arrowRight ? 1.0 : -1.0;
|
|
_step(_forward(right * kListPaneDividerKeyStep));
|
|
return KeyEventResult.handled;
|
|
}
|
|
return KeyEventResult.ignored;
|
|
}
|
|
|
|
String _describe(double width) => '${width.round()} pixels';
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final dragging = _dragWidth != null;
|
|
final active = _hovered || _focused || dragging;
|
|
final increased = _clamp(widget.width + kListPaneDividerKeyStep);
|
|
final decreased = _clamp(widget.width - kListPaneDividerKeyStep);
|
|
return Semantics(
|
|
identifier: 'list-detail-divider',
|
|
label: 'List width',
|
|
hint: 'Drag, or use the arrow keys, to resize the list',
|
|
slider: true,
|
|
value: _describe(widget.width),
|
|
increasedValue: _describe(increased),
|
|
decreasedValue: _describe(decreased),
|
|
onIncrease: increased == widget.width
|
|
? null
|
|
: () => _step(kListPaneDividerKeyStep),
|
|
onDecrease: decreased == widget.width
|
|
? null
|
|
: () => _step(-kListPaneDividerKeyStep),
|
|
child: Focus(
|
|
onKeyEvent: _key,
|
|
onFocusChange: (value) => setState(() => _focused = value),
|
|
child: MouseRegion(
|
|
cursor: SystemMouseCursors.resizeColumn,
|
|
onEnter: (_) => setState(() => _hovered = true),
|
|
onExit: (_) => setState(() => _hovered = false),
|
|
child: GestureDetector(
|
|
key: const ValueKey('list-detail-divider'),
|
|
behavior: HitTestBehavior.opaque,
|
|
// The slider semantics above stand for the drag.
|
|
excludeFromSemantics: true,
|
|
// The handle follows the pointer from where it went down.
|
|
dragStartBehavior: DragStartBehavior.down,
|
|
onHorizontalDragStart: _dragStart,
|
|
onHorizontalDragUpdate: _dragUpdate,
|
|
onHorizontalDragEnd: (_) => _dragEnd(),
|
|
onHorizontalDragCancel: _dragEnd,
|
|
onDoubleTap: widget.onReset,
|
|
child: Center(
|
|
child: AdaptiveResizeGrip(
|
|
axis: Axis.vertical,
|
|
compact: true,
|
|
emphasized: active,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|