mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-10-21 23:11:45 +02:00
73 lines
2.4 KiB
Dart
73 lines
2.4 KiB
Dart
import 'package:file_picker/file_picker.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:spotiflac_android/l10n/l10n.dart';
|
|
import 'package:spotiflac_android/providers/theme_provider.dart';
|
|
import 'package:spotiflac_android/services/custom_font_service.dart';
|
|
|
|
class CustomFontSetting extends ConsumerStatefulWidget {
|
|
const CustomFontSetting({super.key});
|
|
|
|
@override
|
|
ConsumerState<CustomFontSetting> createState() => _CustomFontSettingState();
|
|
}
|
|
|
|
class _CustomFontSettingState extends ConsumerState<CustomFontSetting> {
|
|
bool _importing = false;
|
|
|
|
Future<void> _import() async {
|
|
setState(() => _importing = true);
|
|
try {
|
|
final file = await FilePicker.pickFile(
|
|
type: FileType.custom,
|
|
allowedExtensions: ['ttf', 'otf'],
|
|
);
|
|
if (file == null || !mounted) return;
|
|
if ((await file.length() ?? 0) > CustomFontService.maxBytes) {
|
|
throw const FormatException('Font exceeds import limit');
|
|
}
|
|
final id = await CustomFontService.instance.import(
|
|
await file.readAsBytes(),
|
|
);
|
|
if (!mounted) return;
|
|
await ref.read(themeProvider.notifier).setCustomFont(id, file.name);
|
|
} catch (_) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(context.l10n.appearanceCustomFontInvalid)),
|
|
);
|
|
}
|
|
} finally {
|
|
if (mounted) setState(() => _importing = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = ref.watch(themeProvider);
|
|
return ListTile(
|
|
leading: const Icon(Icons.text_fields),
|
|
title: Text(context.l10n.appearanceCustomFont),
|
|
subtitle: Text(
|
|
theme.customFontId.isEmpty
|
|
? context.l10n.appearanceCustomFontSubtitle
|
|
: theme.customFontName,
|
|
),
|
|
onTap: _importing ? null : _import,
|
|
trailing: _importing
|
|
? const SizedBox.square(
|
|
dimension: 24,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: theme.customFontId.isEmpty
|
|
? const Icon(Icons.add)
|
|
: IconButton(
|
|
tooltip: context.l10n.appearanceCustomFontReset,
|
|
onPressed: () =>
|
|
ref.read(themeProvider.notifier).setCustomFont('', ''),
|
|
icon: const Icon(Icons.close),
|
|
),
|
|
);
|
|
}
|
|
}
|