Organize settings into retained topic tabs

This commit is contained in:
2026-11-07 19:36:02 +00:00
parent 6b654693d9
commit fc4aa1652f
11 changed files with 434 additions and 101 deletions
+4 -83
View File
@@ -18,6 +18,7 @@ import 'settings_group.dart';
import 'storage/settings_storage_section.dart';
import 'health/settings_health_section.dart';
import 'settings_page_body.dart';
import 'settings_tabs.dart';
import 'settings_notifications_section.dart';
import 'settings_ntfy_section.dart';
import 'settings_device_delivery_section.dart';
@@ -35,6 +36,8 @@ import 'server_connections.dart';
import 'saved_servers_section.dart';
import 'managed_connection_section.dart';
part 'settings_topic_pages.dart';
/// The Settings destination: the server connection and the server-side
/// workspace, in native Material (native platforms) or Cupertino (iOS) controls.
///
@@ -169,83 +172,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
@override
Widget build(BuildContext context) {
final servers = widget.servers;
final content = SettingsPageBody(
children: <Widget>[
if (servers != null) ...<Widget>[
SavedServersSection(connections: servers),
ManagedConnectionSection(
client: widget.client,
controller: _controller,
),
] else
SettingsConnectionSection(
controller: _controller,
serverUrlField: _serverUrlField,
),
// A phone signs in at the web login; a browser has its own.
if (widget.showSignIn)
_single(
'Sign in',
SettingsSignInSection(
endpoint: _endpoint,
onChanged: _reconnectAfterSignIn,
),
),
// The web page links the Android app the server offers.
if (widget.showAndroidDownload)
_single('Android app', const SettingsAndroidDownloadSection()),
// A phone installs newer builds from the server.
if (widget.showSignIn && appUpdatesSupported)
_single('App updates', SettingsUpdatesSection(endpoint: _endpoint)),
SettingsWorkspaceSection(
controller: _controller,
workspaceField: _workspaceField,
),
_single(
'Data management',
SettingsStorageSection(gateway: widget.client),
),
_single('Server', SettingsHealthSection(gateway: widget.client)),
// A device-local choice: a browser and a phone keep their own.
SettingsAnimationSection(
controller:
widget.animationSpeed ?? AnimationSpeedController.instance,
),
SettingsFontSection(
controller: widget.fonts ?? AppFontController.instance,
),
if (widget.notifications case final notifications?)
SettingsNotificationsSection(controller: notifications),
if (widget.deliveryService case final service? when service.supported)
SettingsDeviceDeliverySection(
bridge: service,
controller: widget.deliveryServiceController,
requestNotificationPermission: widget.notifications == null
? null
: () async {
await widget.notifications!.requestPermission();
},
),
// The server's ntfy choices, beside the in-app permission.
SettingsNtfySection(
gateway: widget.client,
connected:
widget.client.state.kind == transport.ConnectionKind.connected,
),
if (widget.onOpenSchedules != null || widget.onOpenTemplates != null)
SettingsGroup(
title: 'Library',
leadingRows: true,
children: <Widget>[
if (widget.onOpenSchedules case final open?)
SettingsSchedulesSection(onOpen: open),
if (widget.onOpenTemplates case final open?)
SettingsTemplatesSection(onOpen: open),
],
),
],
);
final content = SettingsTopicTabs(pages: _topicPages());
if (widget.embedded) {
// Under the shell's bar the Cupertino page is the grouped background
// right up to the top edge: the bar's scrim takes the same colour, so
@@ -271,10 +198,4 @@ class _SettingsScreenState extends State<SettingsScreen> {
body: content,
);
}
/// A group holding one free-form block (a form, a status with buttons).
Widget _single(String title, Widget child) => SettingsGroup(
title: title,
children: <Widget>[SettingsGroupItem(child: child)],
);
}
@@ -0,0 +1,121 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'settings_layout.dart';
import 'settings_theme.dart';
import 'settings_page_body.dart';
enum SettingsTopic {
appearance('Appearance'),
connection('Connection'),
notifications('Notifications'),
tools('Tools'),
updates('Updates');
const SettingsTopic(this.label);
final String label;
}
/// Topic pages are created on first visit, then retained with their drafts and
/// independent scroll positions. Hidden pages do not tick animations.
class SettingsTopicTabs extends StatefulWidget {
const SettingsTopicTabs({super.key, required this.pages});
final Map<SettingsTopic, WidgetBuilder> pages;
@override
State<SettingsTopicTabs> createState() => _SettingsTopicTabsState();
}
class _SettingsTopicTabsState extends State<SettingsTopicTabs>
with SingleTickerProviderStateMixin {
late final TabController _tabs;
SettingsTopic _selected = SettingsTopic.appearance;
final Set<SettingsTopic> _visited = {SettingsTopic.appearance};
@override
void initState() {
super.initState();
_tabs = TabController(length: SettingsTopic.values.length, vsync: this);
_tabs.addListener(_changed);
}
void _changed() {
final next = SettingsTopic.values[_tabs.index];
if (next == _selected) return;
setState(() {
_selected = next;
_visited.add(next);
});
}
@override
void dispose() {
_tabs.removeListener(_changed);
_tabs.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final tabs = usesCupertinoControls(context)
? SingleChildScrollView(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.symmetric(
horizontal: settingsPageSidePadding,
),
child: CupertinoSlidingSegmentedControl<SettingsTopic>(
groupValue: _selected,
children: {
for (final topic in SettingsTopic.values)
topic: Padding(
padding: const EdgeInsets.symmetric(
horizontal: settingsControlGap,
),
child: Text(topic.label),
),
},
onValueChanged: (topic) {
if (topic != null) _tabs.index = topic.index;
},
),
)
: TabBar(
controller: _tabs,
isScrollable: true,
tabAlignment: TabAlignment.start,
tabs: [
for (final topic in SettingsTopic.values) Tab(text: topic.label),
],
);
final body = SafeArea(
bottom: false,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Semantics(container: true, label: 'Settings categories', child: tabs),
Expanded(
child: IndexedStack(
index: _selected.index,
children: [
for (final topic in SettingsTopic.values)
if (_visited.contains(topic))
TickerMode(
enabled: topic == _selected,
child: KeyedSubtree(
key: ValueKey('settings-topic-${topic.name}'),
child: Builder(builder: widget.pages[topic]!),
),
)
else
const SizedBox.shrink(),
],
),
),
],
),
);
return usesCupertinoControls(context)
? ColoredBox(
color: settingsCupertinoBackground.resolveFrom(context),
child: body,
)
: body;
}
}
@@ -0,0 +1,110 @@
part of 'settings_screen.dart';
extension _SettingsTopicPages on _SettingsScreenState {
Map<SettingsTopic, WidgetBuilder> _topicPages() {
final servers = widget.servers;
return {
SettingsTopic.appearance: (_) => SettingsPageBody(
children: [
SettingsAnimationSection(
controller:
widget.animationSpeed ?? AnimationSpeedController.instance,
),
SettingsFontSection(
controller: widget.fonts ?? AppFontController.instance,
),
],
),
SettingsTopic.connection: (_) => SettingsPageBody(
children: [
if (servers != null) ...[
SavedServersSection(connections: servers),
ManagedConnectionSection(
client: widget.client,
controller: _controller,
),
] else
SettingsConnectionSection(
controller: _controller,
serverUrlField: _serverUrlField,
),
if (widget.showSignIn)
_single(
'Sign in',
SettingsSignInSection(
endpoint: _endpoint,
onChanged: _reconnectAfterSignIn,
),
),
SettingsWorkspaceSection(
controller: _controller,
workspaceField: _workspaceField,
),
],
),
SettingsTopic.notifications: (_) => SettingsPageBody(
children: [
if (widget.notifications case final notifications?)
SettingsNotificationsSection(controller: notifications),
if (widget.deliveryService case final service? when service.supported)
SettingsDeviceDeliverySection(
bridge: service,
controller: widget.deliveryServiceController,
requestNotificationPermission: widget.notifications == null
? null
: () async {
await widget.notifications!.requestPermission();
},
),
SettingsNtfySection(
gateway: widget.client,
connected:
widget.client.state.kind == transport.ConnectionKind.connected,
),
],
),
SettingsTopic.tools: (_) => SettingsPageBody(
children: [
_single(
'Data management',
SettingsStorageSection(gateway: widget.client),
),
_single('Server', SettingsHealthSection(gateway: widget.client)),
if (widget.onOpenSchedules != null || widget.onOpenTemplates != null)
SettingsGroup(
title: 'Library',
leadingRows: true,
children: [
if (widget.onOpenSchedules case final open?)
SettingsSchedulesSection(onOpen: open),
if (widget.onOpenTemplates case final open?)
SettingsTemplatesSection(onOpen: open),
],
),
],
),
SettingsTopic.updates: (_) => SettingsPageBody(
children: [
if (widget.showAndroidDownload)
_single('Android app', const SettingsAndroidDownloadSection()),
if (widget.showSignIn && appUpdatesSupported)
_single('App updates', SettingsUpdatesSection(endpoint: _endpoint)),
if (!widget.showAndroidDownload &&
!(widget.showSignIn && appUpdatesSupported))
_single(
'App updates',
const Text(
'This installation is updated by its server administrator.',
),
),
],
),
};
}
/// A group holding one free-form block (a form, a status with buttons).
Widget _single(String title, Widget child) => SettingsGroup(
title: title,
children: <Widget>[SettingsGroupItem(child: child)],
);
}