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 sections; @override State createState() => _SettingsSearchPageState(); } class _SettingsSearchPageState extends State { final TextEditingController _search = TextEditingController(); final ScrollController _scroll = ScrollController(); final GlobalKey _viewport = GlobalKey(); final Map _keys = {}; final Set _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 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: [ 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: [ 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 visible, EdgeInsets insets) { if (visible.isEmpty) { return Padding( padding: const EdgeInsets.all(settingsRowInset), child: Text('No settings match "${_search.text.trim()}".'), ); } return NotificationListener( onNotification: (_) { _scheduleActivation(); return false; }, child: KeyedSubtree( key: _viewport, child: SingleChildScrollView( key: const ValueKey('settings.scroll'), controller: _scroll, padding: EdgeInsets.only( bottom: insets.bottom + settingsPageBottomPadding, ), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ for (final (index, section) in visible.indexed) ...[ if (index > 0) const SizedBox(height: settingsSectionGap), KeyedSubtree( key: _keyFor(section.id), child: _isActive(section) ? Builder(builder: section.builder) : SettingsSkeleton(title: section.title), ), ], ], ), ), ), ); } }