Files
alan 3933ff980c Remove Cupertino settings branches
Keep the Material settings implementation while preserving public Cupertino parameters for later cleanup. Narrow settings tests to the Material-only behavior.
2026-11-11 05:21:30 +00:00

171 lines
4.9 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'settings_topic_store.dart';
import '../../core/storage/ordered_local_storage.dart';
import '../../ui/motion/surface_visibility.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,
this.navigationScope,
this.selectionStore,
});
final Map<SettingsTopic, WidgetBuilder> pages;
/// Null keeps standalone previews independent of application persistence.
final String? navigationScope;
final SettingsTopicStore? selectionStore;
@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};
SettingsTopicStore? _store;
int _revision = 0;
bool _restoring = false;
@override
void initState() {
super.initState();
_tabs = TabController(length: SettingsTopic.values.length, vsync: this);
_tabs.addListener(_changed);
_bindStore();
}
@override
void didUpdateWidget(SettingsTopicTabs oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.navigationScope != widget.navigationScope ||
oldWidget.selectionStore != widget.selectionStore) {
_bindStore();
}
}
void _bindStore() {
_store =
widget.selectionStore ??
(widget.navigationScope == null
? null
: SharedPreferencesSettingsTopicStore(widget.navigationScope!));
final revision = ++_revision;
final store = _store;
if (store == null) return;
unawaited(_restore(store, revision));
}
Future<void> _restore(SettingsTopicStore store, int revision) async {
String? saved;
try {
saved = await _ordered(store, store.load);
} catch (_) {
return;
}
if (!mounted || revision != _revision) return;
final topic =
SettingsTopic.values
.where((topic) => topic.name == saved)
.firstOrNull ??
SettingsTopic.appearance;
_restoring = true;
_tabs.index = topic.index;
_restoring = false;
}
Future<T> _ordered<T>(
SettingsTopicStore store,
Future<T> Function() operation,
) => store is SharedPreferencesSettingsTopicStore
? operation()
: orderedLocalStorage(store, operation);
void _persist(SettingsTopicStore store, String topic) {
// Register accepted choices synchronously, before a replacement can load.
// The production store coordinates all instances by their preference key.
unawaited(
_ordered(store, () => store.save(topic)).catchError((Object _) {
// Navigation continues when the local preference cannot be saved.
}),
);
}
void _changed() {
if (!mounted) return;
final next = SettingsTopic.values[_tabs.index];
if (next == _selected) return;
setState(() {
_selected = next;
_visited.add(next);
});
if (_restoring) return;
++_revision;
final store = _store;
if (store != null) _persist(store, next.name);
}
@override
void dispose() {
_tabs.removeListener(_changed);
_tabs.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final tabs = TabBar(
controller: _tabs,
isScrollable: true,
tabAlignment: TabAlignment.start,
tabs: [for (final topic in SettingsTopic.values) Tab(text: topic.label)],
);
return 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: SurfaceVisibility(
shown: true,
current: topic == _selected,
child: KeyedSubtree(
key: ValueKey('settings-topic-${topic.name}'),
child: Builder(builder: widget.pages[topic]!),
),
),
)
else
const SizedBox.shrink(),
],
),
),
],
),
);
}
}