Files
pipelines/app/lib/features/pipelines/widgets/pipeline_tabs.dart
T

107 lines
3.3 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'pipeline_controls.dart';
import 'pipeline_detail_tab.dart';
export 'pipeline_detail_tab.dart';
/// Platform tabs: a Material segmented button or a Cupertino segmented control.
///
/// Keep the native control at every width. Horizontal scrolling gives scaled
/// labels their intrinsic width instead of squeezing or replacing the control.
class PipelineDetailTabs extends StatelessWidget {
const PipelineDetailTabs({
super.key,
required this.selected,
required this.onSelected,
});
final PipelineDetailTab selected;
final ValueChanged<PipelineDetailTab> onSelected;
@override
Widget build(BuildContext context) {
final cupertino = useCupertinoControls(context);
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: _ScrollWhenOverflowing(
child: KeyedSubtree(
key: const Key('pipeline-detail-tabs'),
child: cupertino ? _cupertinoControl() : _materialControl(),
),
),
);
}
Widget _materialControl() {
return SegmentedButton<PipelineDetailTab>(
segments: [
for (final tab in PipelineDetailTab.values)
ButtonSegment(value: tab, label: _label(tab)),
],
selected: {selected},
showSelectedIcon: false,
onSelectionChanged: (selection) => onSelected(selection.single),
);
}
Widget _cupertinoControl() {
return CupertinoSegmentedControl<PipelineDetailTab>(
groupValue: selected,
onValueChanged: onSelected,
children: {
for (final tab in PipelineDetailTab.values)
tab: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: _label(tab),
),
},
);
}
Widget _label(PipelineDetailTab tab) {
// One line, and let the control size the segment to the whole word rather
// than wrapping inside it when scaled text is wider than the pane.
return Text(tab.label, maxLines: 1, softWrap: false);
}
}
/// Gives [child] the full available width as a minimum, then scrolls instead of
/// squeezing it below its content width.
class _ScrollWhenOverflowing extends StatelessWidget {
const _ScrollWhenOverflowing({required this.child});
final Widget child;
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
if (!constraints.hasBoundedWidth) return child;
return ScrollConfiguration(
// Desktop pointer scrolling is vertical only, so allow dragging the
// tab row with a mouse as well as touch and trackpad.
behavior: ScrollConfiguration.of(context).copyWith(
dragDevices: const {
PointerDeviceKind.touch,
PointerDeviceKind.mouse,
PointerDeviceKind.stylus,
PointerDeviceKind.invertedStylus,
PointerDeviceKind.trackpad,
},
),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: ConstrainedBox(
constraints: BoxConstraints(minWidth: constraints.maxWidth),
child: child,
),
),
);
},
);
}
}