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 '../../ui/theme/custom_fonts/custom_font.dart'; import '../../ui/theme/custom_fonts/font_file_picker.dart'; import '../../ui/theme/font_choice.dart'; import 'settings_custom_font_actions.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. ' 'Added fonts: .ttf or .otf, up to 10 MB each.'; const fontLoadingText = 'Loading…'; const fontLoadFailedText = 'Could not load this font.'; const fontSaveFailedText = 'Not saved on this device; it applies until reload.'; ValueKey settingsFontKey(FontChoice font) => ValueKey('settings-font-${font.id}'); const ValueKey settingsAddFontKey = ValueKey( 'settings-font-add', ); /// The device's interface font: one row per option, each named in its own /// face, the user's added fonts below the bundled ones, then "Add font…". /// 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, this.pickFiles = pickFontFiles, }); final AppFontController controller; /// The host file picker; tests substitute chosen files. final FontFilePicker pickFiles; @override State createState() => _SettingsFontSectionState(); } class _SettingsFontSectionState extends State { bool _adding = false; @override void initState() { super.initState(); unawaited(_preview()); } @override void didUpdateWidget(SettingsFontSection oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.controller != widget.controller) unawaited(_preview()); } /// Only the regular weight of each option, only once Settings is shown. Future _preview() async { final controller = widget.controller; for (final font in AppFont.values) { unawaited(controller.preview(font)); } await controller.loadCustomFonts(); for (final font in controller.customFonts) { unawaited(controller.preview(font)); } } Future _add() async { try { await addCustomFont( context, widget.controller, widget.pickFiles, onPicked: () { if (mounted) setState(() => _adding = true); }, ); } finally { if (mounted) setState(() => _adding = false); } } @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 options = [...AppFont.values, ...controller.customFonts]; 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 options) cupertino ? _cupertinoRow(context, font) : _materialRow(context, font), // Its own node: a bare list tile would lend its label and tap to the // section around it. Semantics( container: true, child: SettingsRow( key: settingsAddFontKey, action: true, enabled: !_adding, leading: Icon(cupertino ? CupertinoIcons.add : Icons.add), title: const Text(addCustomFontText), subtitle: _adding ? const Text(addingCustomFontText) : null, onTap: () => unawaited(_add()), ), ), ], ); if (cupertino) return group; return RadioGroup( groupValue: controller.selected, onChanged: (font) { if (font != null) unawaited(controller.select(font)); }, child: group, ); } Widget _materialRow(BuildContext context, FontChoice font) { final tile = RadioListTile( key: settingsFontKey(font), value: font, title: _name(context, font), subtitle: _status(context, font), ); if (font is! CustomFont) return tile; // The options button sits beside the tile rather than in its // `secondary` slot: the tile merges its children's semantics, which // would leave the button unreachable on its own. return Row( children: [ Expanded(child: tile), Padding( padding: const EdgeInsetsDirectional.only(end: 8), child: CustomFontMenu(fonts: widget.controller, font: font), ), ], ); } Widget _cupertinoRow(BuildContext context, FontChoice font) { final check = widget.controller.selected == font ? Icon( CupertinoIcons.check_mark, color: CupertinoTheme.of(context).primaryColor, ) : null; // One node per option, announced as selected. final row = Semantics( container: true, selected: check != null, child: SettingsRow( key: settingsFontKey(font), title: _name(context, font), subtitle: _status(context, font), trailing: check, onTap: () => unawaited(widget.controller.select(font)), ), ); if (font is! CustomFont) return row; // Beside the row, not in it: the list tile merges its children's // semantics (see [_materialRow]). return Row( children: [ Expanded(child: row), Padding( padding: const EdgeInsetsDirectional.only(end: 12), child: CustomFontMenu(fonts: widget.controller, font: 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, FontChoice font) { final family = widget.controller.canPreview(font) ? font.family ?? _systemFamily(context) : _systemFamily(context); return Text(font.label, style: TextStyle(fontFamily: family)); } /// Loading or failure for the chosen option; an added font otherwise /// lists its styles. Widget? _status(BuildContext context, FontChoice font) { final controller = widget.controller; if (controller.selected == font) { if (controller.loadFailed == font) { return Text( fontLoadFailedText, style: TextStyle(color: settingsErrorColor(context)), ); } if (controller.loading) return const Text(fontLoadingText); } return font is CustomFont ? Text(font.styles) : 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;