Files
swiftcontrol/lib/pages/shell/app_shell.dart
Jonas Acker 7a9c1874f7 Help (?) on every tab; overlay offer's close button flush with the card edge
The phone top bar showed the Help button on Ride only. It shows on every
tab now; the plan badge stays on Ride.

The overlay offer's x was a centred glyph in a 48 dp button, ~15 dp short of
the card's right edge where the settings line's icon sits. The glyph now ends
at the edge, inside the same 48 dp target, and the Bluetooth-only offer's line
is shorter so it no longer wraps to four lines in German.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 16:24:21 +01:00

920 lines
33 KiB
Dart

import 'dart:math' as math;
import 'dart:async';
import 'package:bike_control/main.dart' show shownTrainerAppName;
import 'package:bike_control/bluetooth/devices/base_device.dart';
import 'package:bike_control/bluetooth/devices/bluetooth_device.dart';
import 'package:bike_control/gen/l10n.dart';
import 'package:bike_control/pages/activity/activity_log.dart';
import 'package:bike_control/pages/home/home_page.dart' show chainProxy;
import 'package:bike_control/pages/plan/plan_account_page.dart';
import 'package:bike_control/services/blog_news.dart';
import 'package:bike_control/utils/core.dart';
import 'package:bike_control/utils/iap/iap_manager.dart';
import 'package:bike_control/utils/window_size.dart';
import 'package:bike_control/widgets/menu.dart';
import 'package:bike_control/widgets/plan/vs_trial_meter.dart';
import 'package:bike_control/widgets/title.dart';
import 'package:bike_control/widgets/ui/app_theme.dart';
import 'package:bike_control/widgets/ui/bk_brand_band.dart';
import 'package:bike_control/widgets/ui/bk_brand_mark.dart';
import 'package:bike_control/widgets/ui/bk_page_header.dart';
import 'package:bike_control/widgets/ui/bk_tappable.dart';
import 'package:bike_control/widgets/ui/colors.dart';
import 'package:bike_control/widgets/ui/help_button.dart';
import 'package:bike_control/widgets/ui/pro_badge.dart';
import 'package:bike_control/widgets/ui/toast.dart';
import 'package:bike_control/widgets/ui/type_scale.dart';
import 'package:flutter/foundation.dart' show ValueListenable, defaultTargetPlatform;
import 'package:bike_control/widgets/changelog/whats_new.dart';
import 'package:flutter/rendering.dart'
show
BoxHitTestResult,
ContainerBoxParentData,
ContainerRenderObjectMixin,
RenderBoxContainerDefaultsMixin,
RenderObjectVisitor;
import 'package:shadcn_flutter/shadcn_flutter.dart';
import 'package:bike_control/widgets/ui/controller_mark.dart';
/// The app's four top-level sections.
enum AppSection {
ride,
devices,
activity,
settings;
IconData get icon => switch (this) {
AppSection.ride => LucideIcons.bike,
AppSection.devices => LucideIcons.bluetooth,
AppSection.activity => LucideIcons.activity,
AppSection.settings => LucideIcons.settings,
};
String label(BuildContext context) {
final l10n = AppLocalizations.of(context);
return switch (this) {
AppSection.ride => l10n.navRide,
AppSection.devices => l10n.navDevices,
AppSection.activity => l10n.activity,
AppSection.settings => l10n.navSettings,
};
}
}
/// What the shell's chrome and its content share: the selected section, the
/// session's activity log, and the blog behind Activity's news banner.
class ShellController {
ShellController({BlogNewsController? news}) : news = news ?? BlogNewsController() {
section.addListener(_markNewsReadIfViewed);
this.news.hasUnread.addListener(_markNewsReadIfViewed);
}
final ValueNotifier<AppSection> section = ValueNotifier(AppSection.ride);
final ActivityLogController activity = ActivityLogController();
final BlogNewsController news;
void select(AppSection value) => section.value = value;
/// Whether News is on screen right now: Activity opens on its banner.
bool get viewingNews => section.value == AppSection.activity;
/// Posts on screen are posts read: the dot goes the moment News shows.
void _markNewsReadIfViewed() {
if (viewingNews) news.markRead();
}
void dispose() {
section.removeListener(_markNewsReadIfViewed);
news.hasUnread.removeListener(_markNewsReadIfViewed);
section.dispose();
activity.dispose();
news.dispose();
}
}
/// Hands the shell's controller to what is pushed above the shell, so a
/// pushed page in a wide window can keep the sidebar beside it.
class ShellScope extends InheritedWidget {
const ShellScope({super.key, required this.controller, required super.child});
final ShellController controller;
static ShellController? maybeOf(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<ShellScope>()?.controller;
@override
bool updateShouldNotify(ShellScope oldWidget) => controller != oldWidget.controller;
}
enum _NavLayout { bottom, side }
enum _NavDot { error, news }
/// The small status dot on a navigation item: a filled circle with
/// a ring in the background colour, so it reads over the icon's edge.
class NavDot extends StatelessWidget {
const NavDot({super.key, required this.color});
final Color color;
@override
Widget build(BuildContext context) {
return Container(
width: 8,
height: 8,
decoration: BoxDecoration(
color: color,
shape: BoxShape.circle,
border: Border.all(color: Theme.of(context).colorScheme.background, width: 1.5),
),
);
}
}
/// One section entry of the tab bar or the sidebar: a labelled
/// button that reports whether it is selected. The Activity entry carries a
/// red dot while the log holds an error, else an accent dot while the blog
/// has an unread post, and says so to a screen reader.
class ShellNavItem extends StatefulWidget {
const ShellNavItem._({
required this.section,
required this.controller,
required _NavLayout layout,
this.onSelected,
this.planOpen = false,
}) : _layout = layout;
final AppSection section;
final ShellController controller;
final _NavLayout _layout;
/// Runs after the section is selected (a page pushed beside the sidebar
/// closes itself).
final VoidCallback? onSelected;
/// The plan page is open beside the sidebar: no section is the current one.
final bool planOpen;
@override
State<ShellNavItem> createState() => _ShellNavItemState();
}
class _ShellNavItemState extends State<ShellNavItem> {
bool _hovered = false;
@override
Widget build(BuildContext context) {
final section = widget.section;
final controller = widget.controller;
return ListenableBuilder(
listenable: Listenable.merge([controller.section, controller.activity.hasErrors, controller.news.hasUnread]),
builder: (context, _) {
final selected = !widget.planOpen && controller.section.value == section;
final isActivity = section == AppSection.activity;
final dot = !isActivity
? null
: controller.activity.hasErrors.value
? _NavDot.error
: controller.news.hasUnread.value
? _NavDot.news
: null;
final label = section.label(context);
final l10n = AppLocalizations.of(context);
return BkTappable(
onPressed: () {
controller.select(section);
widget.onSelected?.call();
},
label: switch (dot) {
_NavDot.error => l10n.a11yTabHasErrors(label),
_NavDot.news => l10n.a11yTabHasNewPosts(label),
null => label,
},
selected: selected,
excludeChildSemantics: true,
borderRadius: BorderRadius.circular(widget._layout == _NavLayout.bottom ? 12 : 10),
onHover: (hovered) => setState(() => _hovered = hovered),
child: _build(context, label, selected, dot),
);
},
);
}
Widget _icon(BuildContext context, Color color, _NavDot? dot, double size) {
return Stack(
clipBehavior: Clip.none,
children: [
Icon(widget.section.icon, size: size, color: color),
if (dot != null)
Positioned(
key: ValueKey(dot == _NavDot.error ? 'activity-error-dot' : 'activity-news-dot'),
right: -3,
top: -2,
child: NavDot(color: dot == _NavDot.error ? BkStatusColors.of(context).danger : bkAccentText(context)),
),
],
);
}
Widget _build(BuildContext context, String label, bool selected, _NavDot? showDot) {
final cs = Theme.of(context).colorScheme;
switch (widget._layout) {
case _NavLayout.bottom:
final color = selected ? bkAccentText(context) : cs.mutedForeground;
return ConstrainedBox(
constraints: const BoxConstraints(minHeight: 52),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
spacing: 3,
children: [
// The selected tab's icon sits on an accent pill; the slot is
// the pill's size either way, so selecting moves nothing.
SizedBox(
width: 52,
height: 30,
child: selected
? DecoratedBox(
key: const ValueKey('shell-tab-pill'),
decoration: BoxDecoration(
color: BkBrandColors.of(context).navPill,
borderRadius: BorderRadius.circular(15),
),
child: Center(child: _icon(context, color, showDot, 22)),
)
: Center(child: _icon(context, color, showDot, 22)),
),
Text(
label,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: context.typography.caption.copyWith(
color: color,
fontWeight: selected ? FontWeight.w600 : FontWeight.w500,
),
),
],
),
);
case _NavLayout.side:
final color = selected ? cs.primaryForeground : cs.foreground;
return Container(
constraints: const BoxConstraints(minHeight: 40),
padding: const EdgeInsets.symmetric(horizontal: 12),
decoration: BoxDecoration(
color: selected ? cs.primary : (_hovered ? bkCardHover(context) : null),
borderRadius: BorderRadius.circular(10),
),
child: Row(
spacing: 12,
children: [
_icon(context, selected ? color : cs.mutedForeground, showDot, 18),
Expanded(
child: Text(
label,
style: context.typography.small.copyWith(
color: color,
fontWeight: selected ? FontWeight.w600 : FontWeight.w500,
),
),
),
],
),
);
}
}
}
/// The bottom tab bar below 840: four sections, icons over labels. On a
/// wider window the items keep a tab bar's width, centred, instead of
/// stretching across it.
class ShellTabBar extends StatelessWidget {
const ShellTabBar({super.key, required this.controller});
static double _bottomInset(BuildContext context) {
final inset = MediaQuery.paddingOf(context).bottom;
if (defaultTargetPlatform != TargetPlatform.iOS) return inset;
return (inset - homeIndicatorOverlap).clamp(0, inset);
}
final ShellController controller;
/// The widest the four items get together.
static const double maxWidth = 480;
/// How far the labels may reach into an iPhone's home-indicator inset. The
/// indicator itself is a thin bar near the very bottom; the full inset left
/// a wide empty band under the tab labels.
static const double homeIndicatorOverlap = 14;
@override
Widget build(BuildContext context) {
final cs = Theme.of(context).colorScheme;
// Toasts sit above the bar, never on it.
return ReportsToastClearance(
child: DecoratedBox(
decoration: BoxDecoration(
color: cs.background,
border: Border(top: BorderSide(color: cs.border, width: 0.5)),
),
child: SafeArea(
top: false,
bottom: false,
child: Padding(
// Room under the bar's top line before the icons; below them the
// system's bottom inset — on iOS a little less of it (see
// [homeIndicatorOverlap]). Android's inset can hold its 3-button
// bar, so it is cleared in full.
padding: EdgeInsets.fromLTRB(4, 8, 4, 2 + _bottomInset(context)),
child: Center(
heightFactor: 1,
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: maxWidth),
child: Row(
children: [
for (final section in AppSection.values)
Expanded(
child: ShellNavItem._(section: section, controller: controller, layout: _NavLayout.bottom),
),
],
),
),
),
),
),
),
);
}
}
/// The expanded window's permanent sidebar: the wordmark, the sections, and
/// at its foot the current plan and Help & Support.
class ShellSidebar extends StatelessWidget {
const ShellSidebar({super.key, required this.controller, this.onSectionSelected, this.planSelected = false});
final ShellController controller;
/// Runs after a section is chosen: a page pushed beside the sidebar closes.
final VoidCallback? onSectionSelected;
/// The plan page is open beside the sidebar: its plan card shows selected.
final bool planSelected;
static const double width = 232;
@override
Widget build(BuildContext context) {
final cs = Theme.of(context).colorScheme;
return Container(
width: width,
decoration: BoxDecoration(
color: bkSunkenSurface(context),
border: Border(right: BorderSide(color: cs.border, width: 0.5)),
),
padding: const EdgeInsets.fromLTRB(12, 16, 12, 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(8, 4, 8, 16),
child: Row(
spacing: 10,
children: [
const BkBrandMark(size: 30),
Flexible(
child: Semantics(
header: true,
child: Text(
'BikeControl',
style: context.typography.large.copyWith(fontWeight: FontWeight.w700, letterSpacing: -0.3),
),
),
),
],
),
),
for (final section in AppSection.values)
Padding(
padding: const EdgeInsets.only(bottom: 2),
child: ShellNavItem._(
section: section,
controller: controller,
layout: _NavLayout.side,
onSelected: onSectionSelected,
planOpen: planSelected,
),
),
const Spacer(),
SidebarPlanCard(selected: planSelected),
const Gap(8),
const HelpButton(style: HelpButtonStyle.sidebar),
],
),
);
}
}
/// The page's top bar: the title ("BikeControl" on the phone's Ride, the
/// section's name elsewhere) and the page's actions.
class ShellTopBar extends StatelessWidget {
const ShellTopBar({
super.key,
required this.section,
required this.compact,
this.showPlanAndHelp = false,
this.showHelp = false,
this.shell,
this.scrolled,
});
/// Whether the content has scrolled under the bar: a hairline then
/// separates the two. Flush with the page at the top.
final ValueListenable<bool>? scrolled;
/// The shell, for the expanded window's device chips (a tap opens
/// Devices). The chips show only when it is given.
final ShellController? shell;
final AppSection section;
/// The phone's large title, with the update strip under the bar instead of
/// a button in it — below 840.
final bool compact;
/// The plan badge and the (?) Help icon — below 840, where there is no
/// sidebar to carry them.
final bool showPlanAndHelp;
/// The (?) Help icon alone, for tabs that don't carry the plan badge.
final bool showHelp;
/// The phone bar's content height: one 44 dp touch target, the tallest
/// thing any tab puts beside the title.
static const double compactHeight = 44;
@override
Widget build(BuildContext context) {
final title = compact && section == AppSection.ride ? 'BikeControl' : section.label(context);
final style = compact
? context.typography.x2Large.copyWith(fontWeight: FontWeight.w700, letterSpacing: -0.4)
: BkPageHeader.titleStyle(context);
// On a phone the title sits 4 in from the cards' edge; from 600 it lines
// up with the cards, as beside the sidebar.
final phone = isCompactWindow(context);
final bar = AppBar(
padding: EdgeInsets.fromLTRB(phone ? 16 : 24, compact ? 10 : 16, phone ? 8 : 20, 8),
backgroundColor: Theme.of(context).colorScheme.background,
// A fixed height on the phone, so the title sits at the same place on
// every tab: Ride's plan badge and (?) or Activity's Clear would
// otherwise make the bar taller and push the title down.
height: compact ? compactHeight : null,
// The phone's Ride title is the wordmark: the mark leads it — unless the
// two do not fit (a small phone, with the plan badge, (?) and ⋮ beside
// them), where the mark steps aside rather than cutting the name to
// "BikeContr…".
title: _LeadUnlessTight(
gap: 8,
lead: compact && section == AppSection.ride ? const BkBrandMark(size: 34) : null,
child: Semantics(
header: true,
child: Text(title, style: style, maxLines: 1, overflow: TextOverflow.ellipsis),
),
),
trailingGap: 4,
trailing: [
if (shell != null) ShellDeviceChips(shell: shell!),
if (!compact) const AppUpdateButton(),
if (showPlanAndHelp) const PlanBadge(),
if (showPlanAndHelp || showHelp) const HelpButton(),
// The bar's ⋮: in debug builds, the developer tools. Activity's own
// actions sit on its sections' titles (Clear, the rides' ⋮).
const DebugMenuButton(),
],
);
final scrolled = this.scrolled;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
bar,
// A waiting update on the phone: a strip under the bar, which has no
// room for a labelled button beside the title.
if (compact) const AppUpdateButton(banner: true),
// An update's notes, unread: until opened or dismissed.
WhatsNewBanner(horizontalMargin: phone ? 16 : 24),
if (scrolled != null)
ValueListenableBuilder<bool>(
valueListenable: scrolled,
builder: (context, isScrolled, _) => AnimatedOpacity(
opacity: isScrolled ? 1 : 0,
duration: const Duration(milliseconds: 150),
child: Container(
key: const ValueKey('shell-top-bar-separator'),
height: 1,
color: Theme.of(context).colorScheme.border,
),
),
),
],
);
}
}
enum PlanTier { pro, base, trial }
PlanTier currentPlanTier() {
final iap = IAPManager.instance;
if (iap.isProEnabled) return PlanTier.pro;
if (iap.isPurchased.value) return PlanTier.base;
return PlanTier.trial;
}
String planName(BuildContext context, PlanTier tier) => switch (tier) {
PlanTier.pro => 'Pro',
PlanTier.base => AppLocalizations.of(context).fullVersion,
PlanTier.trial => AppLocalizations.of(context).chainTrialTitle,
};
/// Rebuilds [builder] whenever the purchase state changes.
class _PlanListener extends StatelessWidget {
const _PlanListener({required this.builder});
final Widget Function(BuildContext context, PlanTier tier) builder;
@override
Widget build(BuildContext context) {
final iap = IAPManager.instance;
return ListenableBuilder(
listenable: Listenable.merge([iap.entitlements, iap.isPurchased]),
builder: (context, _) => builder(context, currentPlanTier()),
);
}
}
/// The plan as a small badge ("Base", "Trial", PRO) that opens the plans.
class PlanBadge extends StatelessWidget {
const PlanBadge({super.key});
@override
Widget build(BuildContext context) {
return _PlanListener(
builder: (context, tier) {
final cs = Theme.of(context).colorScheme;
final badge = tier == PlanTier.pro
? const ProBadge()
: Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
decoration: BoxDecoration(color: cs.muted, borderRadius: BorderRadius.circular(999)),
child: Text(
planName(context, tier),
style: context.typography.xSmall.copyWith(color: cs.foreground, fontWeight: FontWeight.w600),
),
);
return BkTappable(
key: const ValueKey('plan-badge'),
onPressed: () => openPlanAccount(context),
label: '${AppLocalizations.of(context).currentPlan}: ${planName(context, tier)}',
excludeChildSemantics: true,
borderRadius: BorderRadius.circular(999),
child: ConstrainedBox(
constraints: const BoxConstraints(minWidth: 44, minHeight: 44),
child: Center(child: badge),
),
);
},
);
}
}
/// The sidebar's plan card: "Current plan", the plan's name, its status line,
/// — below Pro — a Go Pro link and, without Pro on this device, what is left
/// of today's virtual shifting trial. The whole card opens the plans.
class SidebarPlanCard extends StatelessWidget {
const SidebarPlanCard({super.key, this.selected = false});
/// The plan page is open: the card is outlined and tapping it does nothing.
final bool selected;
@override
Widget build(BuildContext context) {
return _PlanListener(
builder: (context, tier) {
final cs = Theme.of(context).colorScheme;
final l10n = AppLocalizations.of(context);
final status = IAPManager.instance.getStatusMessage();
return BkTappable(
key: const ValueKey('plan-card'),
onPressed: selected ? null : () => openPlanAccount(context),
selected: selected,
borderRadius: BorderRadius.circular(12),
// The plan wears the brand band, as on Settings.
child: Container(
foregroundDecoration: selected
? BoxDecoration(
borderRadius: BorderRadius.circular(12),
border: Border.all(color: cs.primary, width: 1.5),
)
: null,
child: BkBrandBand(
borderRadius: BorderRadius.circular(12),
padding: const EdgeInsets.all(12),
contours: const BkContourPlacement.compact(),
child: Builder(
builder: (context) {
final cs = Theme.of(context).colorScheme;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
spacing: 2,
children: [
Row(
children: [
Expanded(
child: Text(
l10n.currentPlan,
style: context.typography.caption.copyWith(color: cs.mutedForeground),
),
),
if (tier != PlanTier.pro)
Text(
l10n.goPro,
style: context.typography.xSmall.copyWith(
color: bkAccentText(context),
fontWeight: FontWeight.w600,
),
),
],
),
Text(
planName(context, tier).toUpperCase(),
style: BkDisplay.title(context),
),
if (status.isNotEmpty && status != planName(context, tier))
Text(
status,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: context.typography.caption.copyWith(color: cs.mutedForeground),
),
// Today's virtual shifting trial, as on Settings' plan card.
const VsTrialMeterSlot(gap: 6, compact: true),
],
);
},
),
),
),
);
},
);
}
}
/// The expanded window's header chips: the connected controller with its
/// battery, and the bridged trainer with the app it sends to. Each opens
/// Devices. Nothing while no device is connected.
class ShellDeviceChips extends StatefulWidget {
const ShellDeviceChips({super.key, required this.shell});
final ShellController shell;
@override
State<ShellDeviceChips> createState() => _ShellDeviceChipsState();
}
class _ShellDeviceChipsState extends State<ShellDeviceChips> {
late final StreamSubscription<BaseDevice> _connections;
@override
void initState() {
super.initState();
_connections = core.connection.connectionStream.listen((_) {
if (mounted) setState(() {});
});
}
@override
void dispose() {
_connections.cancel();
super.dispose();
}
void _openDevices() => widget.shell.select(AppSection.devices);
@override
Widget build(BuildContext context) {
return ValueListenableBuilder<bool>(
valueListenable: core.connection.hasDevices,
builder: (context, _, _) {
final controller = core.connection.controllerDevices.where((d) => d.isConnected).firstOrNull;
final proxy = chainProxy();
final trainer = proxy != null && proxy.isConnected ? proxy : null;
if (controller == null && trainer == null) return const SizedBox.shrink();
final cs = Theme.of(context).colorScheme;
final muted = context.typography.xSmall.copyWith(color: cs.mutedForeground);
final strong = context.typography.xSmall.copyWith(color: cs.foreground, fontWeight: FontWeight.w500);
final battery = controller is BluetoothDevice ? controller.batteryLevel : null;
final appName = switch (core.settings.getTrainerApp()?.name) {
final name? => shownTrainerAppName(name),
null => null,
};
return Row(
key: const ValueKey('shell-device-chips'),
mainAxisSize: MainAxisSize.min,
spacing: 8,
children: [
if (controller != null)
_chip(
key: const ValueKey('shell-device-chip-controller'),
label: [controller.displayName(context), if (battery != null) '$battery%'].join(', '),
children: [
Icon(controller.icon, size: 14, color: cs.foreground),
Flexible(
child: Text(
controller.displayName(context),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: strong,
),
),
if (battery != null)
Text(
'$battery%',
style: muted.copyWith(color: battery < 20 ? cs.destructive : null),
),
],
),
if (trainer != null)
_chip(
key: const ValueKey('shell-device-chip-trainer'),
label: appName == null
? trainer.displayName(context)
: AppLocalizations.of(context).shellTrainerChipLabel(trainer.displayName(context), appName),
children: [
// A smart trainer as the drawn trainer; a power meter or
// HR strap bridged instead keeps its own glyph.
if (trainer.icon == LucideIcons.bike)
TrainerGlyph(size: 16, color: cs.foreground)
else
Icon(trainer.icon, size: 14, color: cs.foreground),
Flexible(
child: Text(
trainer.displayName(context),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: strong,
),
),
if (appName != null) ...[
Icon(LucideIcons.arrowRight, size: 12, color: cs.mutedForeground),
Text(appName, maxLines: 1, style: muted),
],
],
),
],
);
},
);
}
Widget _chip({required Key key, required String label, required List<Widget> children}) {
final cs = Theme.of(context).colorScheme;
return ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 240),
child: BkTappable(
key: key,
onPressed: _openDevices,
label: label,
excludeChildSemantics: true,
borderRadius: BorderRadius.circular(999),
child: Container(
constraints: const BoxConstraints(minHeight: 32),
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
decoration: BoxDecoration(color: cs.card, borderRadius: BorderRadius.circular(999)),
child: Row(mainAxisSize: MainAxisSize.min, spacing: 6, children: children),
),
),
);
}
}
/// [child] with [lead] before it, vertically centred — and [lead] dropped when
/// both do not fit the width, so [child] keeps its full width first.
///
/// A render box rather than a `LayoutBuilder` because the app bar measures
/// its title's intrinsic height, which a `LayoutBuilder` cannot report.
class _LeadUnlessTight extends MultiChildRenderObjectWidget {
_LeadUnlessTight({required Widget child, Widget? lead, required this.gap}) : super(children: [child, ?lead]);
final double gap;
@override
RenderObject createRenderObject(BuildContext context) => _RenderLeadUnlessTight(gap);
@override
void updateRenderObject(BuildContext context, _RenderLeadUnlessTight renderObject) => renderObject.gap = gap;
}
class _LeadUnlessTightParentData extends ContainerBoxParentData<RenderBox> {}
class _RenderLeadUnlessTight extends RenderBox
with
ContainerRenderObjectMixin<RenderBox, _LeadUnlessTightParentData>,
RenderBoxContainerDefaultsMixin<RenderBox, _LeadUnlessTightParentData> {
_RenderLeadUnlessTight(this._gap);
double _gap;
set gap(double value) {
if (value == _gap) return;
_gap = value;
markNeedsLayout();
}
bool _showLead = false;
RenderBox get _child => firstChild!;
RenderBox? get _lead => childAfter(firstChild!);
@override
void setupParentData(RenderBox child) {
if (child.parentData is! _LeadUnlessTightParentData) child.parentData = _LeadUnlessTightParentData();
}
@override
double computeMinIntrinsicWidth(double height) => _child.getMinIntrinsicWidth(height);
@override
double computeMaxIntrinsicWidth(double height) =>
_child.getMaxIntrinsicWidth(height) + (_lead == null ? 0 : _lead!.getMaxIntrinsicWidth(height) + _gap);
@override
double computeMinIntrinsicHeight(double width) => math.max(
_child.getMinIntrinsicHeight(width),
_lead?.getMinIntrinsicHeight(double.infinity) ?? 0,
);
@override
double computeMaxIntrinsicHeight(double width) => math.max(
_child.getMaxIntrinsicHeight(width),
_lead?.getMaxIntrinsicHeight(double.infinity) ?? 0,
);
@override
void performLayout() {
final lead = _lead;
final loose = constraints.loosen();
var leadWidth = 0.0;
var leadHeight = 0.0;
_showLead = false;
if (lead != null) {
lead.layout(loose, parentUsesSize: true);
final wanted = _child.getMaxIntrinsicWidth(double.infinity);
_showLead = lead.size.width + _gap + wanted <= constraints.maxWidth;
if (_showLead) {
leadWidth = lead.size.width + _gap;
leadHeight = lead.size.height;
}
}
_child.layout(
BoxConstraints(maxWidth: math.max(0, constraints.maxWidth - leadWidth), maxHeight: constraints.maxHeight),
parentUsesSize: true,
);
final height = math.max(leadHeight, _child.size.height);
size = constraints.constrain(Size(leadWidth + _child.size.width, height));
(_child.parentData! as _LeadUnlessTightParentData).offset = Offset(
leadWidth,
(size.height - _child.size.height) / 2,
);
if (lead != null) {
(lead.parentData! as _LeadUnlessTightParentData).offset = Offset(0, (size.height - lead.size.height) / 2);
}
}
@override
void paint(PaintingContext context, Offset offset) {
context.paintChild(_child, (_child.parentData! as _LeadUnlessTightParentData).offset + offset);
final lead = _lead;
if (lead != null && _showLead) {
context.paintChild(lead, (lead.parentData! as _LeadUnlessTightParentData).offset + offset);
}
}
@override
bool hitTestChildren(BoxHitTestResult result, {required Offset position}) {
final data = _child.parentData! as _LeadUnlessTightParentData;
return result.addWithPaintOffset(
offset: data.offset,
position: position,
hitTest: (result, transformed) => _child.hitTest(result, position: transformed),
);
}
@override
void visitChildrenForSemantics(RenderObjectVisitor visitor) {
visitor(_child);
if (_showLead && _lead != null) visitor(_lead!);
}
}