6367cce68a
An assistant reply gets Read aloud beside Copy; while it reads it becomes Pause/Resume and Stop, one reply at a time app-wide. The spoken word is highlighted in the rendered reply (across multi-part replies) and the transcript scrolls only when that word leaves the viewport. Reading stops when its chat closes or is swapped, another reply starts, or the app goes to the background. Markdown is spoken as sentences; code blocks are announced as "Code block.". Speech goes through a SpeechEngine seam with a flutter_tts implementation (Android, macOS, web; hidden on Linux) and a fake for tests. Settings has a Read aloud section: a voice picker grouped by language (device language first, a sample per voice), a rate slider and reset, saved per device. flutter_tts has no Swift package, so macOS/iOS gain the CocoaPods integration (Podfiles with the Runner deployment targets); Android declares the TTS_SERVICE query. The capture fixture gains ?speech=fake.
135 lines
4.5 KiB
Dart
135 lines
4.5 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter/material.dart';
|
||
|
||
import '../chat/speech/speech.dart';
|
||
import 'settings_group.dart';
|
||
import 'settings_row.dart';
|
||
import 'settings_theme.dart';
|
||
import 'settings_voice_picker.dart';
|
||
|
||
/// Read aloud: the voice and speaking rate replies are read with, saved on
|
||
/// this device, with a sample and a reset to the system voice at normal speed.
|
||
class SettingsReadAloudSection extends StatefulWidget {
|
||
const SettingsReadAloudSection({super.key, this.controller});
|
||
|
||
/// The reader; the app's own by default.
|
||
final SpeechController? controller;
|
||
|
||
@override
|
||
State<SettingsReadAloudSection> createState() =>
|
||
_SettingsReadAloudSectionState();
|
||
}
|
||
|
||
class _SettingsReadAloudSectionState extends State<SettingsReadAloudSection> {
|
||
SpeechController get _speech => widget.controller ?? speechController;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
unawaited(_speech.restore());
|
||
}
|
||
|
||
Future<void> _pickVoice() async {
|
||
final choice = await showSpeechVoicePicker(context, _speech);
|
||
if (choice != null) await _speech.setVoice(choice.voice);
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) => ListenableBuilder(
|
||
listenable: _speech,
|
||
builder: (context, _) {
|
||
final rate = _speech.rate;
|
||
final label = '${rate.toStringAsFixed(1)}×';
|
||
final voice = _speech.voice;
|
||
final sampling = _speech.messageId == speechSampleId;
|
||
return SettingsGroup(
|
||
title: 'Read aloud',
|
||
footer: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||
children: [
|
||
const Text(
|
||
'Saved on this device. Replies are read with this voice and rate.',
|
||
),
|
||
if (_speech.saveFailed)
|
||
Text(
|
||
'Could not save; applies until reload.',
|
||
style: TextStyle(color: settingsErrorColor(context)),
|
||
),
|
||
],
|
||
),
|
||
children: [
|
||
SettingsRow(
|
||
key: const Key('settings-read-aloud-voice'),
|
||
leading: const Icon(Icons.record_voice_over_outlined),
|
||
title: const Text('Voice'),
|
||
subtitle: Text(
|
||
voice == null
|
||
? 'System default'
|
||
: '${voice.name} · ${voice.locale}',
|
||
),
|
||
chevron: true,
|
||
onTap: () => unawaited(_pickVoice()),
|
||
),
|
||
SettingsGroupItem(
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||
children: [
|
||
Text(
|
||
'Speaking rate $label',
|
||
style: settingsTextStyle(context, title: true),
|
||
),
|
||
Semantics(
|
||
label: 'Speaking rate',
|
||
child: Slider(
|
||
key: const Key('settings-read-aloud-rate'),
|
||
value: rate,
|
||
min: minSpeechRate,
|
||
max: maxSpeechRate,
|
||
divisions: 15,
|
||
label: label,
|
||
semanticFormatterCallback: (value) =>
|
||
'${value.toStringAsFixed(1)} times',
|
||
onChanged: (value) =>
|
||
unawaited(_speech.setRate(value, save: false)),
|
||
onChangeEnd: (value) => unawaited(_speech.setRate(value)),
|
||
),
|
||
),
|
||
const Wrap(
|
||
alignment: WrapAlignment.spaceBetween,
|
||
children: [Text('0.5× · Slower'), Text('2.0× · Faster')],
|
||
),
|
||
],
|
||
),
|
||
),
|
||
SettingsGroupItem(
|
||
child: Wrap(
|
||
spacing: 8,
|
||
runSpacing: 8,
|
||
crossAxisAlignment: WrapCrossAlignment.center,
|
||
children: [
|
||
FilledButton.tonalIcon(
|
||
key: const Key('settings-read-aloud-sample'),
|
||
onPressed: () =>
|
||
unawaited(sampling ? _speech.stop() : _speech.sample()),
|
||
icon: Icon(
|
||
sampling ? Icons.stop_outlined : Icons.play_arrow_outlined,
|
||
),
|
||
label: Text(sampling ? 'Stop sample' : 'Play sample'),
|
||
),
|
||
TextButton(
|
||
key: const Key('settings-read-aloud-reset'),
|
||
onPressed: _speech.isDefault
|
||
? null
|
||
: () => unawaited(_speech.reset()),
|
||
child: const Text('Reset to defaults'),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
],
|
||
);
|
||
},
|
||
);
|
||
}
|