Merge commit '09389f18cb01effd9c855e5b91722ade4816395a' into tab-spacing-anim
This commit is contained in:
@@ -28,7 +28,7 @@ export 'segmented_bar_pieces.dart' show SegmentedBackButton, SegmentedPill;
|
||||
/// bar's height through [MediaQuery.paddingOf], so a page keeps its
|
||||
/// scaffold's structure; [SegmentedBarOverlay] does the same anywhere else.
|
||||
/// It handles the top safe area itself.
|
||||
class SegmentedBar extends StatelessWidget
|
||||
class SegmentedBar extends StatefulWidget
|
||||
implements ObstructingPreferredSizeWidget {
|
||||
const SegmentedBar({
|
||||
super.key,
|
||||
@@ -41,6 +41,8 @@ class SegmentedBar extends StatelessWidget
|
||||
this.subtitle,
|
||||
this.subtitleIcon,
|
||||
this.onTitlePressed,
|
||||
this.expandableTitle = false,
|
||||
this.titleAction,
|
||||
this.titleSemanticLabel,
|
||||
this.titleKey,
|
||||
this.actions = const [],
|
||||
@@ -79,6 +81,12 @@ class SegmentedBar extends StatelessWidget
|
||||
|
||||
/// Makes the title pill a button (an information page, a picker).
|
||||
final VoidCallback? onTitlePressed;
|
||||
|
||||
/// Lets the title expand over the content without changing bar clearance.
|
||||
final bool expandableTitle;
|
||||
|
||||
/// An action shown inside the expanded title pill.
|
||||
final Widget? titleAction;
|
||||
final String? titleSemanticLabel;
|
||||
|
||||
/// The key of the title's button (or its content when not pressable).
|
||||
@@ -142,14 +150,74 @@ class SegmentedBar extends StatelessWidget
|
||||
Theme.of(context).scaffoldBackgroundColor;
|
||||
}
|
||||
|
||||
@override
|
||||
State<SegmentedBar> createState() => _SegmentedBarState();
|
||||
}
|
||||
|
||||
class _SegmentedBarState extends State<SegmentedBar> {
|
||||
bool _expanded = false;
|
||||
|
||||
/// The expanded title is drawn in the overlay, anchored to the collapsed
|
||||
/// pill: drawn inside the bar it would overflow the bar's bounds, where
|
||||
/// its pipeline action could be seen but not tapped.
|
||||
final _titleLink = LayerLink();
|
||||
final _expandedTitle = OverlayPortalController();
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant SegmentedBar oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.title != widget.title ||
|
||||
oldWidget.subtitle != widget.subtitle) {
|
||||
_expanded = false;
|
||||
}
|
||||
}
|
||||
|
||||
void _toggleTitle() {
|
||||
// The portal stays attached once used and draws nothing while
|
||||
// collapsed, so collapsing on a new run (during a rebuild) needs no
|
||||
// overlay call.
|
||||
if (!_expandedTitle.isShowing) _expandedTitle.show();
|
||||
setState(() => _expanded = !_expanded);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino = widget.cupertino;
|
||||
final title = widget.title;
|
||||
final titleWidget = widget.titleWidget;
|
||||
final titleWidgetMaxWidth = widget.titleWidgetMaxWidth;
|
||||
final leadingWidget = widget.leading;
|
||||
final automaticallyImplyLeading = widget.automaticallyImplyLeading;
|
||||
final subtitle = widget.subtitle;
|
||||
final subtitleIcon = widget.subtitleIcon;
|
||||
final onTitlePressed = widget.onTitlePressed;
|
||||
final titleSemanticLabel = widget.titleSemanticLabel;
|
||||
final titleKey = widget.titleKey;
|
||||
final actions = widget.actions;
|
||||
final pillColor = widget.pillColor;
|
||||
final foregroundColor = widget.foregroundColor;
|
||||
final bottomPadding = widget.bottomPadding;
|
||||
final leading =
|
||||
this.leading ??
|
||||
leadingWidget ??
|
||||
(automaticallyImplyLeading
|
||||
? impliedSegmentedLeading(context, cupertino)
|
||||
: null);
|
||||
final showTitle = title.isNotEmpty || (subtitle ?? '').isNotEmpty;
|
||||
Widget titlePill(bool expanded) => SegmentedTitlePill(
|
||||
cupertino: cupertino,
|
||||
title: title,
|
||||
subtitle: subtitle,
|
||||
subtitleIcon: subtitleIcon,
|
||||
onPressed: widget.expandableTitle ? _toggleTitle : onTitlePressed,
|
||||
semanticLabel: widget.expandableTitle
|
||||
? '${titleSemanticLabel ?? title}, ${expanded ? 'expanded' : 'collapsed'}'
|
||||
: titleSemanticLabel,
|
||||
buttonKey: expanded ? null : titleKey,
|
||||
expanded: expanded,
|
||||
titleAction: expanded ? widget.titleAction : null,
|
||||
foreground: foregroundColor,
|
||||
fill: pillColor,
|
||||
);
|
||||
final Widget bar = LayoutBuilder(
|
||||
builder: (context, constraints) => Row(
|
||||
children: [
|
||||
@@ -182,17 +250,37 @@ class SegmentedBar extends StatelessWidget
|
||||
),
|
||||
),
|
||||
)
|
||||
: showTitle && !widget.expandableTitle
|
||||
? titlePill(false)
|
||||
: showTitle
|
||||
? SegmentedTitlePill(
|
||||
cupertino: cupertino,
|
||||
title: title,
|
||||
subtitle: subtitle,
|
||||
subtitleIcon: subtitleIcon,
|
||||
onPressed: onTitlePressed,
|
||||
semanticLabel: titleSemanticLabel,
|
||||
buttonKey: titleKey,
|
||||
foreground: foregroundColor,
|
||||
fill: pillColor,
|
||||
? LayoutBuilder(
|
||||
builder: (context, titleSpace) => OverlayPortal(
|
||||
controller: _expandedTitle,
|
||||
overlayChildBuilder: (context) =>
|
||||
!(_expanded && widget.expandableTitle)
|
||||
? const SizedBox.shrink()
|
||||
: Positioned(
|
||||
top: 0,
|
||||
left: 0,
|
||||
child: CompositedTransformFollower(
|
||||
link: _titleLink,
|
||||
showWhenUnlinked: false,
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
maxWidth: titleSpace.maxWidth,
|
||||
),
|
||||
child: titlePill(true),
|
||||
),
|
||||
),
|
||||
),
|
||||
child: CompositedTransformTarget(
|
||||
link: _titleLink,
|
||||
child: ExcludeSemantics(
|
||||
excluding: _expanded && widget.expandableTitle,
|
||||
child: titlePill(false),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
: const SizedBox.shrink(),
|
||||
),
|
||||
@@ -211,7 +299,7 @@ class SegmentedBar extends StatelessWidget
|
||||
? 0
|
||||
: segmentedPieceHeight(cupertino) + 8) -
|
||||
8 -
|
||||
constraints.maxWidth * minTitleShare,
|
||||
constraints.maxWidth * SegmentedBar.minTitleShare,
|
||||
),
|
||||
),
|
||||
child: SegmentedPill(
|
||||
@@ -230,7 +318,7 @@ class SegmentedBar extends StatelessWidget
|
||||
],
|
||||
),
|
||||
);
|
||||
final background = _background(context);
|
||||
final background = widget._background(context);
|
||||
final inset = MediaQuery.paddingOf(context).top;
|
||||
return Stack(
|
||||
children: [
|
||||
@@ -271,12 +359,12 @@ class SegmentedBar extends StatelessWidget
|
||||
child: Padding(
|
||||
padding: EdgeInsetsDirectional.fromSTEB(
|
||||
12,
|
||||
_topPadding,
|
||||
SegmentedBar._topPadding,
|
||||
12,
|
||||
bottomPadding,
|
||||
),
|
||||
child: MediaQuery.withClampedTextScaling(
|
||||
maxScaleFactor: maxTextScale,
|
||||
maxScaleFactor: SegmentedBar.maxTextScale,
|
||||
child: bar,
|
||||
),
|
||||
),
|
||||
|
||||
@@ -94,6 +94,8 @@ class SegmentedTitlePill extends StatelessWidget {
|
||||
this.onPressed,
|
||||
this.semanticLabel,
|
||||
this.buttonKey,
|
||||
this.expanded = false,
|
||||
this.titleAction,
|
||||
this.foreground,
|
||||
this.fill,
|
||||
});
|
||||
@@ -107,6 +109,8 @@ class SegmentedTitlePill extends StatelessWidget {
|
||||
|
||||
/// The key of the pressable title, for finders.
|
||||
final Key? buttonKey;
|
||||
final bool expanded;
|
||||
final Widget? titleAction;
|
||||
final Color? foreground;
|
||||
final Color? fill;
|
||||
|
||||
@@ -137,44 +141,56 @@ class SegmentedTitlePill extends StatelessWidget {
|
||||
final subtitle = this.subtitle ?? '';
|
||||
final content = Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
|
||||
child: Column(
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Semantics(
|
||||
header: true,
|
||||
child: Text(
|
||||
title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: titleStyle,
|
||||
Flexible(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Semantics(
|
||||
header: true,
|
||||
child: Text(
|
||||
title,
|
||||
maxLines: expanded ? null : 1,
|
||||
overflow: expanded
|
||||
? TextOverflow.visible
|
||||
: TextOverflow.ellipsis,
|
||||
style: titleStyle,
|
||||
),
|
||||
),
|
||||
if (subtitle.isNotEmpty)
|
||||
// One line that ellipsises as a whole, so a narrow pill never
|
||||
// overflows on the icon.
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
if (subtitleIcon != null) ...[
|
||||
WidgetSpan(
|
||||
alignment: PlaceholderAlignment.middle,
|
||||
child: Icon(
|
||||
subtitleIcon,
|
||||
size: subtitleStyle.fontSize,
|
||||
color: subtitleStyle.color,
|
||||
),
|
||||
),
|
||||
const TextSpan(text: ' '),
|
||||
],
|
||||
TextSpan(text: subtitle),
|
||||
],
|
||||
),
|
||||
maxLines: expanded ? null : 1,
|
||||
overflow: expanded
|
||||
? TextOverflow.visible
|
||||
: TextOverflow.ellipsis,
|
||||
style: subtitleStyle,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (subtitle.isNotEmpty)
|
||||
// One line that ellipsises as a whole, so a narrow pill never
|
||||
// overflows on the icon.
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
if (subtitleIcon != null) ...[
|
||||
WidgetSpan(
|
||||
alignment: PlaceholderAlignment.middle,
|
||||
child: Icon(
|
||||
subtitleIcon,
|
||||
size: subtitleStyle.fontSize,
|
||||
color: subtitleStyle.color,
|
||||
),
|
||||
),
|
||||
const TextSpan(text: ' '),
|
||||
],
|
||||
TextSpan(text: subtitle),
|
||||
],
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: subtitleStyle,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -200,6 +216,19 @@ class SegmentedTitlePill extends StatelessWidget {
|
||||
: InkWell(key: buttonKey, onTap: onPressed, child: content),
|
||||
);
|
||||
}
|
||||
return SegmentedPill(cupertino: cupertino, fill: fill, child: body);
|
||||
return SegmentedPill(
|
||||
cupertino: cupertino,
|
||||
fill: fill,
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Flexible(
|
||||
fit: FlexFit.loose,
|
||||
child: IntrinsicWidth(child: body),
|
||||
),
|
||||
?titleAction,
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user