7ff012880b
Provide composable shown/current visibility for retained surfaces and migrate visibility readers away from TickerMode. This keeps ticker muting separate from painted and current-surface state.
205 lines
6.1 KiB
Dart
205 lines
6.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'settings_layout.dart';
|
|
import 'settings_theme.dart';
|
|
import 'settings_page_body.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 = 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: SurfaceVisibility(
|
|
shown: true,
|
|
current: 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;
|
|
}
|
|
}
|