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

151 lines
4.6 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,
this.bottom = false,
});
/// The phone layout: a full-width bar under the content, with icons.
final bool bottom;
final PipelineDetailTab selected;
final ValueChanged<PipelineDetailTab> onSelected;
@override
Widget build(BuildContext context) {
final cupertino = useCupertinoControls(context);
if (bottom) {
final divider = cupertino
? CupertinoColors.separator.resolveFrom(context)
: Theme.of(context).colorScheme.outlineVariant;
return DecoratedBox(
decoration: BoxDecoration(
border: Border(top: BorderSide(color: divider, width: 0.5)),
),
child: Padding(
padding: const EdgeInsets.all(8),
child: SizedBox(
width: double.infinity,
child: KeyedSubtree(
key: const Key('pipeline-detail-tabs'),
child: cupertino ? _cupertinoControl() : _materialControl(),
),
),
),
);
}
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,
expandedInsets: bottom ? EdgeInsets.zero : null,
onSelectionChanged: (selection) => onSelected(selection.single),
);
}
Widget _cupertinoControl() {
return CupertinoSegmentedControl<PipelineDetailTab>(
// The strip already insets 16; the control's default inset would push
// the tabs out of line with the content.
padding: EdgeInsets.zero,
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.
final text = Text(tab.label, maxLines: 1, softWrap: false);
if (!bottom) return text;
return FittedBox(
fit: BoxFit.scaleDown,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Builder(
builder: (context) => Icon(
useCupertinoControls(context) ? tab.cupertinoIcon : tab.icon,
size: 18,
),
),
text,
],
),
);
}
}
/// 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,
),
),
);
},
);
}
}