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.
139 lines
3.9 KiB
Dart
139 lines
3.9 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'settings_layout.dart';
|
|
import 'settings_server_load.dart';
|
|
import 'settings_theme.dart';
|
|
|
|
String _describeDefault(Object error) => '$error';
|
|
|
|
/// Shows a [ServerLoad] inside its section: the last value (with any refresh
|
|
/// error and a Retry beside it), a placeholder while the first load runs, or
|
|
/// the error with Retry. Other sections are never affected by this one.
|
|
///
|
|
/// The view starts the first load when it mounts, which happens only once the
|
|
/// page reaches its section; a cached value is never refetched by a rebuild.
|
|
class ServerSettingsView<T> extends StatefulWidget {
|
|
const ServerSettingsView({
|
|
super.key,
|
|
required this.load,
|
|
required this.builder,
|
|
this.describe = _describeDefault,
|
|
this.retryKey,
|
|
});
|
|
|
|
final ServerLoad<T> load;
|
|
final Widget Function(BuildContext context, T value) builder;
|
|
|
|
/// Turns a failure into the line the operator reads.
|
|
final String Function(Object error) describe;
|
|
final Key? retryKey;
|
|
|
|
@override
|
|
State<ServerSettingsView<T>> createState() => _ServerSettingsViewState<T>();
|
|
}
|
|
|
|
class _ServerSettingsViewState<T> extends State<ServerSettingsView<T>> {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_startIfNeeded();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(ServerSettingsView<T> oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.load != widget.load) _startIfNeeded();
|
|
}
|
|
|
|
void _startIfNeeded() {
|
|
final load = widget.load;
|
|
if (!load.hasValue && !load.loading && load.error == null) {
|
|
unawaited(load.load());
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final load = widget.load;
|
|
return ListenableBuilder(
|
|
listenable: load,
|
|
builder: (context, _) => _content(context, load),
|
|
);
|
|
}
|
|
|
|
Widget _content(BuildContext context, ServerLoad<T> load) {
|
|
final error = load.error;
|
|
if (load.hasValue) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
widget.builder(context, load.value as T),
|
|
if (error != null) _error(context, error),
|
|
],
|
|
);
|
|
}
|
|
if (error != null) return _error(context, error);
|
|
return const SettingsPlaceholder();
|
|
}
|
|
|
|
Widget _error(BuildContext context, Object error) => Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
Semantics(
|
|
liveRegion: true,
|
|
child: Text(
|
|
widget.describe(error),
|
|
style: settingsTextStyle(context, error: true),
|
|
),
|
|
),
|
|
Align(
|
|
alignment: AlignmentDirectional.centerStart,
|
|
child: TextButton(
|
|
key: widget.retryKey,
|
|
onPressed: widget.load.loading ? null : () => widget.load.refresh(),
|
|
child: const Text('Retry'),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
/// A loading placeholder for a value that has not arrived: two text-height
|
|
/// bars in the surface colour, sized from the text scale.
|
|
class SettingsPlaceholder extends StatelessWidget {
|
|
const SettingsPlaceholder({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
final scaler = MediaQuery.textScalerOf(context);
|
|
final line = scaler.scale(theme.textTheme.bodyMedium?.fontSize ?? 14);
|
|
final color = theme.colorScheme.surfaceContainerHighest;
|
|
Widget bar(double widthFactor) => FractionallySizedBox(
|
|
alignment: AlignmentDirectional.centerStart,
|
|
widthFactor: widthFactor,
|
|
child: Container(
|
|
height: line,
|
|
decoration: BoxDecoration(
|
|
color: color,
|
|
borderRadius: BorderRadius.circular(settingsFieldGap),
|
|
),
|
|
),
|
|
);
|
|
return Semantics(
|
|
label: 'Loading',
|
|
excludeSemantics: true,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
bar(0.9),
|
|
const SizedBox(height: settingsTitleGap),
|
|
bar(0.6),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|