Keep Files breadcrumbs aligned to the current folder

Pin the horizontally scrolling breadcrumb to its trailing edge so deep paths keep the current folder visible. Preserve tappable navigation, middle-ellipsize long labels, and cover the behavior with focused widget tests and the rendered fixture.
This commit is contained in:
2026-11-12 23:08:03 +00:00
parent d19a14b9f9
commit d56a76c9a7
4 changed files with 227 additions and 17 deletions
+116 -13
View File
@@ -97,19 +97,11 @@ class FilesHeader extends StatelessWidget {
],
),
if (listing != null)
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
for (final crumb in folderCrumbs(listing.home, listing.path))
TextButton(
onPressed: controller.loading
? null
: () => controller.open(crumb.path),
child: Text(crumb.label),
),
],
),
_FilesBreadcrumbs(
home: listing.home,
path: listing.path,
loading: controller.loading,
onOpen: controller.open,
),
Row(
children: [
@@ -142,3 +134,114 @@ class FilesHeader extends StatelessWidget {
);
}
}
class _FilesBreadcrumbs extends StatefulWidget {
const _FilesBreadcrumbs({
required this.home,
required this.path,
required this.loading,
required this.onOpen,
});
final String home;
final String path;
final bool loading;
final ValueChanged<String> onOpen;
@override
State<_FilesBreadcrumbs> createState() => _FilesBreadcrumbsState();
}
class _FilesBreadcrumbsState extends State<_FilesBreadcrumbs> {
final ScrollController _scroll = ScrollController();
double? _lastWidth;
@override
void initState() {
super.initState();
_pinToEnd();
}
@override
void didUpdateWidget(covariant _FilesBreadcrumbs oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.path != widget.path) _pinToEnd();
}
void _pinToEnd() {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted && _scroll.hasClients) {
_scroll.jumpTo(_scroll.position.maxScrollExtent);
}
});
}
@override
void dispose() {
_scroll.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final crumbs = folderCrumbs(widget.home, widget.path);
return LayoutBuilder(
builder: (context, constraints) {
final surface = Theme.of(context).colorScheme.surface;
if (_lastWidth != constraints.maxWidth) {
_lastWidth = constraints.maxWidth;
_pinToEnd();
}
return SizedBox(
width: constraints.maxWidth,
height: kMinInteractiveDimension,
child: Stack(
children: [
SingleChildScrollView(
key: const Key('files-breadcrumb-scroll'),
controller: _scroll,
scrollDirection: Axis.horizontal,
child: Row(
children: [
for (var index = 0; index < crumbs.length; index++)
TextButton(
onPressed: widget.loading
? null
: () => widget.onOpen(crumbs[index].path),
child: Text(
_middleEllipsis(crumbs[index].label),
overflow: TextOverflow.ellipsis,
),
),
],
),
),
IgnorePointer(
child: Align(
alignment: Alignment.centerLeft,
child: Container(
width: 20,
height: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [surface, surface.withValues(alpha: 0)],
),
),
),
),
),
],
),
);
},
);
}
}
String _middleEllipsis(String label, {int maxCharacters = 36}) {
if (label.length <= maxCharacters) return label;
final visible = maxCharacters - 1;
final leading = (visible + 1) ~/ 2;
final trailing = visible - leading;
return '${label.substring(0, leading)}…${label.substring(label.length - trailing)}';
}