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 pages; @override State createState() => _SettingsTopicTabsState(); } class _SettingsTopicTabsState extends State with SingleTickerProviderStateMixin { late final TabController _tabs; SettingsTopic _selected = SettingsTopic.appearance; final Set _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( 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; } }