-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added option to choose a voice for TTS (#258)
* feat: added option to choose a voice for TTS * com#2 refactoring voice dropdown & using setConfig
- Loading branch information
1 parent
3fdf93a
commit eaed9d0
Showing
10 changed files
with
159 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import 'package:dairy_app/app/themes/theme_extensions/note_create_page_theme_extensions.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:flutter_tts/flutter_tts.dart'; | ||
|
||
import '../../features/auth/core/constants.dart'; | ||
import '../../features/auth/presentation/bloc/locale/locale_cubit.dart'; | ||
import '../../features/auth/presentation/bloc/user_config/user_config_cubit.dart'; | ||
|
||
class VoiceDropdown extends StatefulWidget { | ||
const VoiceDropdown({Key? key}) : super(key: key); | ||
|
||
@override | ||
State<VoiceDropdown> createState() => _VoiceDropdownState(); | ||
} | ||
|
||
class _VoiceDropdownState extends State<VoiceDropdown> { | ||
final FlutterTts _flutterTts = FlutterTts(); | ||
List<Map>? _voices; | ||
Map? selectedVoice; | ||
final bool _isInitialized = false; | ||
|
||
@override | ||
void didChangeDependencies() { | ||
super.didChangeDependencies(); | ||
|
||
if (!_isInitialized) { | ||
_initTts(); | ||
// _isInitialized = true; | ||
} | ||
} | ||
|
||
void _initTts() { | ||
final userConfigCubit = context.read<UserConfigCubit>(); | ||
final currentSelectedVoice = | ||
userConfigCubit.state.userConfigModel?.prefKeyVoice; | ||
|
||
final localeCubit = context.watch<LocaleCubit>().state; | ||
|
||
_flutterTts.getVoices.then((data) { | ||
List<Map> voices = List<Map>.from(data); | ||
|
||
voices = voices | ||
.where((voice) => | ||
voice["name"].contains(localeCubit.currentLocale.toString())) | ||
.toList(); | ||
|
||
print("voices = $voices"); | ||
|
||
setState(() { | ||
_voices = voices; | ||
|
||
selectedVoice = voices.firstWhere( | ||
(voice) => voice['name'] == currentSelectedVoice?['name'], | ||
orElse: () => voices.isNotEmpty | ||
? voices.first | ||
: {'name': 'Default', 'locale': 'en-US'}, | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final userConfigCubit = BlocProvider.of<UserConfigCubit>(context); | ||
final mainTextColor = Theme.of(context) | ||
.extension<NoteCreatePageThemeExtensions>()! | ||
.mainTextColor; | ||
|
||
return Row( | ||
children: [ | ||
Text( | ||
'Select Voice', | ||
style: TextStyle( | ||
fontSize: 16.0, | ||
color: mainTextColor, | ||
), | ||
), | ||
const Spacer(), | ||
PopupMenuButton<Map>( | ||
padding: const EdgeInsets.only(bottom: 0.0), | ||
onSelected: (value) async { | ||
setState(() { | ||
selectedVoice = value; | ||
}); | ||
|
||
userConfigCubit.setUserConfig( | ||
UserConfigConstants.prefKeyVoice, selectedVoice); | ||
}, | ||
itemBuilder: (context) => | ||
_voices?.map((voice) { | ||
return PopupMenuItem<Map>( | ||
value: voice, | ||
child: Text( | ||
voice['name'], | ||
style: TextStyle(color: mainTextColor), | ||
), | ||
); | ||
}).toList() ?? | ||
[], | ||
child: Row( | ||
mainAxisSize: MainAxisSize.min, | ||
children: <Widget>[ | ||
Text( | ||
selectedVoice?['name'] ?? 'Default', | ||
style: TextStyle( | ||
color: mainTextColor, | ||
fontSize: 16, | ||
), | ||
), | ||
const SizedBox(width: 10), | ||
Icon( | ||
Icons.keyboard_arrow_down, | ||
color: mainTextColor, | ||
), | ||
], | ||
), | ||
), | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
lib/features/auth/presentation/bloc/user_config/user_config_cubit.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters