import 'package:flutter/foundation.dart'; import 'package:flutter_tts/flutter_tts.dart'; class TtsService { static final TtsService _instance = TtsService._internal(); factory TtsService() => _instance; TtsService._internal(); final FlutterTts _flutterTts = FlutterTts(); bool _initialized = false; Future init() async { if (_initialized) return; try { await _flutterTts.setLanguage("zh-CN"); await _flutterTts.setSpeechRate(0.5); // 适中语速 await _flutterTts.setVolume(1.0); // 满音量 await _flutterTts.setPitch(1.0); _initialized = true; } catch (e) { debugPrint('TTS init error: $e'); } } Future speak(String text) async { try { if (!_initialized) { await init(); } await _flutterTts.stop(); await _flutterTts.speak(text); } catch (e) { debugPrint('TTS speak error: $e'); } } Future stop() async { try { await _flutterTts.stop(); } catch (_) {} } }