158 lines
5.0 KiB
Dart
158 lines
5.0 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../ui/adaptive/host.dart';
|
|
import 'destination.dart';
|
|
import 'shell_bar.dart';
|
|
|
|
/// Narrower than this, an open item is its own page; wider windows keep it
|
|
/// beside the destination rail.
|
|
const double kDetailPageMaxWidth = 600;
|
|
|
|
/// Keeps one full-screen detail page (an open run, pipeline or chat) in step
|
|
/// with a screen's selection: selecting pushes the page over the tabs, like a
|
|
/// new activity, and closing it (its Back, the system Back) clears the
|
|
/// selection. A selection cleared elsewhere (a route change) removes the page.
|
|
class DetailPageHost {
|
|
Route<void>? _route;
|
|
|
|
/// The owner's latest build of the page's content, so the open page
|
|
/// follows the owner's state (a renamed title, a newer reply).
|
|
final ValueNotifier<WidgetBuilder?> _content = ValueNotifier(null);
|
|
|
|
/// Whether the page is pushed (or about to be).
|
|
bool get open => _route != null;
|
|
|
|
void sync(
|
|
BuildContext context, {
|
|
required bool selected,
|
|
required AppDestination destination,
|
|
required WidgetBuilder builder,
|
|
required VoidCallback onClosed,
|
|
}) {
|
|
if (selected && _route != null) {
|
|
// Another route cannot be rebuilt mid-build; the page follows next frame.
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (_route != null) _content.value = builder;
|
|
});
|
|
}
|
|
if (selected && _route == null) {
|
|
_content.value = builder;
|
|
final route = detailPageRoute(
|
|
context,
|
|
destination: destination,
|
|
builder: (context) => ValueListenableBuilder<WidgetBuilder?>(
|
|
valueListenable: _content,
|
|
builder: (context, build, _) =>
|
|
build == null ? const SizedBox.shrink() : build(context),
|
|
),
|
|
);
|
|
_route = route;
|
|
// Pushing during a build is not allowed; the page follows this frame.
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!context.mounted || _route != route) return;
|
|
Navigator.of(context).push(route).then((_) {
|
|
if (_route != route) return;
|
|
_route = null;
|
|
onClosed();
|
|
});
|
|
});
|
|
} else if (!selected && _route != null) {
|
|
final route = _route!;
|
|
_route = null;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (route.isActive) route.navigator?.removeRoute(route);
|
|
});
|
|
}
|
|
}
|
|
|
|
/// Removes the page with its owner.
|
|
void dispose() {
|
|
// The notifier is left to the page: a page still animating out keeps
|
|
// listening to it until it is gone.
|
|
final route = _route;
|
|
_route = null;
|
|
if (route != null && route.isActive) route.navigator?.removeRoute(route);
|
|
}
|
|
}
|
|
|
|
/// A full-screen page whose bar shows what the page's content publishes
|
|
/// through [ShellBar] (title and actions), with the platform's Back.
|
|
Route<void> detailPageRoute(
|
|
BuildContext context, {
|
|
required AppDestination destination,
|
|
required WidgetBuilder builder,
|
|
}) {
|
|
final page = _DetailPage(destination: destination, builder: builder);
|
|
return usesCupertinoHost(context)
|
|
? CupertinoPageRoute<void>(builder: (_) => page)
|
|
: MaterialPageRoute<void>(builder: (_) => page);
|
|
}
|
|
|
|
class _DetailPage extends StatefulWidget {
|
|
const _DetailPage({required this.destination, required this.builder});
|
|
|
|
final AppDestination destination;
|
|
final WidgetBuilder builder;
|
|
|
|
@override
|
|
State<_DetailPage> createState() => _DetailPageState();
|
|
}
|
|
|
|
class _DetailPageState extends State<_DetailPage> {
|
|
final ShellBarRegistry _bar = ShellBarRegistry();
|
|
|
|
@override
|
|
void dispose() {
|
|
_bar.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _back() => Navigator.of(context).maybePop();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final body = ShellBarScope(
|
|
registry: _bar,
|
|
destination: widget.destination,
|
|
child: Builder(builder: widget.builder),
|
|
);
|
|
return ListenableBuilder(
|
|
listenable: _bar,
|
|
builder: (context, _) {
|
|
final spec = _bar.specFor(widget.destination);
|
|
final title = spec?.title ?? '';
|
|
final actions = spec?.actions ?? const <Widget>[];
|
|
if (usesCupertinoHost(context)) {
|
|
return CupertinoPageScaffold(
|
|
navigationBar: CupertinoNavigationBar(
|
|
leading: CupertinoNavigationBarBackButton(
|
|
key: const ValueKey('shell.back'),
|
|
onPressed: _back,
|
|
),
|
|
automaticallyImplyLeading: false,
|
|
middle: Text(title),
|
|
trailing: actions.isEmpty
|
|
? null
|
|
: Row(mainAxisSize: MainAxisSize.min, children: actions),
|
|
),
|
|
child: SafeArea(child: body),
|
|
);
|
|
}
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
automaticallyImplyLeading: false,
|
|
leading: BackButton(
|
|
key: const ValueKey('shell.back'),
|
|
onPressed: _back,
|
|
),
|
|
title: Text(title),
|
|
actions: actions,
|
|
),
|
|
body: SafeArea(top: false, child: body),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|