import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../ui/theme/app_font.dart'; import '../../ui/theme/app_font_controller.dart'; import 'settings_group.dart'; import 'settings_row.dart'; import 'settings_theme.dart'; const fontSectionTitle = 'Font'; const fontSectionFooter = 'Saved on this device only. Code keeps its monospace font.'; const fontLoadingText = 'Loading…'; const fontLoadFailedText = 'Could not load this font.'; const fontSaveFailedText = 'Not saved on this device; it applies until reload.'; ValueKey settingsFontKey(AppFont font) => ValueKey('settings-font-${font.id}'); /// The device's interface font: one row per option, each named in its own /// face. Material rows are radio tiles, Cupertino rows carry the checkmark. /// A choice is stored locally and applies as soon as its files load. class SettingsFontSection extends StatefulWidget { const SettingsFontSection({super.key, required this.controller}); final AppFontController controller; @override State createState() => _SettingsFontSectionState(); } class _SettingsFontSectionState extends State { @override void initState() { super.initState(); _preview(); } @override void didUpdateWidget(SettingsFontSection oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.controller != widget.controller) _preview(); } /// Only the regular weight of each option, only once Settings is shown. void _preview() { for (final font in AppFont.values) { unawaited(widget.controller.preview(font)); } } @override Widget build(BuildContext context) => ListenableBuilder( listenable: widget.controller, builder: (context, _) => _group(context), ); Widget _group(BuildContext context) { final controller = widget.controller; final cupertino = usesCupertinoControls(context); final group = SettingsGroup( title: fontSectionTitle, footer: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const Text(fontSectionFooter), if (controller.saveFailed) Text( fontSaveFailedText, style: TextStyle(color: settingsErrorColor(context)), ), ], ), children: [ for (final font in AppFont.values) cupertino ? _cupertinoRow(context, font) : RadioListTile( key: settingsFontKey(font), value: font, title: _name(context, font), subtitle: _status(context, font), ), ], ); if (cupertino) return group; return RadioGroup( groupValue: controller.selected, onChanged: (font) { if (font != null) unawaited(controller.select(font)); }, child: group, ); } Widget _cupertinoRow(BuildContext context, AppFont font) => SettingsRow( key: settingsFontKey(font), title: _name(context, font), subtitle: _status(context, font), trailing: widget.controller.selected == font ? Icon( CupertinoIcons.check_mark, color: CupertinoTheme.of(context).primaryColor, ) : null, onTap: () => unawaited(widget.controller.select(font)), ); /// The option's name in its own face once its regular weight has loaded; /// the system face until then (and always for the system option). Widget _name(BuildContext context, AppFont font) { final family = widget.controller.canPreview(font) ? font.family ?? _systemFamily(context) : _systemFamily(context); return Text(font.label, style: TextStyle(fontFamily: family)); } Widget? _status(BuildContext context, AppFont font) { final controller = widget.controller; if (controller.selected != font) return null; if (controller.loadFailed == font) { return Text( fontLoadFailedText, style: TextStyle(color: settingsErrorColor(context)), ); } return controller.loading ? const Text(fontLoadingText) : null; } } /// The platform's own interface family (Roboto, or the Cupertino system /// face), independent of the font the theme currently applies. String? _systemFamily(BuildContext context) => usesCupertinoControls(context) ? const CupertinoTextThemeData().textStyle.fontFamily : Typography.material2021( platform: Theme.of(context).platform, ).black.bodyLarge?.fontFamily;