3136e5bb7e
Replace the tabbed Settings flow with a single grouped page that searches and lazily loads server-backed sections. Preserve existing settings behavior while adding inline loading and retry states and updating coverage for the new navigation model.
227 lines
7.0 KiB
Dart
227 lines
7.0 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'settings_layout.dart';
|
|
import 'settings_page_chrome.dart';
|
|
import 'settings_section.dart';
|
|
import 'settings_skeleton.dart';
|
|
|
|
/// The one Settings page: a pinned search field over a scrolling column of
|
|
/// grouped sections, with a jump index beside it on wide layouts.
|
|
///
|
|
/// Local sections render at once. A [SettingsSection.lazy] section shows a
|
|
/// skeleton until the list scrolls within a viewport of it or a search matches
|
|
/// it; once reached it stays mounted, so its loaded value survives scrolling.
|
|
class SettingsSearchPage extends StatefulWidget {
|
|
const SettingsSearchPage({super.key, required this.sections});
|
|
|
|
final List<SettingsSection> sections;
|
|
|
|
@override
|
|
State<SettingsSearchPage> createState() => _SettingsSearchPageState();
|
|
}
|
|
|
|
class _SettingsSearchPageState extends State<SettingsSearchPage> {
|
|
final TextEditingController _search = TextEditingController();
|
|
final ScrollController _scroll = ScrollController();
|
|
final GlobalKey _viewport = GlobalKey();
|
|
final Map<String, GlobalKey> _keys = {};
|
|
final Set<String> _activated = {};
|
|
String _query = '';
|
|
bool _activationScheduled = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_scheduleActivation();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_search.dispose();
|
|
_scroll.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
bool get _searching => _query.trim().isNotEmpty;
|
|
|
|
List<SettingsSection> get _visible => [
|
|
for (final section in widget.sections)
|
|
if (section.matches(_query)) section,
|
|
];
|
|
|
|
GlobalKey _keyFor(String id) => _keys.putIfAbsent(id, GlobalKey.new);
|
|
|
|
bool _isActive(SettingsSection section) =>
|
|
!section.lazy || _activated.contains(section.id) || _searching;
|
|
|
|
void _onQueryChanged(String value) {
|
|
setState(() {
|
|
_query = value;
|
|
if (_searching) {
|
|
_activated.addAll([
|
|
for (final section in _visible)
|
|
if (section.lazy) section.id,
|
|
]);
|
|
}
|
|
});
|
|
_scheduleActivation(jumpToFirstMatch: true);
|
|
}
|
|
|
|
/// Activation and jumps run after the frame: they read layout, and a scroll
|
|
/// notification may arrive during layout.
|
|
void _scheduleActivation({bool jumpToFirstMatch = false}) {
|
|
if (_activationScheduled) return;
|
|
_activationScheduled = true;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_activationScheduled = false;
|
|
if (!mounted) return;
|
|
if (jumpToFirstMatch) _jumpForQuery();
|
|
_activateNear();
|
|
});
|
|
}
|
|
|
|
void _jumpForQuery() {
|
|
if (_searching) {
|
|
final first = _visible.firstOrNull;
|
|
if (first != null) _jumpTo(first.id);
|
|
} else if (_scroll.hasClients) {
|
|
unawaited(
|
|
_scroll.animateTo(
|
|
0,
|
|
duration: const Duration(milliseconds: 200),
|
|
curve: Curves.easeOut,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
void _jumpTo(String id) {
|
|
final context = _keys[id]?.currentContext;
|
|
if (context == null) return;
|
|
unawaited(
|
|
Scrollable.ensureVisible(
|
|
context,
|
|
duration: const Duration(milliseconds: 250),
|
|
curve: Curves.easeOut,
|
|
),
|
|
);
|
|
}
|
|
|
|
void _activateNear() {
|
|
final viewport = _viewport.currentContext?.findRenderObject();
|
|
if (viewport is! RenderBox || !viewport.hasSize) return;
|
|
final top = viewport.localToGlobal(Offset.zero).dy;
|
|
final limit = top + viewport.size.height * (1 + settingsPreloadViewports);
|
|
var changed = false;
|
|
for (final section in widget.sections) {
|
|
if (!section.lazy || _activated.contains(section.id)) continue;
|
|
final box = _keys[section.id]?.currentContext?.findRenderObject();
|
|
if (box is! RenderBox || !box.hasSize || !box.attached) continue;
|
|
if (box.localToGlobal(Offset.zero).dy <= limit) {
|
|
_activated.add(section.id);
|
|
changed = true;
|
|
}
|
|
}
|
|
if (changed) setState(() {});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final insets = MediaQuery.paddingOf(context);
|
|
final visible = _visible;
|
|
return LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final wide = constraints.maxWidth >= settingsIndexMinWidth;
|
|
return Row(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: Padding(
|
|
padding: EdgeInsetsDirectional.fromSTEB(
|
|
insets.left + settingsPageSidePadding,
|
|
insets.top + settingsPageTopPadding,
|
|
insets.right + settingsPageSidePadding,
|
|
0,
|
|
),
|
|
child: Align(
|
|
alignment: Alignment.topCenter,
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(
|
|
maxWidth: settingsContentMaxWidth,
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
SettingsSearchField(
|
|
controller: _search,
|
|
onChanged: _onQueryChanged,
|
|
),
|
|
const SizedBox(height: settingsSectionGap),
|
|
Expanded(child: _list(visible, insets)),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
if (wide && visible.isNotEmpty)
|
|
SizedBox(
|
|
width: settingsIndexWidth,
|
|
child: Padding(
|
|
padding: EdgeInsetsDirectional.only(
|
|
top: insets.top + settingsPageTopPadding,
|
|
),
|
|
child: SettingsSectionIndex(
|
|
sections: visible,
|
|
onJump: _jumpTo,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _list(List<SettingsSection> visible, EdgeInsets insets) {
|
|
if (visible.isEmpty) {
|
|
return Padding(
|
|
padding: const EdgeInsets.all(settingsRowInset),
|
|
child: Text('No settings match "${_search.text.trim()}".'),
|
|
);
|
|
}
|
|
return NotificationListener<ScrollNotification>(
|
|
onNotification: (_) {
|
|
_scheduleActivation();
|
|
return false;
|
|
},
|
|
child: KeyedSubtree(
|
|
key: _viewport,
|
|
child: SingleChildScrollView(
|
|
key: const ValueKey<String>('settings.scroll'),
|
|
controller: _scroll,
|
|
padding: EdgeInsets.only(
|
|
bottom: insets.bottom + settingsPageBottomPadding,
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
for (final (index, section) in visible.indexed) ...<Widget>[
|
|
if (index > 0) const SizedBox(height: settingsSectionGap),
|
|
KeyedSubtree(
|
|
key: _keyFor(section.id),
|
|
child: _isActive(section)
|
|
? Builder(builder: section.builder)
|
|
: SettingsSkeleton(title: section.title),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|